viewsets.py 3.36 KB
Newer Older
Gijs Hendriksen's avatar
Gijs Hendriksen committed
1
2
3
from django.db import IntegrityError

from rest_framework import permissions
4
from rest_framework.generics import get_object_or_404
Gijs Hendriksen's avatar
Gijs Hendriksen committed
5
6
7
8
from rest_framework.viewsets import GenericViewSet, ModelViewSet
from rest_framework.mixins import ListModelMixin
from rest_framework.decorators import list_route
from rest_framework.response import Response
9
10
from rest_framework.exceptions import (ValidationError, NotFound,
                                       PermissionDenied)
Gijs Hendriksen's avatar
Gijs Hendriksen committed
11
12
13
14
15
16

from pizzas.models import Product, PizzaEvent, Order
from pizzas.api import serializers


class PizzaViewset(GenericViewSet, ListModelMixin):
Thom Wiggers's avatar
Thom Wiggers committed
17
    queryset = Product.available_products.all()
Gijs Hendriksen's avatar
Gijs Hendriksen committed
18
19
20
21
22
23
    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
24
25
26
27
28
            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)
29
        raise PermissionDenied
Gijs Hendriksen's avatar
Gijs Hendriksen committed
30
31
32
33
34
35

    @list_route()
    def event(self, request):
        event = PizzaEvent.current()

        if event:
36
37
            serializer = serializers.PizzaEventSerializer(event)
            return Response(serializer.data)
Gijs Hendriksen's avatar
Gijs Hendriksen committed
38

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


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

    def get_queryset(self):
        event = PizzaEvent.current()
        if self.request.user.has_perm('pizzas.change_order'):
            return Order.objects.filter(pizza_event=event)
50
        if self.action == 'update' or self.action == 'destroy':
Gijs Hendriksen's avatar
Gijs Hendriksen committed
51
52
53
            if not event or event.has_ended:
                return Order.objects.none()

54
            return Order.objects.filter(member=self.request.member,
Gijs Hendriksen's avatar
Gijs Hendriksen committed
55
56
                                        paid=False,
                                        pizza_event=event)
57
        return Order.objects.filter(member=self.request.member,
Gijs Hendriksen's avatar
Gijs Hendriksen committed
58
59
60
                                    pizza_event=event)

    def get_serializer_class(self):
61
        if self.request.member.has_perm('pizzas.change_order'):
Gijs Hendriksen's avatar
Gijs Hendriksen committed
62
63
64
65
66
            return serializers.AdminOrderSerializer
        return serializers.OrderSerializer

    def get_object(self):
        if self.kwargs[self.lookup_field] == 'me':
67
            order = get_object_or_404(self.get_queryset(),
68
                                      member=self.request.member,
Gijs Hendriksen's avatar
Gijs Hendriksen committed
69
70
71
72
73
74
75
76
77
78
                                      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:
79
80
81
82
83
                if self.request.user.has_perm('pizzas.change_order'):
                    serializer.save(pizza_event=PizzaEvent.current())
                else:
                    serializer.save(member=self.request.member,
                                    pizza_event=PizzaEvent.current())
Gijs Hendriksen's avatar
Gijs Hendriksen committed
84
85
        except IntegrityError:
            raise ValidationError('Something went wrong when saving the order')