def get(self, user_id): args = parser.parse_args() response = {} # Check aruments are valid or not error_mssgs = check_valid_request(args, ops='get') # log_info(error_mssgs) # Error for invalid arguments if error_mssgs: return {'status': 'fail', 'msg': error_mssgs}, 400 try: content, status_code = (Cart.decode_token( token=args['Authorization'])) if content['status'] == 'success': if user_id != content['data']['user_id']: response['status'] = 'fail' response['msg'] = 'Invalid Token' return response, 403 cart = CartDB.objects(user_id=user_id).first() if cart is None: response['status'] = 'success' response['msg'] = 'Empty' response['data'] = [] return response, 200 products = get_cart_json(cart) products_details = Cart.get_products_details(products) response = Cart.create_json_body(products, products_details) return response, 200 elif content['status'] == 'fail': return content, status_code except KeyError as e: response['status'] = 'fail' response['msg'] = str(e) + ' KeyError' response['data'] = [] return response, 400 except ValidationError as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 400 except Exception as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 500
def delete(self, user_id, product_id, operation): args = parser.parse_args() response = {} if operation != 'delete': return {'status': 'fail', 'msg': 'Invalid endpoint'}, 400 # Check aruments are valid or not error_mssgs = check_valid_request(args, ops='delete') # log_info(error_mssgs) # Error for invalid arguments if error_mssgs: return {'status': 'fail', 'msg': error_mssgs}, 400 try: content, status_code = (UpdateCart.decode_token( token=args['Authorization'])) if content['status'] == 'success': if user_id != content['data']['user_id']: response['status'] = 'fail' response['msg'] = 'Invalid Token' return response, 403 # Authorized buyer cart = CartDB.objects(user_id=user_id).first() if cart is None: response['status'] = 'fail' response['msg'] = 'Empty' response['data'] = [] return response, 403 for product in cart.products: if product_id == product.product_id: # Select specified product cart.products.remove(product) if len(cart.products) == 0: # If specified product count is 0 cart.delete() else: cart.save() break products = get_cart_json(cart) products_details = UpdateCart.get_products_details(products) # log_info(products_details) response = UpdateCart.create_json_body(products, products_details) return response, 200 elif content['status'] == 'fail': return content, status_code except KeyError as e: response['status'] = 'fail' response['msg'] = str(e) + ' KeyError' response['data'] = [] return response, 400 except ValidationError as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 400 except Exception as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 500
def delete(self, user_id): args = parser.parse_args() response = {} # Check aruments are valid or not error_mssgs = check_valid_request(args, ops='delete') # log_info(error_mssgs) # Error for invalid arguments if error_mssgs: return {'status': 'fail', 'msg': error_mssgs}, 400 try: content, status_code = (UpdateCart.decode_token( token=args['Authorization'])) if content['status'] == 'success': if user_id != content['data']['user_id']: response['status'] = 'fail' response['msg'] = 'Invalid Token' return response, 403 # Authorized buyer cart = CartDB.objects(user_id=user_id).first() if cart is None: response['status'] = 'fail' response['msg'] = 'Empty' response['data'] = [] return response, 403 cart.delete() response['status'] = 'success' response['msg'] = 'Products deleted from cart' response['data'] = [] return response, 200 elif content['status'] == 'fail': return content, status_code except KeyError as e: response['status'] = 'fail' response['msg'] = str(e) + ' KeyError' response['data'] = [] return response, 400 except ValidationError as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 400 except Exception as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 500