def on_post(self, req, resp, *args, **kwargs): schema = OrderSchema() data = json.loads(req.stream.read(req.content_length or 0)) data.pop('type', None) order, errors = schema.load(data) if errors: logger.error(errors) errors = [ format_error('missing-field', ' '.join(value), dict(field=key)) for key, value in errors.items() ] resp.status = falcon.HTTP_400 resp.body = json.dumps(format_errors(errors), ensure_ascii=False) else: schema.context['remove_fields'] = ['seed', 'password'] order.save() data, errors = schema.dump(order) resp.status = falcon.HTTP_200 resp.body = json.dumps(format_response(data), ensure_ascii=False)
def on_put(self, req, resp, order, *args, **kwargs): schema = OrderSchema() data = json.loads(req.stream.read(req.content_length or 0)) products = data.pop('products', None) order, errors = schema.load(data, partial=True, instance=order) if products: op_schema = OrderProductSchema() op, op_errors = op_schema.load(products, many=True, partial=True) else: op_errors = None op = None if errors: logger.error(errors) errors = [ format_error('missing-field', ' '.join(value), dict(field=key)) for key, value in errors.items() ] resp.status = falcon.HTTP_400 resp.body = json.dumps(format_errors(errors), ensure_ascii=False) elif op_errors: logger.error(op_errors) op_errors = [ format_error('missing-field', ' '.join(value), dict(field=key)) for key, value in op_errors.items() ] resp.status = falcon.HTTP_400 resp.body = json.dumps(format_errors(op_errors), ensure_ascii=False) else: if op: order.products = op schema.context['remove_fields'] = ['seed', 'password'] order.save() data, errors = schema.dump(order) resp.status = falcon.HTTP_200 resp.body = json.dumps(format_response(data), ensure_ascii=False)
def on_put(self, req, resp, product, *args, **kwargs): schema = ProductSchema() data = json.loads(req.stream.read(req.content_length or 0)) ingredients = data.pop('ingredients', None) product, errors = schema.load(data, partial=True, instance=product) pi_errors = None pi = None if ingredients: pi_schema = ProductIngredientSchema() pi, pi_errors = pi_schema.load(ingredients, many=True, partial=True) if errors: logger.error(errors) errors = [ format_error('missing-field', ' '.join(value), dict(field=key)) for key, value in errors.items() ] resp.status = falcon.HTTP_400 resp.body = json.dumps(format_errors(errors), ensure_ascii=False) elif pi_errors: logger.error(pi_errors) pi_errors = [ format_error('missing-field', ' '.join(value), dict(field=key)) for key, value in pi_errors.items() ] resp.status = falcon.HTTP_400 resp.body = json.dumps(format_errors(pi_errors), ensure_ascii=False) else: if pi: product.ingredients = pi product.save() data, errors = schema.dump(product) resp.status = falcon.HTTP_200 resp.body = json.dumps(format_response(data), ensure_ascii=False)
def on_put(self, req, resp, client, *args, **kwargs): data = json.loads(req.stream.read(req.content_length or 0)) schema = ClientSchema() client, errors = schema.load(data, instance=client, partial=True) if errors: errors = [ format_error('missing-field', ' '.join(value), dict(field=key)) for key, value in errors.items() ] resp.status = falcon.HTTP_400 resp.body = json.dumps(format_errors(errors), ensure_ascii=False) else: client.save() schema.context['remove_fields'] = ['seed', 'password'] data, errors = schema.dump(client) resp.status = falcon.HTTP_200 resp.body = json.dumps(format_response(data), ensure_ascii=False)
def on_post(self, req, resp, *args, **kwargs): schema = IngredientSchema() data = json.loads(req.stream.read(req.content_length or 0)) data.pop('type', None) ingredient, errors = schema.load(data) if errors: errors = [ format_error('missing-field', ' '.join(value), dict(field=key)) for key, value in errors.items() ] resp.status = falcon.HTTP_400 resp.body = json.dumps(format_errors(errors), ensure_ascii=False) else: ingredient.save() data, errors = schema.dump(ingredient) resp.status = falcon.HTTP_200 resp.body = json.dumps(format_response(data), ensure_ascii=False)
def on_post(self, req, resp): data = json.loads(req.stream.read(req.content_length or 0)) schema = ClientSchema() user, errors = schema.load(data) if errors: errors = [ format_error('missing-field', ' '.join(value), dict(field=key)) for key, value in errors.items() ] resp.status = falcon.HTTP_400 resp.body = json.dumps(format_errors(errors), ensure_ascii=False) else: user.set_password(user.password) user.save() schema.context['remove_fields'] = ['seed', 'password'] data, errors = schema.dump(user) resp.status = falcon.HTTP_200 resp.body = json.dumps(format_response(data), ensure_ascii=False)