def another_brand(init_db): """ Another brand fixture """ return Brand( name='puma', description='' )
def new_brand(init_db): """ New brand fixture """ return Brand( name='nike', description='American company' )
def post(self): """ Endpoint to create the brand """ request_data = request.get_json() BrandValidators.validate(request_data) request_data = request_data_strip(request_data) request_data['name'] = request_data['name'].lower() new_brand = Brand(**request_data) new_brand.save() brand_schema = BrandSchema() brand_data = brand_schema.dump(new_brand) success_response['message'] = 'Brand successfully created' success_response['data'] = {'brand': brand_data} return success_response, 201
def get(self, brand_id): """" Endpoint to get a single brand """ brand_schema = BrandSchema() brand = brand_schema.dump(Brand.find_by_id(brand_id)) if not brand: error_response['message'] = 'Brand not found' return error_response, 404 success_response['message'] = 'Brand successfully fetched' success_response['data'] = {'brand': brand} return success_response, 200
def delete(self, brand_id): """" Endpoint to delete a brand """ brand_schema = BrandSchema() brand = Brand.find_by_id(brand_id) if not brand: error_response['message'] = 'Brand not found' return error_response, 404 brand.delete() success_response['message'] = 'Brand successfully deleted' success_response['data'] = {'brand': brand_schema.dump(brand)} return success_response, 200
def validate_brand(cls, brand_id): """ Checks if the provided brand ID is valid Args: brand_id (int): brand ID Raises: (ValidationError): raise an exception if the brand ID doesn't exist in the database """ if brand_id is not None: if not is_positive_integer(brand_id): raise_validation_error( 'The brand ID should be a positive integer') if not Brand.find_by_id(brand_id): raise_validation_error('The brand ID provided doesn\'t exist')
def put(self, brand_id): """" Endpoint to update brand """ brand_schema = BrandSchema() brand = Brand.find_by_id(brand_id) if not brand: error_response['message'] = 'Brand not found' return error_response, 404 request_data = request.get_json() BrandValidators.validate(request_data, brand_id=brand_id) request_data = request_data_strip(request_data) request_data['name'] = request_data['name'].lower() brand.update(request_data) success_response['message'] = 'Brand successfully updated' success_response['data'] = {'brand': brand_schema.dump(brand)} return success_response, 200