viewsets.py 3.12 KB
Newer Older
Gijs Hendriksen's avatar
Gijs Hendriksen committed
1
2
3
4
5
6
7
8
from django.shortcuts import get_object_or_404
from django.db import IntegrityError

from rest_framework import permissions
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
17
18
19
20
21
22
23
24

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


class PizzaViewset(GenericViewSet, ListModelMixin):
    queryset = Product.objects.filter(available=True)
    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')):
            return super().list(request, *args, **kwargs)
25
        raise PermissionDenied
Gijs Hendriksen's avatar
Gijs Hendriksen committed
26
27
28
29
30
31

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

        if event:
32
33
            serializer = serializers.PizzaEventSerializer(event)
            return Response(serializer.data)
Gijs Hendriksen's avatar
Gijs Hendriksen committed
34

35
        raise NotFound
Gijs Hendriksen's avatar
Gijs Hendriksen committed
36
37
38
39
40
41
42
43
44
45


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)
46
        if self.action == 'update' or self.action == 'destroy':
Gijs Hendriksen's avatar
Gijs Hendriksen committed
47
48
49
            if not event or event.has_ended:
                return Order.objects.none()

50
            return Order.objects.filter(member=self.request.member,
Gijs Hendriksen's avatar
Gijs Hendriksen committed
51
52
                                        paid=False,
                                        pizza_event=event)
53
        return Order.objects.filter(member=self.request.member,
Gijs Hendriksen's avatar
Gijs Hendriksen committed
54
55
56
                                    pizza_event=event)

    def get_serializer_class(self):
57
        if self.request.member.has_perm('pizzas.change_order'):
Gijs Hendriksen's avatar
Gijs Hendriksen committed
58
59
60
61
62
            return serializers.AdminOrderSerializer
        return serializers.OrderSerializer

    def get_object(self):
        if self.kwargs[self.lookup_field] == 'me':
63
            order = get_object_or_404(self.get_queryset(),
64
                                      member=self.request.member,
Gijs Hendriksen's avatar
Gijs Hendriksen committed
65
66
67
68
69
70
71
72
73
74
                                      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:
75
76
77
78
79
                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
80
81
        except IntegrityError:
            raise ValidationError('Something went wrong when saving the order')