def post(self) -> Response: """ POST request method for creating a new meal document. JSON Web Token is required. Admin-level access is required. """ # check if user is admin is_admin = Users.objects.get(id=get_jwt_identity()).access.admin if is_admin: data = request.get_json() try: # validate the passed field values Meals(**data).validate() except (FieldDoesNotExist, TypeError, ValidationError): return wrong_value() try: new_meal = Meals(**data) new_meal.save() output = {'new_meal_id': str(new_meal.id)} except NotUniqueError: output = 'Meal with this name already exists.' except AttributeError: output = 'Could not get new meal\'s ID.' return jsonify({'result': output}) else: return forbidden()
def put(self, meal_id: str) -> Response: """ PUT request method for updating a meal document. JSON Web Token is required. Admin-level access is required. """ is_admin = Users.objects.get(id=get_jwt_identity()).access.admin if is_admin: data = request.get_json() try: # validate the passed field values Meals(**data).validate() except (FieldDoesNotExist, TypeError, ValidationError, NotUniqueError, DuplicateKeyError): return wrong_value() try: updated_meal = Meals.objects.get(id=meal_id) # if new value for the field is not given, don't change it updated_meal.update( set__name=data.get('name', updated_meal.name), set__description=data.get('description', updated_meal.description), set__price=data.get('price', updated_meal.price), set__image_url=data.get('image_url', updated_meal.image_url)) output = f'Successfully updated meal {meal_id}' return jsonify({'result': output}) except (DoesNotExist, ValidationError): output = f'No meal with id={meal_id}' return jsonify({'result': output}) except NotUniqueError: return wrong_value() else: return forbidden()
def get(self) -> Response: """ GET request method for retrieving all meals documents. JSON Web Token is required. """ output = Meals.objects() return jsonify({'result': output})
def get(self) -> Response: """ GET response method for all documents in meal collection. JSON Web Token is required. :return: JSON object """ output = Meals.objects() return jsonify({'result': output})
def delete(self, user_id: str) : authorized: bool = Users.objects.get(id=get_jwt_identity()).access.admin if authorized: output = Meals.objects(id=user_id).delete() return jsonify({'result': output}) else: return forbidden()
def put(self, meal_id: str) -> Response: """ PUT response method for updating a meal. JSON Web Token is required. Authorization is required: Access(admin=true) :return: JSON object """ data = request.get_json() put_user = Meals.objects(id=meal_id).update(**data) return jsonify({'result': put_user})
def post(self): authorized : bool = Users.objects.get(id=get_jwt_identity()).access.admin if authorized: data = request.get_json() post_user = Meals(**data).save() output = {'id': str(post_user.id)} return jsonify({'result': output}) else: return forbidden()
def delete(self, user_id: str) -> Response: """ DELETE response method for deleting single meal. JSON Web Token is required. Authorization is required: Access(admin=true) :return: JSON object """ authorized: bool = Meals.objects.get( id=get_jwt_identity()).access.admin if authorized: output = Meals.objects(id=user_id).delete() return jsonify({'result': output}) else: return forbidden()
def post(self) -> Response: """ POST response method for creating meal. JSON Web Token is required. Authorization is required: Access(admin=true) :return: JSON object """ authorized: bool = Meals.objects.get( id=get_jwt_identity()).access.admin if authorized: data = request.get_json() post_user = Meals(**data).save() output = {'id': str(post_user.id)} return jsonify({'result': output}) else: return forbidden()
def csv_to_meal(filepath: str = 'resources/meal_data.csv', delimiter: str = '\t'): """ Converts data in csv file to documents in meal collection. Uses @mongo wrapper to connect via mongoengine during execution. :param filepath: :param delimiter: :return: """ with open(filepath, 'r') as file: data = csv.DictReader(file, delimiter=delimiter) for datum in data: dish = Meals(**datum, __auto_convert=True).save() print( f"Added: {dish.name} | {dish.description} | {dish.price} => {dish.id}" )
def add_meal(): """ add meal BOOK-A-MEAL API Adding meal --- tags: - meals parameters: - in: body name: body schema: id: add_meal required: - meal_name - price - meal_type properties: meal_name: type: string description: meal_name default: frenchbeans price: type: integer description: price for meal default: 5000 meal_type: type: string description: meal category default: lunch responses: 400: description: 201: description: Meal Added schema: id: adding_meal_message properties: id: type: integer description: price for meal default: 3 meal_name: type: string description: meal_name default: frenchbeans price: type: integer description: price for meal default: 5000 meal_type: type: string description: meal category default: lunch created_at: type: string description: time created default: Fri, 04 May 2018 00:10:06 GMT updated_at: type: string description: time updated default: Fri, 04 May 2018 00:10:06 GMT """ """ Adding meal """ if not request.get_json() or 'meal_name' not in request.get_json()\ or 'price' not in request.get_json() or 'meal_type' not in request.get_json(): abort(400) meal_name = request.get_json().get('meal_name') price = request.get_json().get('price') meal_type = request.get_json().get('meal_type') if type(price) is not int: abort(400) meal_exists = Meals.query.filter_by(meal_name=meal_name).first() if meal_exists != None: return jsonify({'message': "Meal Already Exists"}), 400 meal = Meals(meal_name=meal_name, price=price, meal_type=meal_type) db.session.add(meal) db.session.commit() return jsonify({'message': 'Meal Successfully Added'}), 201
def put(self, meal_id: str) : data = request.get_json() put_user = Meals.objects(id=meal_id).update(**data) return jsonify({'result': put_user})
def get(self): output = Meals.objects() return jsonify({'result': output})