viewsets.py 3.77 KB
Newer Older
Gijs Hendriksen's avatar
Gijs Hendriksen committed
1
2
from django.db import IntegrityError
from rest_framework import permissions
3
4
5
from rest_framework.decorators import action
from rest_framework.exceptions import (ValidationError, NotFound,
                                       PermissionDenied)
6
from rest_framework.generics import get_object_or_404
Gijs Hendriksen's avatar
Gijs Hendriksen committed
7
8
from rest_framework.mixins import ListModelMixin
from rest_framework.response import Response
9
from rest_framework.viewsets import GenericViewSet, ModelViewSet
Gijs Hendriksen's avatar
Gijs Hendriksen committed
10

11
from payments.models import Payment
Gijs Hendriksen's avatar
Gijs Hendriksen committed
12
from pizzas.api import serializers
13
from pizzas.models import Product, PizzaEvent, Order
14
from pizzas.services import can_change_order
Gijs Hendriksen's avatar
Gijs Hendriksen committed
15
16
17


class PizzaViewset(GenericViewSet, ListModelMixin):
Thom Wiggers's avatar
Thom Wiggers committed
18
    queryset = Product.available_products.all()
Gijs Hendriksen's avatar
Gijs Hendriksen committed
19
20
21
22
23
24
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = serializers.PizzaSerializer

    def list(self, request, *args, **kwargs):
        if (PizzaEvent.current() or
                request.user.has_perm('pizzas.change_product')):
Thom Wiggers's avatar
Thom Wiggers committed
25
26
27
28
29
            queryset = self.get_queryset()
            if not request.user.has_perm('pizzas.order_restricted_products'):
                queryset = queryset.exclude(restricted=True)
            serializer = serializers.PizzaSerializer(queryset, many=True)
            return Response(serializer.data)
30
        raise PermissionDenied
Gijs Hendriksen's avatar
Gijs Hendriksen committed
31

32
    @action(detail=False)
Gijs Hendriksen's avatar
Gijs Hendriksen committed
33
34
35
36
    def event(self, request):
        event = PizzaEvent.current()

        if event:
37
38
39
            context = {'request': request}
            serializer = serializers.PizzaEventSerializer(event,
                                                          context=context)
40
            return Response(serializer.data)
Gijs Hendriksen's avatar
Gijs Hendriksen committed
41

42
        raise NotFound
Gijs Hendriksen's avatar
Gijs Hendriksen committed
43
44
45
46
47
48
49
50


class OrderViewset(ModelViewSet):
    permission_classes = (permissions.IsAuthenticated,)
    queryset = Order.objects.all()

    def get_queryset(self):
        event = PizzaEvent.current()
51
        if can_change_order(self.request.member, event):
Gijs Hendriksen's avatar
Gijs Hendriksen committed
52
            return Order.objects.filter(pizza_event=event)
53
        if self.action == 'update' or self.action == 'destroy':
Gijs Hendriksen's avatar
Gijs Hendriksen committed
54
55
56
            if not event or event.has_ended:
                return Order.objects.none()

57
            return Order.objects.filter(member=self.request.member,
58
                                        payment__type=Payment.NONE,
Gijs Hendriksen's avatar
Gijs Hendriksen committed
59
                                        pizza_event=event)
60
        return Order.objects.filter(member=self.request.member,
Gijs Hendriksen's avatar
Gijs Hendriksen committed
61
62
63
                                    pizza_event=event)

    def get_serializer_class(self):
64
65
66
        if (self.action.endswith('update') and
                can_change_order(self.request.member,
                                 self.get_object().pizza_event)):
Gijs Hendriksen's avatar
Gijs Hendriksen committed
67
68
69
70
            return serializers.AdminOrderSerializer
        return serializers.OrderSerializer

    def get_object(self):
71
        if self.kwargs.get(self.lookup_field) == 'me':
72
            order = get_object_or_404(self.get_queryset(),
73
                                      member=self.request.member,
Gijs Hendriksen's avatar
Gijs Hendriksen committed
74
75
76
77
78
79
80
81
82
83
                                      pizza_event=PizzaEvent.current())
            self.check_object_permissions(self.request, order)
            return order
        return super().get_object()

    def perform_create(self, serializer):
        try:
            if serializer.validated_data.get('name'):
                serializer.save(pizza_event=PizzaEvent.current())
            else:
84
85
86
                if (self.action.endswith('update') and
                    can_change_order(self.request.member,
                                     PizzaEvent.current())):
87
88
89
90
                    serializer.save(pizza_event=PizzaEvent.current())
                else:
                    serializer.save(member=self.request.member,
                                    pizza_event=PizzaEvent.current())
Gijs Hendriksen's avatar
Gijs Hendriksen committed
91
92
        except IntegrityError:
            raise ValidationError('Something went wrong when saving the order')