1
1
"""Views for Restaurants App."""
2
2
3
3
from django .db import transaction
4
- from django .http import Http404
4
+ from django .core . cache import cache
5
5
from django .shortcuts import get_object_or_404
6
6
from rest_framework .views import APIView
7
- from rest_framework .permissions import IsAuthenticatedOrReadOnly , AllowAny
8
7
from rest_framework .response import Response
9
8
from rest_framework import status
10
9
11
10
from apps .utilities .pagination import LargeSetPagination
12
11
from apps .foods .models import Food
13
- from apps .foods .serializers import FoodSerializer , FoodMiniSerializer
12
+ from apps .foods .serializers import FoodMiniSerializer
14
13
from apps .categories .models import Category
14
+ from apps .categories .serializers import CategoryListSerializer
15
15
from .models import Restaurant
16
16
from .serializers import RestaurantSerializer
17
- from .permissions import IsBusinessOwnerOrReadOnly
17
+ from .permissions import IsBusinessOrReadOnly
18
18
19
19
20
20
class RestaurantListAPIView (APIView ):
21
21
"""APIView to list and create restaurants."""
22
22
serializer_class = RestaurantSerializer
23
- permission_classes = [IsAuthenticatedOrReadOnly ]
23
+ permission_classes = [IsBusinessOrReadOnly ]
24
+ cache_key = "restaurant"
25
+ cache_timeout = 7200 # 2 hours
24
26
25
27
def get (self , request ):
26
28
# Get a list of restaurants
27
- stores = Restaurant .objects .filter (available = True ).order_by ("id" )
28
- if stores .exists ():
29
- paginator = LargeSetPagination ()
30
- paginated_data = paginator .paginate_queryset (stores , request )
29
+ paginator = LargeSetPagination ()
30
+ cached_data = cache .get (self .cache_key )
31
+
32
+ if cached_data is None :
33
+ restaurants = Restaurant .objects .get_all ()
34
+ if not restaurants .exists ():
35
+ return Response (
36
+ {"detail" : "No restaurants available." },
37
+ status = status .HTTP_404_NOT_FOUND
38
+ )
39
+ # Fetches the data from the database and serializes it
40
+ paginated_data = paginator .paginate_queryset (restaurants , request )
31
41
serializer = self .serializer_class (paginated_data , many = True )
32
- return paginator .get_paginated_response (serializer .data )
33
- return Response (
34
- {"detail" : "No restaurants available." },
35
- status = status .HTTP_404_NOT_FOUND
36
- )
42
+ # Set cache
43
+ cache .set (self .cache_key , serializer .data , self .cache_timeout )
44
+ else :
45
+ # Retrieve the cached data and serialize it
46
+ paginated_cached_data = paginator .paginate_queryset (
47
+ cached_data , request )
48
+ serializer = self .serializer_class (
49
+ paginated_cached_data , many = True )
50
+
51
+ return paginator .get_paginated_response (serializer .data )
37
52
38
53
@transaction .atomic
39
54
def post (self , request ):
40
55
# Create a new restaurant
41
56
serializer = self .serializer_class (data = request .data )
42
57
if serializer .is_valid ():
43
58
serializer .save ()
44
- return Response (
45
- serializer .data ,
46
- status = status .HTTP_201_CREATED
47
- )
48
- return Response (
49
- serializer .errors ,
50
- status = status .HTTP_400_BAD_REQUEST
51
- )
59
+ # Invalidate cache
60
+ cache .delete (self .cache_key )
61
+ return Response (serializer .data , status = status .HTTP_201_CREATED )
62
+ return Response (serializer .errors , status = status .HTTP_400_BAD_REQUEST )
52
63
53
64
54
65
class RestaurantDetailAPIView (APIView ):
55
66
"""APIView to retrieve, update, and delete a restaurant."""
56
67
serializer_class = RestaurantSerializer
57
- permission_classes = [IsBusinessOwnerOrReadOnly ]
68
+ permission_classes = [IsBusinessOrReadOnly ]
58
69
59
70
def get_object (self , restaurant_id ):
60
71
# Get a restaurant instance by id
61
- try :
62
- return Restaurant .objects .get (pk = restaurant_id )
63
- except Restaurant .DoesNotExist :
64
- raise Http404
72
+ return get_object_or_404 (Restaurant , pk = restaurant_id )
65
73
66
74
def get (self , request , restaurant_id ):
67
75
# Get details of a restaurant
68
- store = self .get_object (restaurant_id )
69
- serializer = self .serializer_class (store )
76
+ restaurant = self .get_object (restaurant_id )
77
+ serializer = self .serializer_class (restaurant )
70
78
return Response (serializer .data )
71
79
72
80
@transaction .atomic
73
81
def put (self , request , restaurant_id ):
74
82
# Update a restaurant
75
- store = self .get_object (restaurant_id )
76
- serializer = self .serializer_class (store , data = request .data )
83
+ restaurant = self .get_object (restaurant_id )
84
+ serializer = self .serializer_class (restaurant , data = request .data )
77
85
if serializer .is_valid ():
78
86
serializer .save ()
79
87
return Response (serializer .data )
80
- return Response (
81
- serializer .errors ,
82
- status = status .HTTP_400_BAD_REQUEST
83
- )
88
+ return Response (serializer .errors , status = status .HTTP_400_BAD_REQUEST )
84
89
85
90
@transaction .atomic
86
91
def delete (self , request , restaurant_id ):
87
92
# Delete a restaurant
88
- store = self .get_object (restaurant_id )
89
- store .delete ()
93
+ restaurant = self .get_object (restaurant_id )
94
+ restaurant .available = False # Logical deletion
95
+ restaurant .save ()
90
96
return Response (status = status .HTTP_204_NO_CONTENT )
91
97
92
98
93
99
class RestaurantCategoriesAPIView (APIView ):
94
- permission_classes = [IsAuthenticatedOrReadOnly ]
100
+ permission_classes = [IsBusinessOrReadOnly ]
101
+ serializer_class = CategoryListSerializer
95
102
96
- def get (self , request , restaurant_id , formate = None ):
103
+ def get (self , request , restaurant_id , format = None ):
97
104
# Get a list of categories associated with a restaurant
98
- categories = Category .filter (restaurant = restaurant_id )
105
+ categories = Category .objects . filter (restaurant = restaurant_id )
99
106
if categories .exists ():
100
107
paginator = LargeSetPagination ()
101
108
paginated_data = paginator .paginate_queryset (categories , request )
@@ -108,12 +115,12 @@ def get(self, request, restaurant_id, formate=None):
108
115
109
116
110
117
class RestaurantFoodsAPIView (APIView ):
111
- permission_classes = [IsAuthenticatedOrReadOnly ]
118
+ permission_classes = [IsBusinessOrReadOnly ]
112
119
113
120
def get (self , request , restaurant_id , format = None ):
114
121
# Get a list of foods for a restaurant
115
122
restaurant = get_object_or_404 (Restaurant , id = restaurant_id )
116
- foods = Food .objects .filter ( restaurant = restaurant )
123
+ foods = Food .objects .get_foods_by_restaurant ( restaurant )
117
124
if foods .exists ():
118
125
paginator = LargeSetPagination ()
119
126
paginated_data = paginator .paginate_queryset (foods , request )
@@ -123,52 +130,3 @@ def get(self, request, restaurant_id, format=None):
123
130
{"detail" : "No foods available." },
124
131
status = status .HTTP_404_NOT_FOUND
125
132
)
126
-
127
- @transaction .atomic
128
- def post (self , request , restaurant_id , format = None ):
129
- # Create a new food item for a restaurant
130
- restaurant = get_object_or_404 (Restaurant , id = restaurant_id )
131
- serializer = FoodSerializer (data = request .data )
132
- if serializer .is_valid ():
133
- serializer .save (restaurant = restaurant )
134
- return Response (
135
- serializer .data ,
136
- status = status .HTTP_201_CREATED
137
- )
138
- return Response (
139
- serializer .errors ,
140
- status = status .HTTP_400_BAD_REQUEST
141
- )
142
-
143
-
144
- class RestaurantFoodDetailAPIView (APIView ):
145
- permission_classes = [AllowAny ]
146
-
147
- def get (self , request , restaurant_id , food_id , format = None ):
148
- # Retrieve details for a food item associated with a restaurant
149
- restaurant = get_object_or_404 (Restaurant , id = restaurant_id )
150
- food = get_object_or_404 (Food , id = food_id , restaurant = restaurant )
151
- serializer = FoodSerializer (food )
152
- return Response (serializer .data )
153
-
154
- def put (self , request , restaurant_id , food_id , format = None ):
155
- # Update details for a food item associated with a restaurant
156
- restaurant = get_object_or_404 (Restaurant , id = restaurant_id )
157
- food = get_object_or_404 (Food , id = food_id , restaurant = restaurant )
158
- serializer = FoodSerializer (food , data = request .data )
159
- if serializer .is_valid ():
160
- serializer .save ()
161
- return Response (serializer .data )
162
- return Response (
163
- serializer .errors ,
164
- status = status .HTTP_400_BAD_REQUEST
165
- )
166
-
167
- def delete (self , request , restaurant_id , food_id , format = None ):
168
- # Delete a food item associated with a restaurant
169
- restaurant = get_object_or_404 (Restaurant , id = restaurant_id )
170
- food = get_object_or_404 (Food , id = food_id , restaurant = restaurant )
171
- food .delete ()
172
- return Response (status = status .HTTP_204_NO_CONTENT )
173
-
174
- # TODO: Add new permission
0 commit comments