def put(self): ''' Update the Customer profile TODO: when this get merged and available on pypi : https://github.com/wtforms/wtforms/pull/147/files Sample:: form.populate_obj(g.customer.data, partial=False) Also, consider:: form.patch_data Returns the Customer profile ''' form = ProfileForm.from_json(request.get_json()) if not form.validate_on_submit(): return jsonify(errors=form.errors) form.populate_obj(g.customer.data) CustomerService.update_customer(g.customer) return jsonify(customer=g.customer.json)
def put(self, customer_id): ''' Update an existing customer. It is not possible to update the email address or the password using this service. TODO: Use patch when wtf-forms will be ready :param customer_id: The id of the customer to update Returns a json representation of a customer. ''' form = CustomerForm.from_json(request.get_json()) if not form.validate_on_submit(): return jsonify(errors=form.errors) customer = CustomerService.get_customer_by_id(customer_id) if customer is None: return jsonify(msg="Could not find customer") customer.data.cellphone = form.cellphone.data customer.data.first_name = form.first_name.data customer.data.last_name = form.last_name.data customer.data.newsletter = form.newsletter.data CustomerService.update_customer(customer) return jsonify(customer=customer.json)
def get(self): ''' Support pagination style: 'page' json parameter. Returns a list of customers and pagination data. ''' form = CustomerListForm.from_json(request.get_json()) if not form.validate(): return jsonify(errors=form.errors) page = form.page.data total = CustomerService.get_count_customers() total_pages = ceil(total / CustomerService.RESULTS_PER_PAGE) start = page * CustomerService.RESULTS_PER_PAGE stop = start + CustomerService.RESULTS_PER_PAGE raw_customers = CustomerService.get_customers( start=start, stop=stop ) return jsonify( current_page=page, total_pages=int(total_pages), customers=[customer.json for customer in raw_customers] )
def post(self): ''' Create a new customer. Returns its ID. ''' form = RegistrationForm.from_json(request.get_json()) if not form.validate_on_submit(): return jsonify(errors=form.errors) customer = Customer(email=form.email.data, password=form.password.data) CustomerService.add_customer(customer) return jsonify(id=customer.id)
def patch(self): ''' Change the password of a customer, requires the old password. Returns a success message. ''' form = PasswordForm.from_json(request.get_json()) #Here we are not using form.validate_on_submit because it # will only work for PUT and POST request methods. if not form.validate() or not request.method == "PATCH": return jsonify(errors=form.errors) g.customer.set_password(form.password.data) CustomerService.update_customer(g.customer) return jsonify(msg="OK")
def post(self): ''' Create a new customer. Returns its ID. ''' form = RegistrationForm.from_json(request.get_json()) if not form.validate_on_submit(): return jsonify(errors=form.errors) customer = Customer( email=form.email.data, password=form.password.data ) CustomerService.add_customer(customer) return jsonify(id=customer.id)
def delete(self, customer_id): ''' Delete a customer by its ID. :param customer_id: The id of the customer to delete Returns True if the customer has been found and deleted. ''' result = CustomerService.delete_customer_by_id(customer_id) return jsonify(result=result)
def post(self): ''' Create a new customer Returns a json representation of a customer. ''' form = CreateCustomerForm.from_json(request.get_json()) if not form.validate_on_submit(): return jsonify(errors=form.errors) customer = Customer(email=form.email.data, password=AdminCustomer._generate_password()) customer.data.cellphone = form.cellphone.data customer.data.first_name = form.first_name.data customer.data.last_name = form.last_name.data customer.data.newsletter = form.newsletter.data CustomerService.add_customer(customer) return jsonify(customer=customer.json)
def validate_email(self, field): ''' Custom validator for the email, make sure that it is not already used. :param field: Field object, the email string is in field.data ''' customer = CustomerService.get_customer_by_email(field.data) if customer is not None: raise ValidationError('Email address already taken.')
def get(self, customer_id): ''' Returns one customer :param customer_id: The ID of the customer to retrieve. ''' customer = CustomerService.get_customer_by_id(customer_id) if customer is None: return jsonify(msg="Could not find customer") return jsonify(customer=customer.json)
def post(self): ''' Create a new customer Returns a json representation of a customer. ''' form = CreateCustomerForm.from_json(request.get_json()) if not form.validate_on_submit(): return jsonify(errors=form.errors) customer = Customer( email=form.email.data, password=AdminCustomer._generate_password() ) customer.data.cellphone = form.cellphone.data customer.data.first_name = form.first_name.data customer.data.last_name = form.last_name.data customer.data.newsletter = form.newsletter.data CustomerService.add_customer(customer) return jsonify(customer=customer.json)
def delete(self, customer_id): ''' Delete a customer by its ID. :param customer_id: The id of the customer to delete Returns True if the customer has been found and deleted. ''' result = CustomerService.delete_customer_by_id(customer_id) return jsonify( result=result )
def get(self): ''' Support pagination style: 'page' json parameter. Returns a list of customers and pagination data. ''' form = CustomerListForm.from_json(request.get_json()) if not form.validate(): return jsonify(errors=form.errors) page = form.page.data total = CustomerService.get_count_customers() total_pages = ceil(total / CustomerService.RESULTS_PER_PAGE) start = page * CustomerService.RESULTS_PER_PAGE stop = start + CustomerService.RESULTS_PER_PAGE raw_customers = CustomerService.get_customers(start=start, stop=stop) return jsonify(current_page=page, total_pages=int(total_pages), customers=[customer.json for customer in raw_customers])
def validate_email(self, field): ''' Custom validator for the email, make sure that it is not already used. :param field: Field object, the email string is in field.data ''' customer = CustomerService.get_customer_by_email(field.data) if customer is not None: raise ValidationError( 'Email address already taken.' )
def get(self, customer_id): ''' Returns one customer :param customer_id: The ID of the customer to retrieve. ''' customer = CustomerService.get_customer_by_id(customer_id) if customer is None: return jsonify( msg="Could not find customer" ) return jsonify( customer=customer.json )
def check_auth(username, password): ''' This function is called to check if a username / password combination is valid. :param username: The email address used in the basic auth field :param password: The password used in the basic auth field ''' customer = CustomerService.get_customer_by_email(username) if customer is not None: if customer.check_password(password): g.customer = customer return True return False