@@ -19,44 +19,47 @@ class FixedCouponListAPIView(APIView):
19
19
"""APIView for listing and creating fixed coupons."""
20
20
permission_classes = [IsAuthenticated ]
21
21
serializer_class = FixedCouponSerializer
22
- pagination_class = MediumSetPagination
23
- CACHE_TIMEOUT = 7200 # Cache for 2 hours
22
+ cache_key = "fixed_coupon"
23
+ cache_timeout = 7200
24
24
25
25
def get (self , request , format = None ):
26
26
# Get a list of available fixed coupons
27
- cache_key = f"fixed_coupon_ { request . user . id } "
28
- cached_data = cache .get (cache_key )
27
+ paginator = MediumSetPagination ()
28
+ cached_data = cache .get (self . cache_key )
29
29
30
30
if cached_data is None :
31
- coupons = FixedCoupon .objects .filter (available = True ).order_by ("id" )
32
- if coupons .exists ():
33
- serializer = self .serializer_class (coupons , many = True )
34
- # Set cache
35
- cache .set (cache_key , serializer .data , self .CACHE_TIMEOUT )
36
- return Response (serializer .data , status = status .HTTP_200_OK )
37
- return Response (
38
- {"detail" : "No fixed coupons available." },
39
- status = status .HTTP_404_NOT_FOUND
40
- )
41
- return Response (cached_data , status = status .HTTP_200_OK )
31
+ coupons = FixedCoupon .objects .get_all ()
32
+ if not coupons .exists ():
33
+ return Response (
34
+ {"detail" : "No fixed coupons available." },
35
+ status = status .HTTP_404_NOT_FOUND
36
+ )
37
+ # Fetches the data from the database and serializes it
38
+ paginated_data = paginator .paginate_queryset (coupons , request )
39
+ serializer = self .serializer_class (paginated_data , many = True )
40
+ # Set cache
41
+ cache .set (self .cache_key , serializer .data , self .cache_timeout )
42
+ else :
43
+ # Retrieve the cached data and serialize it
44
+ paginated_cached_data = paginator .paginate_queryset (
45
+ cached_data , request )
46
+ serializer = self .serializer_class (
47
+ paginated_cached_data , many = True )
48
+
49
+ return paginator .get_paginated_response (serializer .data )
42
50
43
51
@transaction .atomic
44
52
def post (self , request , format = None ):
45
53
# Create a new fixed coupon
46
54
serializer = self .serializer_class (data = request .data )
55
+
47
56
if serializer .is_valid ():
48
57
serializer .save ()
49
58
# Invalidate cache
50
- cache_key = f"fixed_coupon_{ request .user .id } "
51
- cache .delete (cache_key )
52
- return Response (
53
- serializer .data ,
54
- status = status .HTTP_201_CREATED
55
- )
59
+ cache .delete (self .cache_key )
60
+ return Response (serializer .data , status = status .HTTP_201_CREATED )
56
61
return Response (
57
- serializer .errors ,
58
- status = status .HTTP_400_BAD_REQUEST
59
- )
62
+ serializer .errors , status = status .HTTP_400_BAD_REQUEST )
60
63
61
64
62
65
class FixedCouponDetailAPIView (APIView ):
@@ -91,35 +94,43 @@ def put(self, request, fixed_coupon_id):
91
94
def delete (self , request , fixed_coupon_id ):
92
95
# Delete a fixed coupon
93
96
fixed_coupon = self .get_object (fixed_coupon_id )
94
- fixed_coupon .delete ()
97
+ fixed_coupon .available = False # Logical deletion
98
+ fixed_coupon .save ()
95
99
return Response (status = status .HTTP_204_NO_CONTENT )
96
100
97
101
98
102
class PercentageCouponListAPIView (APIView ):
99
103
"""APIView for listing and creating percentage coupons."""
100
104
permission_classes = [IsAuthenticated ]
101
105
serializer_class = PercentageCouponSerializer
102
- pagination_class = MediumSetPagination
103
- CACHE_TIMEOUT = 7200 # Cache for 2 hours
106
+ cache_key = "percentage_coupon"
107
+ cache_timeout = 7200
104
108
105
109
def get (self , request , format = None ):
106
110
# Get a list of available percentage coupons
107
- cache_key = f"percentage_coupon_ { request . user . id } "
108
- cached_data = cache .get (cache_key )
111
+ paginator = MediumSetPagination ()
112
+ cached_data = cache .get (self . cache_key )
109
113
110
114
if cached_data is None :
111
- coupons = PercentageCoupon .objects .filter (
112
- available = True ).order_by ("id" )
113
- if coupons .exists ():
114
- serializer = self .serializer_class (coupons , many = True )
115
- # Set cache
116
- cache .set (cache_key , serializer .data , self .CACHE_TIMEOUT )
117
- return Response (serializer .data , status = status .HTTP_200_OK )
118
- return Response (
119
- {"detail" : "No percentage coupons available." },
120
- status = status .HTTP_404_NOT_FOUND
121
- )
122
- return Response (cached_data , status = status .HTTP_200_OK )
115
+ coupons = PercentageCoupon .objects .get_all ()
116
+ if not coupons .exists ():
117
+ return Response (
118
+ {"detail" : "No percentage coupons available." },
119
+ status = status .HTTP_404_NOT_FOUND
120
+ )
121
+
122
+ paginated_data = paginator .paginate_queryset (coupons , request )
123
+ serializer = self .serializer_class (paginated_data , many = True )
124
+ # Set cache
125
+ cache .set (self .cache_key , serializer .data , self .cache_timeout )
126
+ else :
127
+ # Retrieve the cached data and serialize it
128
+ paginated_cached_data = paginator .paginate_queryset (
129
+ cached_data , request )
130
+ serializer = self .serializer_class (
131
+ paginated_cached_data , many = True )
132
+
133
+ return paginator .get_paginated_response (serializer .data )
123
134
124
135
@transaction .atomic
125
136
def post (self , request , format = None ):
@@ -128,16 +139,9 @@ def post(self, request, format=None):
128
139
if serializer .is_valid ():
129
140
serializer .save ()
130
141
# Invalidate cache
131
- cache_key = f"percentage_coupon_{ request .user .id } "
132
- cache .delete (cache_key )
133
- return Response (
134
- serializer .data ,
135
- status = status .HTTP_201_CREATED
136
- )
137
- return Response (
138
- serializer .errors ,
139
- status = status .HTTP_400_BAD_REQUEST
140
- )
142
+ cache .delete (self .cache_key )
143
+ return Response (serializer .data , status = status .HTTP_201_CREATED )
144
+ return Response (serializer .errors , status = status .HTTP_400_BAD_REQUEST )
141
145
142
146
143
147
class PercentageCouponDetailAPIView (APIView ):
@@ -174,7 +178,8 @@ def put(self, request, percentage_coupon_id):
174
178
def delete (self , request , percentage_coupon_id ):
175
179
# Delete a percentage coupon
176
180
percentage_coupon = self .get_object (percentage_coupon_id )
177
- percentage_coupon .delete ()
181
+ percentage_coupon .available = False # Logical deletion
182
+ percentage_coupon .save ()
178
183
return Response (status = status .HTTP_204_NO_CONTENT )
179
184
180
185
0 commit comments