Beispiel #1
0
def recipe(recipe_id):
    recipe = Recipe.query.filter_by(id=recipe_id).first()
    if not recipe:
        return jsonify({'message': 'Not found'}), HTTPStatus.NOT_FOUND
    if request.method == 'DELETE':
        recipe.delete()
        return jsonify({}), HTTPStatus.NO_CONTENT
    if request.method == 'GET':
        return RecipeSchema().jsonify(recipe), HTTPStatus.OK
    try:
        data = request.json or request.form
        image = request.files.get('image')
        serializer = RecipeSchema()
        if request.method == 'PUT':
            recipe_serializer = serializer.load(data, instance=recipe)
        elif request.method == 'PATCH':
            recipe_serializer = serializer.load(data,
                                                instance=recipe,
                                                partial=True)
        recipe.owner_id = get_jwt_identity()
        if image:
            recipe.image = save_file(image)
        recipe_serializer.save()
        return RecipeSchema().jsonify(recipe), HTTPStatus.OK
    except ValidationError as err:
        return jsonify(err.messages), HTTPStatus.BAD_REQUEST
Beispiel #2
0
def stories(story_id):
    story = Story.query.filter_by(id=story_id).first()
    if not story:
        return jsonify({'message': 'Not found'}), HTTPStatus.NOT_FOUND
    if request.method == 'DELETE':
        story.delete()
        return jsonify(), HTTPStatus.NO_CONTENT
    if request.method == 'GET':
        return StorySchema().jsonify(story), HTTPStatus.OK
    try:
        data = request.json or request.form
        image = request.files.get('image')
        serializer = StorySchema()
        if request.method == 'PUT':
            story_serializer = serializer.load(data, instance=story)
        elif request.method == 'PATCH':
            story_serializer = serializer.load(data,
                                               instance=story,
                                               partial=True)
        story.owner_id = get_jwt_identity()
        if image:
            story.image = save_file(image)
        story_serializer.save()
        return StorySchema().jsonify(story), HTTPStatus.OK
    except ValidationError as err:
        return jsonify(err.messages), HTTPStatus.BAD_REQUEST
Beispiel #3
0
def categories(category_id):
    category = Category.query.filter_by(id=category_id).first()
    if not category:
        return jsonify({'message': 'Not found'}), HTTPStatus.NOT_FOUND
    if request.method == 'DELETE':
        recipe.delete()
        return jsonify({}), HTTPStatus.NO_CONTENT
    if request.method == 'GET':
        return CategorySchema().jsonify(recipe), HTTPStatus.OK
    try:
        data = request.json or request.form
        image = request.files.get('image')
        serializer = CategorySchema()
        if request.method == 'PUT':
            category_serializer = serializer.load(data, instance=category)
        elif request.method == 'PATCH':
            category_serializer = serializer.load(data,
                                                  instance=category,
                                                  partial=True)
        if image:
            category.image = save_file(image)
        category_serializer.save()
        return CategorySchema().jsonify(category), HTTPStatus.OK
    except ValidationError as err:
        return jsonify(err.messages), HTTPStatus.BAD_REQUEST
Beispiel #4
0
def category():
    if request.method == 'POST':
        try:
            data = request.json or request.form
            image = request.files.get('image')
            serializer = CategorySchema()
            category = serializer.load(data)
            category.image = save_file(image)
            category.save()
            return CategorySchema().jsonify(category), HTTPStatus.CREATED
        except ValidationError as err:
            return jsonify(err.messages), HTTPStatus.BAD_REQUEST
    categories = Category.query.filter_by(is_published=True)
    return CategorySchema(many=True).jsonify(categories), HTTPStatus.OK
def recipes():
    if request.method == 'POST':
        try:
            data = request.json or request.form
            # print(data)
            image = request.files.get('image')
            serializer = RecipeSchema()
            recipe = serializer.load(data)
            recipe.owner_id = 1
            recipe.image = save_file(image)
            recipe.save()
            return RecipeSchema().jsonify(recipe), HTTPStatus.CREATED
        except ValidationError as err:
            return jsonify(err.messages), HTTPStatus.BAD_REQUEST
    recipes = Recipe.query.filter_by(is_published=True)
    return RecipeSchema(many=True).jsonify(recipes), HTTPStatus.OK
Beispiel #6
0
def story():
    if request.method == 'POST':
        try:
            data = request.json or request.form
            print(data)
            image = request.files.get('images')
            serializer = StorySchema()
            story = serializer.load(data)
            story.owner_id = 1
            story.image = save_file(image)
            story.save()
            return StorySchema().jsonify(story), HTTPStatus.CREATED
        except ValidationError as err:
            return jsonify(err.messages), HTTPStatus.BAD_REQUEST
    stories = Story.query.filter_by(is_published=True)
    return StorySchema(many=True).jsonify(stories), HTTPStatus.OK