This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooking_sit.py
124 lines (105 loc) · 4.91 KB
/
booking_sit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from django.core.exceptions import ObjectDoesNotExist
from django.template.defaultfilters import slugify
from rest_framework import mixins, viewsets
from rest_framework.response import Response
from koie_booking.models.booking import BookingModel
from koie_booking.serializers.booking_sit import BookingSitSerializer
from koie_report.permissions import IsSitMemberOrKoieAdmin
from ntnui.enums import KeyStatus
from django.utils.translation import gettext as _
class BookingSitViewSet(
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet,
):
queryset = BookingModel.objects.all()
serializer_class = BookingSitSerializer
lookup_field = "uuid"
ordering_fields = ("created", "arrival_date", "departure_date", "user", "koie")
permission_classes = (IsSitMemberOrKoieAdmin,)
def filter_queryset_key_status(self):
"""Filters the queryset based on key_status, and checks that key_status is valid"""
if self.request.query_params.get("key_status") and any(
[self.request.query_params.get("key_status") == status.name for status in KeyStatus]
):
return self.queryset.filter(
key_status=KeyStatus[self.request.query_params.get("key_status")]
)
return self.queryset
def list(self, request):
"""
Gets bookings for sit view.
QueryParams: [key_status, koie, arrival_date_start,
arrival_date_end, departure_date_start, departure_date_end,
order_by]
Dates are provided in ISO-format: YYYY-MM-DD
"""
# Filter on key_status
self.queryset = self.filter_queryset_key_status()
# Filter on koie
koie = self.request.query_params.get("koie", None)
if koie:
self.queryset = self.queryset.filter(koie__slug=slugify(koie))
# Filter on arrival_date
arrival_date_start = self.request.query_params.get("arrival_date_start", None)
if arrival_date_start:
self.queryset = self.queryset.filter(arrival_date__gte=arrival_date_start)
arrival_date_end = self.request.query_params.get("arrival_date_end", None)
if arrival_date_end:
self.queryset = self.queryset.filter(arrival_date__lte=arrival_date_end)
# Filter on departure_date
departure_date_start = self.request.query_params.get("departure_date_start", None)
if departure_date_start:
self.queryset = self.queryset.filter(departure_date__gte=departure_date_start)
departure_date_end = self.request.query_params.get("departure_date_end", None)
if departure_date_end:
self.queryset = self.queryset.filter(departure_date__lte=departure_date_end)
# Ordering
order = self.request.query_params.get("order_by", None)
if order and order in self.ordering_fields:
self.queryset = self.queryset.order_by(order)
else:
self.queryset = self.queryset.order_by(self.ordering_fields[0])
serializer = BookingSitSerializer(self.queryset, context={"request": request}, many=True)
return Response(serializer.data)
def retrieve(self, request, uuid):
try:
booking = self.queryset.get(uuid=uuid)
serializer = BookingSitSerializer(booking, context={"request": request})
return Response(serializer.data)
except BookingModel.DoesNotExist:
return Response({"detail": _("Booking not found.")}, status=404)
def update(self, request, *args, **kwargs):
""" Returns a response saying that the method is not allowed. """
return Response({"detail": "Not yet implemented use PATCH"}, status=501)
def partial_update(self, request, uuid=None):
""" Changes field: key_status. Requires fields key_status and koie to be specified """
try:
self.check_object_permissions(self.request, self.get_object())
serializer = BookingSitSerializer(
self.get_object(),
context={"request": request},
many=False,
data=request.data,
partial=True,
)
if serializer.is_valid():
self.perform_update(serializer)
return Response({"detail": _("Changes were made")})
else:
return Response({"detail": serializer.errors}, status=400)
except BookingModel.DoesNotExist:
return Response({"detail": "Could not find booking"}, status=400)
except ObjectDoesNotExist:
return Response({"detail": "Could not find object"}, status=400)
except KeyError:
return Response(
{
"detail": (
"key_status can only be one of:"
" 'not_picked_up', 'picked_up', 'delivered'"
)
},
status=400,
)