def edit_customer(current_user, customer_id): """ Validate the customer Id. Also check for the name attribute in the json payload. If the name exists update the customer with the new name. :param current_user: Current User :param customer_id: Customer Id :return: Http Json response """ if request.content_type == 'application/json': data = request.get_json() name = data.get('name') if name: try: int(customer_id) except ValueError: return response('failed', 'Please provide a valid Customer Id', 400) user_customer = User.get_by_id( current_user.id).customers.filter_by(id=customer_id).first() if user_customer: user_customer.update(name) return response_for_created_customer(user_customer, 201) return response( 'failed', 'The Customer with Id ' + customer_id + ' does not exist', 404) return response( 'failed', 'No attribute or value was specified, nothing was changed', 400) return response('failed', 'Content-type must be json', 202)
def create_customerlist(current_user): """ Create a Customer from the sent json data. :param current_user: Current User :return: """ if request.content_type == 'application/json': data = request.get_json() name = data.get('name') if name: user_customer = Customer(name.lower(), current_user.id) user_customer.save() return response_for_created_customer(user_customer, 201) return response('failed', 'Missing name attribute', 400) return response('failed', 'Content-type must be json', 202)
def handle_400_errors(e): """ Return a custom response for 400 errors. :param e: :return: """ return response('failed', 'Bad Request', 400)
def handle_404_error(e): """ Return a custom message for 404 errors. :param e: :return: """ return response('failed', 'Customer resource cannot be found', 404)
def route_not_found(e): """ Return a custom 404 Http response message for missing or not found routes. :param e: Exception :return: Http Response """ return response('failed', 'Endpoint not found', 404)
def internal_server_error(e): """ Return a custom message for a 500 internal error :param e: Exception :return: """ return response('failed', 'Internal server error', 500)
def get_customer(current_user, customer_id): """ Return a user customer with the supplied user Id. :param current_user: User :param customer_id: Customer Id :return: """ try: int(customer_id) except ValueError: return response('failed', 'Please provide a valid Customer Id', 400) else: user_customer = User.get_by_id( current_user.id).customers.filter_by(id=customer_id).first() if user_customer: return response_for_user_customer(user_customer.json()) return response('failed', "Customer not found", 404)
def delete_customer(current_user, customer_id): """ Deleting a User Customer from the database if it exists. :param current_user: :param customer_id: :return: """ try: int(customer_id) except ValueError: return response('failed', 'Please provide a valid Customer Id', 400) user_customer = User.get_by_id( current_user.id).customers.filter_by(id=customer_id).first() if not user_customer: abort(404) user_customer.delete() return response('success', 'Customer Deleted successfully', 200)
def method_not_found(e): """ Custom response for methods not allowed for the requested URLs :param e: Exception :return: """ return response('failed', 'The method is not allowed for the requested URL', 405)