Esempio n. 1
0
def delele_recipe():
    id = get_jwt_identity()
    recipe_id = request.json.get('recipe_id', None)

    user = User.get_or_none(User.id == id)
    recipe = Recipe.get_or_none(Recipe.id == recipe_id)

    delete = Recipe.delete().where(Recipe.user_id == user.id,
                                   Recipe.id == recipe.id)
    delete.execute()

    user = User.get_or_none(User.id == id)
    recipes = Recipe.select().where(Recipe.user_id == id)

    # url_photo = recipe.photo
    my_recipes = Recipe.select().where(Recipe.user_id == id)
    recipesNum = len([c.id for c in my_recipes])

    return jsonify({
        "status":
        "success",
        "recipesNum":
        recipesNum,
        "recipes": [{
            "id": recipe.id,
            "name": recipe.name,
            "photo": recipe.photo,
            "countrys": recipe.countrys,
            "hour": recipe.hour,
            "sec": recipe.sec,
            "directions": recipe.directions,
            "ingredients": recipe.ingredients,
            "time": recipe.time,
            "username": user.username,
            "user_id": user.id,
            "user_photo": user.photo
        } for recipe in recipes]
    }), 200
Esempio n. 2
0
def delete_recipe(**kwargs):
    response_dict = {}
    recipe_id = request.get_json().get('recipe_id')
    try:
        recipe = Recipe.get_instance(**{'id': recipe_id})
        kwargs['user'].profile.remove_from_relationship('recipes', recipe)
        Recipe.delete(recipe_id)
        response_dict['status'] = 200
        response_dict['message'] = "Recipe successfully deleted"
        return jsonify(response_dict), 200
   except AssertionError as e:
       response_dict['status'] = 400
       response_dict['message'] = "%s" % (e)
       return jsonify(response_dict), 400


# The following 2 endpoints could possibly be
# merged into one. This depends on the front
# end design when it comes to editing information
@app.route("/user/edit",methods=["PUT"])
@authenticate
def edit_user():
    """
    I'm not sure if I need to return the updated
    information or not. The front end can use the
    given values to udpate the display as long 
    as the status is 200. If the front end will
    re-render, this is not a concern. Otherwise 
    this needs to return a new user information dictionary
    """
    request_dict = request.form.to_dict()
    user_info = {}
    for field in User.get_fields():
        if request_dict.get(field, None) != None:
            user_info[field] = request_dict.get(field)
    try:
        User.update(user.id,**user_info)
        # An updated user information dictionary will
        # be returned on next re-render
        response_dict['status'] = 200
        response_dict['message'] = "Successfully updated information"
        return jsonify(response_dict), 200
    except AssertionError as e:
        # Validation problem
        response_dict['status'] = 400
        response_dict['message'] = "%s" % (e)
        return jsonify(response_dict), 400

@app.route('/profile/edit', methods=["PUT"])
@authenticate
def edit_profile(**kwargs):
    response_dict = {}
    request_dict = request.get_json()
    new_info = {}
    for field in Profile.get_fields():
        if request_dict.get(field, None) != None:
            new_info[field] = request_dict.get(field)
    try:
        Profile.update(user.profile.id, **new_info)
        response_dict['status'] = 200
        response_dict['message'] = "Successfully updated profile"
        return jsonify(response_dict), 200
    except AssertionError as e:
        response_dict['status'] = 400
        response_dict['message'] = "%s" % (e)
        return jsonify(response_dict), 400