def put(self, customer_id): """ Update a single Customer """ app.logger.info('Updating a Customer with id [{}]'.format(customer_id)) content_type = request.headers.get('content_type') if not content_type or content_type != 'application/json': abort(status.HTTP_400_BAD_REQUEST, "No Content-Type set") customer = Customer.find(customer_id) if not customer: abort(status.HTTP_404_NOT_FOUND, "Customer with id '{}' was not found.".format(customer_id)) customer_info = request.get_json() customer_info.pop("_id", None) try: customer.deserialize(customer_info) except DataValidationError as error: raise BadRequest(str(error)) customer._id = customer_id customer.save() message = customer.serialize() return_code = status.HTTP_200_OK return message, return_code
def test_find_customer(self): """ Find a Customer by ID """ customer = Customer("Hey", "Jude") customer.save() time.sleep(WAIT_SECONDS) saved_customer = Customer.find(customer._id) self.assertIsNot(customer, None) self.assertEqual(customer._id, saved_customer._id) self.assertEqual(customer.first_name, "Hey")
def delete(self, customer_id): """ Delete a Customer """ app.logger.info('Deleting a Customer with id [{}]'.format(customer_id)) customer = Customer.find(customer_id) if customer: customer.delete() return '', status.HTTP_204_NO_CONTENT
def put(self, customer_id): """ Unsubscribe a Customer """ customer = Customer.find(customer_id) if not customer: abort(status.HTTP_404_NOT_FOUND, "Customer with id '{}' was not found.".format(customer_id)) customer.subscribed = False customer.save() return customer.serialize(), status.HTTP_200_OK
def get(self, customer_id): """ Retrieve a single Customer Address This endpoint will return a Customer based on it's id """ customer = Customer.find(customer_id) if not customer: abort(status.HTTP_404_NOT_FOUND, "Customer with id '{}' was not found.".format(customer_id)) return customer.serialize()["address"], status.HTTP_200_OK
def delete(self, customer_id): """ Delete a Customer This endpoint will delete a Customer based the id specified in the path """ app.logger.info('Request to Delete a customer with id [%s]', customer_id) customer = Customer.find(customer_id) if customer: customer.delete() return '', status.HTTP_204_NO_CONTENT
def get(self, user_id): """ Retrieve a single customer This endpoint will return a Customer based on user_id """ app.logger.info('Request for customer with user_id: %s', user_id) cust = Customer.find(user_id) result = [customer.serialize() for customer in cust] if len(result) == 0: api.abort(status.HTTP_404_NOT_FOUND, "Customer with id '{}' was not found".format(user_id)) return result, status.HTTP_200_OK
def delete(self, user_id): """ Delete a Customer This endpoint will delete a Customer based the id specified in the path """ app.logger.info('Request to delete customer with user_id: %s', user_id) customer = Customer.find(user_id) if customer: cust = customer[0] cust.delete() return '', status.HTTP_204_NO_CONTENT
def get(self, customer_id): """ Retrieve a single Customer This endpoint will return a Customer based on it's id """ app.logger.info("Request to Retrieve a customer with id [%s]", customer_id) customer = Customer.find(customer_id) if not customer: raise NotFound( "404 Not Found: Customer with the id was not found.") return customer.serialize(), status.HTTP_200_OK
def get(self, customer_id): """ Retrieve a single Customer This endpoint will return a Customer based on it's id """ app.logger.info("Request to Retrieve a customer with id [%s]", customer_id) customer = Customer.find(customer_id) if not customer: abort(status.HTTP_404_NOT_FOUND, "Customer WAS NOT FOUND ") return customer.serialize(), status.HTTP_200_OK
def put(self, customer_id): """ Suspend a Customer This endpoint will suspend a customer based on its ID """ app.logger.info("Request to suspend customer with id: %s", customer_id) customer = Customer.find(customer_id) if not customer: raise NotFound( "Cus...tomer with id '{}' was not found.".format(customer_id)) customer.active = False customer.update() app.logger.info("Customer with ID [%s] suspended.", customer.id) return customer.serialize(), status.HTTP_200_OK
def put(self, user_id): """ Activate a Customer This endpoint will activate a Customer """ app.logger.info('Request to activate customer with id: %s', user_id) customers = Customer.find(user_id) if customers.count() == 0: api.abort(status.HTTP_404_NOT_FOUND, "Customer with id '{}' was not found".format(user_id)) cust = customers[0] cust.user_id = user_id cust.active = True cust.save() return make_response(jsonify(cust.serialize()), status.HTTP_200_OK)
def get(self, customer_id): """ Retrieve a single Customer """ app.logger.info('Finding a Customer with id [{}]'.format(customer_id)) customer = Customer.find(customer_id) if customer: message = customer.serialize() return_code = status.HTTP_200_OK else: message = { 'error': 'Customer with id: %s was not found' % str(customer_id) } return_code = status.HTTP_404_NOT_FOUND return message, return_code
def put(self, user_id): """ Update a Customer This endpoint will update a Customer based the body that is posted """ app.logger.info('Request to update customer with id: %s', user_id) check_content_type('application/json') customers = Customer.find(user_id) if customers.count() == 0: api.abort(status.HTTP_404_NOT_FOUND, "Customer with id '{}' was not found".format(user_id)) cust = customers[0] cust.deserialize(request.get_json()) cust.user_id = user_id cust.save() return cust.serialize(), status.HTTP_200_OK
def put(self, customer_id): """ Diable a Customer's active attributes in the database """ app.logger.info('Disabling a Customer with id [{}]'.format(customer_id)) content_type = request.headers.get('Content-Type') if not content_type or content_type != 'application/json': abort(status.HTTP_400_BAD_REQUEST, "No Content-Type set") customer = Customer.find(customer_id) if not customer: abort(status.HTTP_404_NOT_FOUND, "Customer with id '{}' was not found.".format(customer_id)) customer.active = "False" customer.save() message = customer.serialize() return_code = status.HTTP_200_OK return message, return_code
def put(self, customer_id): """ Update a Customer This endpoint will update a Customer based the body that is posted """ app.logger.info('Request to Update a customer with id [%s]', customer_id) customer = Customer.find(customer_id) if not customer: api.abort( status.HTTP_404_NOT_FOUND, "Customer with id '{}' was not found.".format(customer_id)) app.logger.debug('Payload = %s', api.payload) data = api.payload customer.deserialize(data) customer.id = customer_id customer.save() return customer.serialize(), status.HTTP_200_OK
def test_find_customer(self): """ Find a Customer by ID """ customer = Customer( id=1, first_name="bye", last_name="yoyoyo", email="*****@*****.**", address="456 7th street, New York, NY, 10001", active=True, ) db.session.add(customer) result = Customer.find(customer.id) self.assertIsNot(result, None) self.assertEqual(result.id, customer.id) self.assertEqual(result.first_name, "bye") self.assertEqual(result.last_name, "yoyoyo") self.assertEqual(result.email, "*****@*****.**") self.assertEqual(result.address, "456 7th street, New York, NY, 10001") self.assertEqual(result.active, True)
def put(self, customer_id): """ Update a Customer This endpoint will update a Customer based the body that is posted """ app.logger.info('Request to Update a customer with id [%s]', customer_id) #check_content_type('application/json') customer = Customer.find(customer_id) if not customer: abort(status.HTTP_404_NOT_FOUND, "Customer with id '{}' was not found.".format(customer_id)) payload = request.get_json() try: customer.deserialize(payload) except DataValidationError as error: raise BadRequest(str(error)) customer.id = customer_id customer.save() return customer.serialize(), status.HTTP_200_OK
def test_find_customer(self): """ Find a Customer by ID """ customer = Customer(firstname="Sarah", lastname="Sally", email="*****@*****.**", subscribed=False, address1="124 Main St", address2="1E", city="New York", country="USA", province="NY", zip="12310").save() new_customer = Customer(firstname="John", lastname="Doe", email="*****@*****.**", subscribed=False, address1="123 Main St", address2="1B", city="New York", country="USA", province="NY", zip="12310") new_customer.save() customer = Customer.find(new_customer.id) self.assertIsNot(customer, None) self.assertEqual(customer.id, new_customer.id) self.assertEqual(customer.firstname, "John") self.assertEqual(customer.lastname, "Doe") self.assertEqual(customer.email, "*****@*****.**") self.assertEqual(customer.subscribed, False) self.assertEqual(customer.address1, "123 Main St") self.assertEqual(customer.address2, "1B") self.assertEqual(customer.city, "New York") self.assertEqual(customer.province, "NY") self.assertEqual(customer.country, "USA") self.assertEqual(customer.zip, "12310")
def test_customer_not_found(self): """ Test for a Customer that doesn't exist """ customer = Customer.find('this_is_uuid') time.sleep(WAIT_SECONDS) self.assertIs(customer, None)
def test_find_customer(self): """ Find active/inactive customers with filter """ active_customer = Customer(first_name="Peter", last_name="Parker", user_id="pparker", password="******", active=True) active_customer.save() inactive_customer = Customer(first_name="Peter.B.", last_name="Parker", user_id="pbparker", password="******", active=False) inactive_customer.save() """ Find active customers with filter """ result_active_filter = Customer.find(active_customer.user_id) self.assertEqual(result_active_filter[0].user_id, active_customer.user_id) """ Find active customers with filter """ result_active_no_filter = Customer.find(active_customer.user_id) self.assertEqual(result_active_no_filter[0].user_id, active_customer.user_id) """ Find inactive customers with filter """ result_inactive_filter = Customer.find(inactive_customer.user_id) self.assertEqual(result_inactive_filter[0].user_id, inactive_customer.user_id) """ Find inactive customers with filter """ result_inactive_no_filter = Customer.find(inactive_customer.user_id) self.assertEqual(result_inactive_no_filter[0].user_id, inactive_customer.user_id) def test_queries(self): """ Find active/inactive customers with filter """ cust = Customer(first_name="Peter", last_name="Parker", user_id="pparker", password="******", active=True) cust.save() addr = Address(apartment="1R", city="Chicago", state="Illinois", street="6721 5th Ave", zip_code="10030", customer_id=cust.customer_id) addr.save() cust.address_id = addr.id cust.save() """ Find by first name """ cust_fname = Customer.find_by_first_name(cust.first_name) self.assertEqual(cust_fname[0].customer_id, cust.customer_id) """ Find by last name """ cust_lname = Customer.find_by_last_name(cust.last_name) self.assertEqual(cust_lname[0].customer_id, cust.customer_id) """ Find by status """ cust_active = Customer.find_by_status(False) self.assertEqual(len(cust_active), 0) """ Find by city """ cust_city = Address.find_by_city(cust.city) self.assertEqual(cust_city[0].customer_id, cust.customer_id) """ Find by state """ cust_state = Address.find_by_state(cust.state) self.assertEqual(cust_state[0].customer_id, cust.customer_id) """ Find by zip code """ cust_zip = Address.find_zip(cust.zip_code) self.assertEqual(cust_zip[0].customer_id, cust.customer_id)