コード例 #1
0
ファイル: views.py プロジェクト: hatchways/team-cheesesteak
def edit_recipe(recipe_id):    
    request_dict = request.get_json()
    recipe_id = request_dict.get('recipe_id')
    recipe_info = {'recipe': {}}
    response_dict = {}
    for field in Recipe.get_fields():
        if request_dict.get(field, None) != None:
            recipe_info[field] = request_dict.get(field)
    try:
        Recipe.update(recipe_id,**recipe_info)
        recipe = Recipe.get_instance(**{'id', recipe_id})
        response_dict['recipe'] = recipe.to_dict(excludes=[
            'profile',
            'profile_id',
            'price',
            'ingredients',
            'required_items',
            'image_urls'
        ])
        for field, value in recipe.get_formatted_info():
            response_dict['recipe'][field] = value
        response_dict['status'] = 200
        response_dict['message'] = "Successfully updated recipe"
        return jsonify(response_dict), 200
    except AssertionError as e:
        # Validation problem
        response_dict['status'] = 400
        response_dict['message'] = "%s" % (e)
        return jsonfiy(response_dict), 400
コード例 #2
0
ファイル: views.py プロジェクト: hatchways/team-cheesesteak
def create_recipe(**kwargs):
    request_dict = request.get_json()
    recipe_info = {}
    response_dict = {'recipe': {}}
    for field in Recipe.get_fields():
        if request_dict.get(field, None) != None:
            recipe_info[field] = request_dict.get(field)
    try:
        recipe = Recipe.create(**recipe_info)
        kwargs['user'].profile.add_to_relationship('recipes', recipe)
        response_dict['recipe'] = recipe.to_dict(excludes=[
            'profile',
            'profile_id',
            'price',
            'ingredients',
            'required_items',
            'image_urls'
        ])
        for field, value in recipe.get_formatted_info():
            response_dict['recipe'][field] = value
        response_dict['status'] = 200
        response_dict['message'] = "Successfully created recipe"
        return jsonify(response_dict), 201
    except AssertionError as e:
        response_dict['status'] = 400
        response_dict['message'] = "%s" % (e)
        # Validation problem
        return jsonify(response_dict), 400