Пример #1
0
    def recipes():
        if request.method == "POST":
            data = request.get_json()
            recipe_name = data['name']
            recipe = Recipes.query.filter_by(name=recipe_name).first()

            if not recipe:
                new_recipe = Recipes(name=data['name'],
                                     prep_time=data['prep_time'],
                                     difficulty=data['difficulty'],
                                     vegetarian=data['vegetarian'],
                                     rating=data['rating'])
                new_recipe.rating_numbering = 1
                new_recipe.save()
                response = jsonify({
                    'id': new_recipe.id,
                    'name': new_recipe.name,
                    'prep_time': new_recipe.prep_time,
                    'difficulty': new_recipe.difficulty,
                    'vegetarian': new_recipe.vegetarian,
                    'rating': new_recipe.rating
                })
                response.status_code = 201
                return response
            else:
                response = jsonify({'message': 'recipie already exist'})
                response.status_code = 304
                return response
        else:
            # GET
            recipes = Recipes.get_all()
            results = []

            for recipe in recipes:
                obj = {
                    'id': recipe.id,
                    'name': recipe.name,
                    'prep_time': recipe.prep_time,
                    'difficulty': recipe.difficulty,
                    'vegetarian': recipe.vegetarian
                }
                results.append(obj)
            response = jsonify(results)
            response.status_code = 200
            return response
Пример #2
0
    def post(self, user_in_session, category_id):
        # This method will create and save a recipe
        """
       Create Recipe
       ---
       tags:
         - Recipes Endpoints
       parameters:
         - in: path
           name: category_id
           description: category_id
           type: integer
         - in: body
           name: Recipes details
           description: Create recipe by providing recipe name and description
           type: string
           required: true
           schema:
             id: create_recipes
             properties:
               recipe_name:
                 default: Ugali
               recipe_procedure:
                 default: Boil water; Put flour; mix the water and flour for five min; serve ugali.
       responses:
         201:
           description: Recipe created successfully
         409:
           description: Recipe exists!
         400:
           description: Invalid recipe name given
         404:
           description: Category not found
         422:
           description: Please fill all the fields
        """
        recipe_name = str(request.data.get('recipe_name', '')).strip()
        recipe_procedure = str(request.data.get('recipe_procedure',
                                                '')).strip()

        check_recipe_existence = Recipes.query.filter_by(
            users_id=user_in_session, category_id=category_id).all()
        # This checks whether the catgory specified has the a similar category name from the user.
        if not recipe_name or not recipe_procedure:
            return make_response(
                jsonify({'message': 'Please fill all the fields'})), 400

        if not re.search(self.regex_recipe_name, recipe_name):
            return make_response(
                jsonify({'message': 'Invalid recipe name given'})), 400

        for recipe_in_list in check_recipe_existence:
            recipe_name_in_list = recipe_in_list.recipe_name
            if recipe_name.upper() == recipe_name_in_list.upper():
                return make_response(
                    jsonify({'message': 'Recipe name exists!'})), 409

        recipes_save = Recipes(recipe_name=recipe_name,
                               recipe_description=recipe_procedure,
                               category_id=category_id,
                               users_id=user_in_session)
        recipes_save.save()
        # This saves the category after it passes all the conditions.

        return make_response(
            jsonify({'message': 'Recipe created successfully'})), 201
Пример #3
0
    def recipes(id):
        auth_header = request.headers.get('Authorization')
        if auth_header:
            access_token = auth_header.split(" ")[1]
        else:
            return {"Message": "Please provide an access token"}, 300
        category = RecipeCategory.query.filter_by(id=id).first()
        result = []
        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                page = int(request.args.get('page', 1))
                per_page = int(request.args.get('per_page', 20))
                if category:
                    if request.method == 'GET':
                        if category.recipes:
                            recipe_object = category.recipes.paginate(
                                page=page, per_page=per_page)
                            for recipe in recipe_object.items:
                                obj = {
                                    "id": recipe.id,
                                    "name": recipe.name,
                                    "Recipe": recipe.recipe,
                                    "Date Created": recipe.date_created,
                                    "Date Modified": recipe.date_modified
                                }
                                result.append(obj)
                            response = jsonify(
                                {
                                    'Next Page': recipe_object.next_num,
                                    'Prev Page': recipe_object.prev_num,
                                    'Has next': recipe_object.has_next,
                                    'Has previous': recipe_object.has_prev
                                }, result)
                            response.status_code = 200
                            return response
                        else:
                            response = jsonify(
                                {"Message": "No recipes added yet"})
                            response.status_code = 404
                            return response

                    elif request.method == 'POST':
                        name = request.data.get('name', '')
                        recipe = request.data.get('recipe', '')
                        if name and recipe:
                            the_recipes = Recipes(name=name,
                                                  recipe=recipe,
                                                  belonging_to=category)
                            the_recipes.save()
                            for recipe in category.recipes.all():
                                obj = {
                                    "id": recipe.id,
                                    "name": recipe.name,
                                    "Recipe": recipe.recipe,
                                    "Date created": recipe.date_created,
                                    "Date modified": recipe.date_modified
                                }
                                result.append(obj)
                                response = jsonify(result)
                                response.status_code = 201
                                return response
                        else:
                            return {
                                "Message": "Please use keys name and recipe"
                            }, 203
                else:
                    return {"Message": "Category does not exist"}, 405

            else:
                #  Token is not legit so return the error message
                message = user_id
                response = jsonify({"Message": message})
                response.status_code = 401
                return response
Пример #4
0
    def post(self, current_user, id):
        """Method to add a recipe in a category
        ---
        tags:
            - Recipes
        produces:
            - application/json
        security:
          - TokenHeader: []

        parameters:
            - in: body
              name: Recipe Register
              required: true
              type: string
              schema:
                id: recipes
                properties:
                  recipe_name:
                    type: string
                    default: Panckakes
                  recipe_ingredients:
                    type: long
                    default: Milk, Flour
                  recipe_methods:
                    type: long
                    default: Cook in a pan till ready
            - in: path
              name: id
              required: true
              type: integer
        responses:
          200:
            schema:
              id: recipes
          400:
            description: Bad Request on recipe creation route
          201:
            description: Success in creating a recipe
          404:
            description: Category does not exist
        """
        category_id = id
        if category_id:
            try:
                recipe_name = str(request.data.get('recipe_name', ''))
                recipe_ingredients = request.data.get('recipe_ingredients', '')
                recipe_methods = request.data.get('recipe_methods', '')

                try:
                    recipe_validation(recipe_name, recipe_methods, recipe_ingredients)
                except Exception as e:
                    response = {'message': str(e)}
                    return make_response(jsonify(response)), 400

                recipe = Recipes(recipe_name=recipe_name, recipe_ingredients=recipe_ingredients,
                                 recipe_methods=recipe_methods, category_id=category_id, created_by=current_user.id)

                recipe_details = Recipes.query.filter_by(
                    category_id=category_id, recipe_name=recipe_name, created_by=current_user.id).first()

                if recipe_details:
                    response = {'message': 'Recipe name exists',
                                'status': 'fail'}
                    return make_response(jsonify(response)), 400
                recipe.save()
                response = {'id': recipe.id,
                            'recipe_name': recipe.recipe_name,
                            'recipe_ingredients': recipe.recipe_ingredients,
                            'recipe_methods': recipe.recipe_methods,
                            'category_id': recipe.category_id,
                            'date_created': recipe.date_created,
                            'date_modified': recipe.date_modified
                           }
                response = make_response(jsonify(response)), 201
                return response

            except Exception:
                response = {'message': 'Category does not exist',
                            'status': 'error'}
                return make_response(jsonify(response)), 404