Beispiel #1
0
    def put(current_user, self, id):
        """
                      Edit and update a recipe with the specified id
                      ---
                      tags:
                        - recipes
                      parameters:
                        - in: path
                          name: id
                          required: true
                          description: The ID of the recipe to edit
                          type: string
                        - in: body
                          name: recipe
                          required: true
                          description: The title and body of the recipe
                          type: string
                          schema:
                            id: recipe
                            properties:
                                title:
                                    type: string
                                    default: Meat soup
                                body:
                                    type: string
                                    default: Pour, mix, cook
                      security:
                         - TokenHeader: []
                      responses:
                        200:
                          description: A single recipe successfully edited
                          schema:
                            id: recipe
                            properties:
                                title:
                                    type: string
                                    default: Meat soup
                                body:
                                    type: string
                                    default: Pour, mix, cook
                      """

        recipe = Recipe.query.filter_by(id=id, user_id=current_user.id).first()
        if not recipe:
            return {"Error": "A recipe with that Id does not exist"}, 404
        recipe_dict = request.get_json(force=True)
        if 'title' in recipe_dict:
            recipe_title = recipe_dict['title'].strip()
            error, validate_title = recipe.validate_recipe(ctx=recipe_title)
            if validate_title:
                if Recipe.is_unique(id=id,
                                    title=recipe_title,
                                    user_id=current_user.id):
                    recipe.title = recipe_title
                else:
                    abort(status.HTTP_409_CONFLICT,
                          'A recipe with the same title already exists')
            else:
                abort(400, {"error": error})
        if 'body' in recipe_dict:
            recipe_body = recipe_dict['body'].strip()
            body_errors, validate_body = recipe.validate_recipe(
                ctx=recipe_body)
            if validate_body:
                recipe.body = recipe_body
            else:
                abort(400, body_errors)

        dumped_recipe, dumped_errors = recipe_schema.dump(recipe)
        if dumped_errors:
            abort(status.HTTP_400_BAD_REQUEST, dumped_errors)

        recipe.update()
        return {"message": "Recipe successfully edited"}, 200
Beispiel #2
0
    def post(current_user, self, category_id):
        """
        Create a recipe
        ---
        tags:
          - recipes
        parameters:
          - in: path
            name: category_id
            required: true
            description: Category Id
            type: integer
          - in: body
            name: recipe
            required: true
            description: The title of the recipe
            type: string
            schema:
              id: recipe
              properties:
                title:
                  type: string
                  default: Meat soup
                body:
                  type: string
                  default: This is the process of making meat soup
        security:
           - TokenHeader: []
        responses:
          200:
            description: Create a recipe
            schema:
              id: recipe
              properties:
                title:
                  type: string
                  default: Meat soup
                body:
                  type: string
                  default: This is the process of making meat soup
        """

        request_dict = request.get_json()

        if not request_dict:
            abort(status.HTTP_400_BAD_REQUEST, "All fields are required")
        errors = recipe_schema.validate(request_dict)
        if errors:
            abort(status.HTTP_400_BAD_REQUEST, errors)
        recipe_title = request_dict['title'].title()
        recipe_body = request_dict['body'].strip()
        if not Recipe.is_unique(
                id=0, title=recipe_title, user_id=current_user.id):
            abort(status.HTTP_409_CONFLICT,
                  'A recipe with the same title already exists')
        error, validated_title = Recipe.validate_recipe(ctx=recipe_title)
        if not validated_title:
            abort(400, error)
        error_body, validated_body = Recipe.validate_recipe(ctx=recipe_body)
        if not validated_body:
            abort(400, error_body)
        category = Category.query.filter_by(id=category_id,
                                            user_id=current_user.id).first()
        if category:
            recipe = Recipe(title=recipe_title,
                            body=recipe_body,
                            category_id=category.id,
                            user=current_user)
            recipe.add(recipe)
            response = "Recipe uccessfully added!"
            return make_response(jsonify(response), status.HTTP_201_CREATED)
        else:
            abort(400,
                  "A category with Id {0} does not exist".format(category_id))