-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
413 lines (355 loc) · 17.3 KB
/
views.py
File metadata and controls
413 lines (355 loc) · 17.3 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from django.db.models import Count
from django.shortcuts import get_object_or_404
from django.http import HttpResponse
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin, DestroyModelMixin, RetrieveModelMixin, UpdateModelMixin
from rest_framework.decorators import api_view, action
from rest_framework.permissions import IsAuthenticated, AllowAny, IsAdminUser
from .permissions import IsAdminOrReadOnly
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet, GenericViewSet
from .models import Product, Collection, Customer, Order, OrderItem, Review, Cart, CartItem
from .serializers import ProductSerializer, CollectionSerializer, CustomerSerializer, ReviewSerializer, CartSerializer, OrderSerializer, CreateOrderSerializer, UpdateOrderSerializer, CartItemSerializer, AddCartItemSerializer, UpdateCartItemSerializer
# ======================
# ViewSets
# ======================
class ProductViewSet(ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
permission_classes = [IsAdminOrReadOnly] # Only admin users can edit products
def get_serializer_context(self):
return {'request': self.request}
def destroy(self, request, *args, **kwargs):
if OrderItem.objects.filter(product_id=kwargs['pk']).count() > 0:
return Response(
{
'error': 'Product cannot be deleted because it is associated with an order item.'
},
status=status.HTTP_405_METHOD_NOT_ALLOWED)
return super().destroy(request, *args, **kwargs)
# Use he above destry() method to delete the product in ViewSet method
# def delete(self, request, pk):
# product = get_object_or_404(Product, pk=pk)
# if product.orderitems.count() > 0:
# return Response(
# {
# 'error': 'Product cannot be deleted because it is associated with an order item.'
# },
# status=status.HTTP_405_METHOD_NOT_ALLOWED)
# product.delete()
# return Response(status=status.HTTP_204_NO_CONTENT)
class CollectionViewSet(ModelViewSet):
queryset = Collection.objects.annotate(products_count=Count('products')).all()
serializer_class = CollectionSerializer
permission_classes = [IsAdminOrReadOnly] # Only admin users can edit products
def destroy(self, request, *args, **kwargs):
if Product.objects.filter(collection_id=kwargs['pk']).count() > 0:
return Response(
{
'error': 'Collection cannot be deleted because it includes one or more products.'
},
status=status.HTTP_405_METHOD_NOT_ALLOWED)
return super().destroy(request, *args, **kwargs)
# def delete(self, request, pk):
# collection = get_object_or_404(Collection, pk=pk)
# if collection.products.count() > 0:
# return Response(
# {
# 'error': 'Collection cannot be deleted because it includes one or more products.'
# },
# status=status.HTTP_405_METHOD_NOT_ALLOWED)
# collection.delete()
# return Response(status=status.HTTP_204_NO_CONTENT)
class ReviewViewSet(ModelViewSet):
# queryset = Review.objects.all() # This will return all the reviews
serializer_class = ReviewSerializer
# Filter reviews based on product_id
def get_queryset(self):
return Review.objects.filter(product_id=self.kwargs['product_pk'])
# Pass additional context to serializer
def get_serializer_context(self):
return {'product_id': self.kwargs['product_pk']}
class CartViewSet(CreateModelMixin,
RetrieveModelMixin,
DestroyModelMixin,
GenericViewSet):
# queryset = Cart.objects.all()
queryset = Cart.objects.prefetch_related('items__product').all()
serializer_class = CartSerializer
class CartItemViewSet(ModelViewSet):
http_method_names = ['get', 'post', 'patch', 'delete']
# queryset = CartItem.objects.all()
# serializer_class = CartItemSerializer
def get_serializer_class(self):
if self.request.method == 'POST':
return AddCartItemSerializer
if self.request.method == 'PATCH':
return UpdateCartItemSerializer
return CartItemSerializer
# Pass additional context to serializer
def get_serializer_context(self):
return {'cart_id': self.kwargs['cart_pk']}
# Filter cart items based on cart_id
def get_queryset(self):
# return CartItem.objects.filter(cart_id=self.kwargs['cart_pk']) # Extra SQl queries
return CartItem.objects.filter(cart_id=self.kwargs['cart_pk']).select_related('product')
# class CusdtomerViewSet(CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, GenericViewSet):
class CusdtomerViewSet(ModelViewSet):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
# permission_classes = [IsAuthenticated] # Only authenticated users can access this view
permission_classes = [IsAdminUser]
"""
Override the get_permissions method to set permissions based on request method
GET is allowed by anyone
PUT is allowed only for authenticated users
"""
# def get_permissions(self):
# if self.request.method == 'GET':
# return [AllowAny()]
# return [IsAuthenticated()]
"""
Override the 'me' method below and apply the permissions
"""
@action(detail=False, methods=['GET', 'PUT'], permission_classes=[IsAuthenticated])
def me(self, request):
# customer = Customer.objects.get_or_create(user_id=request.user.id) # Get the customer object based on the user id
# Above line will only return a Tuple with 2 values
# Unpack the tuple to get the customer object
(customer, created) = Customer.objects.get_or_create(user_id=request.user.id) # Get the customer object based on the user id
if request.method == 'GET':
serializer = CustomerSerializer(customer)
return Response(serializer.data)
# return Response(request.user.id)
elif request.method == 'PUT':
serializer = CustomerSerializer(customer, data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
class OrderViewSet(ModelViewSet):
http_method_names = ['get', 'patch', 'delete', 'head', 'options']
# queryset = Order.objects.all() # This will return all the Orders
# serializer_class = OrderSerializer # Override this below
permission_classes = [IsAuthenticated] # Replace this with the below method
def get_permissions(self):
if self.request.method in ['PATCH', 'DELETE']:
return [IsAdminUser()]
return [IsAuthenticated()]
# Override the create method so that w ecan return 'order' object
def create(self, request, *args, **kwargs):
serializer = CreateOrderSerializer(
data=request.data,
context={'user_id': request.user.id}
)
serializer.is_valid(raise_exception=True)
order = serializer.save()
serializer = OrderSerializer(order)
return Response(serializer.data, status=status.HTTP_201_CREATED)
def get_serializer_class(self):
if self.request.method == 'POST':
return CreateOrderSerializer
elif self.request.method == 'PATCH':
return UpdateOrderSerializer
return OrderSerializer
# Pass additional context to serializer
# This is not needed as we are passing the context in the create method
# def get_serializer_context(self):
# return {'user_id': self.request.user.id}
def get_queryset(self):
user = self.request.user
# If the user is an admin, return all orders
if user.is_staff:
return Order.objects.all()
# If the user is not an admin, return only the orders for the logged-in user
# Filter orders based on customer_id
customer_id = Customer.objects.only('id').get(user_id=user.id)
return Order.objects.filter(customer_id=customer_id)
# ========================================================================================
# Generic API Views
# Lecture: https://members.codewithmosh.com/courses/the-ultimate-django-part2-1/lectures/34900537
# ========================================================================================
# class ProductList(ListCreateAPIView):
# queryset = Product.objects.select_related('collection').all()
# serializer_class = ProductSerializer
# def get_serializer_context(self):
# return {'request': self.request}
# class ProductDetail(RetrieveUpdateDestroyAPIView):
# queryset = Product.objects.all()
# serializer_class = ProductSerializer
# # lookup_field = 'id' # set <int:pk> as lookup field in urls.py
# def delete(self, request, pk):
# product = get_object_or_404(Product, pk=pk)
# if product.orderitems.count() > 0:
# return Response(
# {
# 'error': 'Product cannot be deleted because it is associated with an order item.'
# },
# status=status.HTTP_405_METHOD_NOT_ALLOWED)
# product.delete()
# return Response(status=status.HTTP_204_NO_CONTENT)
# class CollectionList(ListCreateAPIView):
# queryset = Collection.objects.annotate(products_count=Count('products')).all()
# serializer_class = CollectionSerializer
# class CollectionDetail(RetrieveUpdateDestroyAPIView):
# queryset = Collection.objects.annotate(products_count=Count('products')).all()
# serializer_class = CollectionSerializer
# def delete(self, request, pk):
# collection = get_object_or_404(Collection, pk=pk)
# if collection.products.count() > 0:
# return Response(
# {
# 'error': 'Collection cannot be deleted because it includes one or more products.'
# },
# status=status.HTTP_405_METHOD_NOT_ALLOWED)
# collection.delete()
# return Response(status=status.HTTP_204_NO_CONTENT)
# ======================
# Class Based API Views
# ======================
# class ProductList(APIView):
# def get(self, request):
# products = Product.objects.select_related('collection').all() # select_related for joining tables
# serializer = ProductSerializer(products, many=True, context={'request': request}) # many=True for iterating on multiple objects
# return Response(serializer.data)
# def post(self, request):
# serializer = ProductSerializer(data=request.data)
# serializer.is_valid(raise_exception=True)
# serializer.save()
# return Response(serializer.data, status=status.HTTP_201_CREATED)
# class ProductDetail(APIView):
# def get(self, request, id):
# product = get_object_or_404(Product, pk=id)
# serializer = ProductSerializer(product)
# return Response(serializer.data)
# def put(self, request, id):
# product = get_object_or_404(Product, pk=id)
# serializer = ProductSerializer(product, data=request.data)
# serializer.is_valid(raise_exception=True)
# serializer.save()
# return Response(serializer.data)
# def delete(self, request, id):
# product = get_object_or_404(Product, pk=id)
# if product.orderitems.count() > 0:
# return Response(
# {
# 'error': 'Product cannot be deleted because it is associated with an order item.'
# },
# status=status.HTTP_405_METHOD_NOT_ALLOWED)
# product.delete()
# return Response(status=status.HTTP_204_NO_CONTENT)
# class CollectionList(APIView):
# def get(self, request):
# collections = Collection.objects.annotate(products_count=Count('products')).all()
# serializer = CollectionSerializer(collections, many=True)
# return Response(serializer.data)
# def post(self, request):
# serializer = CollectionSerializer(data=request.data)
# serializer.is_valid(raise_exception=True)
# serializer.save()
# return Response(serializer.data, status=status.HTTP_201_CREATED)
# class CollectionDetail(APIView):
# def get(self, request, pk):
# collection = get_object_or_404(Collection.objects.annotate(products_count=Count('products')), pk=pk)
# serializer = CollectionSerializer(collection)
# return Response(serializer.data)
# def put(self, request, pk):
# collection = get_object_or_404(Collection, pk=pk)
# serializer = CollectionSerializer(collection, data=request.data)
# serializer.is_valid(raise_exception=True)
# serializer.save()
# return Response(serializer.data)
# def delete(self, request, pk):
# collection = get_object_or_404(Collection, pk=pk)
# if collection.products.count() > 0:
# return Response(
# {
# 'error': 'Collection cannot be deleted because it includes one or more products.'
# },
# status=status.HTTP_405_METHOD_NOT_ALLOWED)
# collection.delete()
# return Response(status=status.HTTP_204_NO_CONTENT)
# ==========================
# Function Based API Views
# ==========================
# @api_view(['GET', 'POST'])
# def product_list(request):
# if request.method == 'GET':
# products = Product.objects.select_related('collection').all() # select_related for joining tables
# serializer = ProductSerializer(products, many=True, context={'request': request}) # many=True for iterating on multiple objects
# return Response(serializer.data)
# elif request.method == 'POST':
# serializer = ProductSerializer(data=request.data)
# # if serializer.is_valid():
# # print(serializer.validated_data)
# # return Response('OK')
# # else:
# # return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# serializer.is_valid(raise_exception=True)
# serializer.save()
# return Response(serializer.data, status=status.HTTP_201_CREATED)
# @api_view(['GET', 'PUT', 'DELETE'])
# def product_detail(request, id):
# product = get_object_or_404(Product, pk=id)
# if request.method == 'GET':
# serializer = ProductSerializer(product)
# return Response(serializer.data)
# elif request.method == 'PUT':
# serializer = ProductSerializer(product, data=request.data)
# serializer.is_valid(raise_exception=True)
# serializer.save()
# return Response(serializer.data)
# elif request.method == 'DELETE':
# if product.orderitems.count() > 0:
# return Response(
# {
# 'error': 'Product cannot be deleted because it is associated with an order item.'
# },
# status=status.HTTP_405_METHOD_NOT_ALLOWED)
# product.delete()
# return Response(status=status.HTTP_204_NO_CONTENT)
# @api_view(['GET', 'POST'])
# def collection_list(request):
# if request.method == 'GET':
# queryset = Collection.objects.annotate(products_count=Count('products')).all()
# serializer = CollectionSerializer(queryset, many=True)
# return Response(serializer.data)
# # return Response(f'Collection List Page: {queryset}')
# elif request.method == 'POST':
# serializer = CollectionSerializer(data=request.data)
# serializer.is_valid(raise_exception=True)
# serializer.save()
# return Response(serializer.data, status=status.HTTP_201_CREATED)
# @api_view(['GET', 'PUT', 'DELETE'])
# def collection_detail(request, pk):
# # collection = get_object_or_404(Collection, pk=pk) # pk=pk is same as id=pk
# collection = get_object_or_404(
# Collection.objects.annotate(products_count=Count('products'), pk=pk)
# )
# if request.method == 'GET':
# serializer = CollectionSerializer(collection)
# return Response(serializer.data)
# # return Response(f'Collection Detail Page: {pk}')
# elif request.method == 'PUT':
# serializer = CollectionSerializer(collection, data=request.data)
# serializer.is_valid(raise_exception=True)
# serializer.save()
# return Response(serializer.data)
# elif request.method == 'DELETE':
# if collection.products.count() > 0:
# return Response({
# 'error': 'Collection cannot be deleted because it contains products.'
# }, status=status.HTTP_405_METHOD_NOT_ALLOWED)
# collection.delete()
# return Response(status=status.HTTP_204_NO_CONTENT)
# ==================================================================================
# Without get_obj_or_404
# def product_detail(request, id):
# try:
# product = Product.objects.get(pk=id)
# serializer = ProductSerializer(product)
# return Response(serializer.data)
# # return Response(f'Product Detail Page: {id}')
# except Product.DoesNotExist:
# return Response({'error': 'Product not found'}, status=status.HTTP_404_NOT_FOUND)