def put(self, _id): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'Admin privilege required.'}, 401 parser = reqparse.RequestParser() parser.add_argument('food_id', type=list, required=True, location='json', help="every order need a list of food id!") data = Order.parser.parse_args() order = OrderModel.find_by_id(_id) if order: foods = [ FoodModel.find_by_id(food).json() for food in data['food_id'] ] total_price = sum([food['price'] for food in foods]) order.total_price = total_price order.updated_at = datetime.now() else: return {'message': 'Order not found'}, 404 try: order.save_to_db() except: return {"message": "An error occurred inserting the food."}, 500 return order.json(), 201
def post(self): parser = reqparse.RequestParser() parser.add_argument('file',type=werkzeug.datastructures.FileStorage, location='files') payload = parser.parse_args() food_id = request.form.to_dict().get("food", "") if (payload['file'] == ""): return { 'status' : 'error', 'message' : 'File parameter cannot be empty' } elif (food_id == ""): return { 'status' : 'error', 'message' : 'Food_Id parameter cannot be empty' } food = FoodModel.find_by_id(food_id) if (food): photo = payload['file'] if (photo): file_extension = os.path.splitext(photo.filename)[1] filename = secure_filename(f'{uuid.uuid4()}{file_extension}') file_location = os.path.join(UPLOAD_FOLDER,filename) photo.save(file_location) food.photo = file_location food.save() return { 'status' : 'ok', 'message' : f'File {filename} uploaded' }, 201 else: return { 'status' : 'error', 'message' : f'Error processing the upload file' }, 403 else: return { 'status' : 'error', 'message' : f'Food {food_id} not found' }, 404
def post(self): user_id = get_jwt_identity() data = Order.parser.parse_args() foods = [FoodModel.find_by_id(food).json() for food in data['food_id']] total_price = sum([food['price'] for food in foods]) status = 'submitted.' created_at = datetime.now() order = OrderModel(user_id, total_price, status, created_at, created_at) try: order.save_to_db() except: return {"message": "An error occurred inserting the food."}, 500 return order.json(), 201
def get(self, food_id): food = FoodModel.find_by_id(food_id) if food: if food.photo: return send_from_directory(UPLOAD_FOLDER, os.path.basename(food.photo), as_attachment=True) else: return { 'status' : 'error', 'message' : f'Food {food_id} does not have a photo' }, 404 else: return { 'status' : 'error', 'message' : f'Food {food_id} not found' }, 404
def get(food_id): food = FoodModel.find_by_id(food_id) if not food: return {'message': strings.error_food_not_found}, 404 return food.json(), 200