@@ -241,7 +241,22 @@ def get(self, request, user=None):
241
241
list of order object and 200 status if no error
242
242
message and corresponding status if error
243
243
"""
244
- orders = Order .objects .filter (user = user )
244
+ limit = request .GET .get ('limit' , str (settings .DEFAULT_LIMIT ))
245
+ offset = request .GET .get ('offset' , str (settings .DEFAULT_OFFSET ))
246
+ if not limit .isdigit () or not offset .isdigit ():
247
+ return Response (
248
+ {'message' : messages .INVALID_LIMIT_OR_OFFSET },
249
+ status = status .HTTP_400_BAD_REQUEST
250
+ )
251
+ limit = int (limit )
252
+ offset = int (offset )
253
+ if limit > settings .MAX_LIMIT :
254
+ limit = 100
255
+ if limit < 0 :
256
+ limit = settings .DEFAULT_LIMIT
257
+ if offset < 0 :
258
+ offset = settings .DEFAULT_OFFSET
259
+ orders = Order .objects .filter (user = user ).order_by ('-id' )[offset :offset + limit ]
245
260
serializer = OrderSerializer (orders , many = True )
246
261
response_data = dict (
247
262
orders = serializer .data
@@ -329,14 +344,21 @@ def post(self, request, user=None):
329
344
if not serializer .is_valid ():
330
345
log_error (request .path , request .data , 400 , serializer .errors )
331
346
return Response (serializer .errors , status = status .HTTP_400_BAD_REQUEST )
332
-
347
+ row = None
333
348
with connection .cursor () as cursor :
334
- cursor .execute ("SELECT coupon_code from applied_coupon WHERE user_id = " \
335
- + str (user .id )\
336
- + " AND coupon_code = '" \
337
- + coupon_request_body ['coupon_code' ]\
338
- + "'" )
339
- row = cursor .fetchall ()
349
+ try :
350
+ cursor .execute ("SELECT coupon_code from applied_coupon WHERE user_id = " \
351
+ + str (user .id )\
352
+ + " AND coupon_code = '" \
353
+ + coupon_request_body ['coupon_code' ]\
354
+ + "'" )
355
+ row = cursor .fetchall ()
356
+ except Exception as e :
357
+ log_error (request .path , request .data , 500 , e )
358
+ return Response (
359
+ {'message' : e },
360
+ status = status .HTTP_500_INTERNAL_SERVER_ERROR
361
+ )
340
362
341
363
if row and row != None :
342
364
return Response (
0 commit comments