def put(self, request, prodId, optionId, format=None): try: querySet = ProductOptions.objects.filter(id=optionId, productId=prodId) if not querySet: raise Exception(error_code(5)) data = request.data is_valid = validateObject(validKeys=product_options_keys, data=data) if not is_valid: raise Exception(error_code(2)) prod = querySet[0] prod.name = data.get('name') prod.description = data.get('description') prod.save() return Response({'Message': success_code(2)}, status.HTTP_200_OK, content_type='application/json') except Exception as e: return Response({'Error': str(e)}, status.HTTP_400_BAD_REQUEST, content_type='application/json')
def post(self, request, format=None): try: data = request.data # validate the inputs is_valid = validateObject(validKeys=product_keys, data=data, check_all=True) if not is_valid: raise Exception(error_code(1)) data['id'] = str(uuid.uuid4()) prod = Products(id=data.get('id'), name=data.get('name'), description=data.get('description'), price=data.get('price'), deliveryPrice=data.get('deliveryPrice')) prod.save() return Response({'Message': success_code(1)}, status.HTTP_201_CREATED, content_type='application/json') except Exception as e: return Response({'Error': str(e)}, status.HTTP_400_BAD_REQUEST, content_type='application/json')
def delete(self, request, id, format=None): try: isdeleted = Products.objects.filter(id=id).delete() if isdeleted[0] == 0: raise Exception(error_code(3)) return Response({'Message': success_code(3)}, status.HTTP_200_OK, content_type='application/json') except Exception as e: return Response({'Error': str(e)}, status.HTTP_400_BAD_REQUEST, content_type='application/json')
def get(self, request, id, format=None): try: querySet = ProductOptions.objects.filter(productId=id) if not querySet: raise Exception(error_code(7)) response_items = loadProductOptionDetails(self, querySet) return Response(response_items, status.HTTP_200_OK, content_type='application/json') except Exception as e: return Response({'Error': str(e)}, status.HTTP_400_BAD_REQUEST, content_type='application/json')
def get(self, request, format=None): try: # When url consists more than one query parameters if len(request.GET) > 1: raise Exception(error_code(6)) if not request.GET: querySet = Products.objects.filter() else: resquested_name = request.GET['name'] querySet = Products.objects.filter( name__contains=resquested_name) if not querySet: raise Exception(error_code(8)) response_items = loadProductDetails(self, querySet) return Response(response_items, status.HTTP_200_OK, content_type='application/json') except Exception as e: return Response({'Error': str(e)}, status.HTTP_400_BAD_REQUEST, content_type='application/json')