generated from mwc/project_banjo_app
75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
from app.models import Recipe, Ingredient, Step, Note
|
|
from banjo.http import BadRequest
|
|
from banjo.urls import route_get, route_post
|
|
from scrape_schema_recipe import scrape_url
|
|
|
|
@route_get('recipes/all')
|
|
def show_all_recipes(params):
|
|
"""
|
|
Returns all recipes.
|
|
"""
|
|
recipes = Recipe.objects.all()
|
|
return {'recipes': [r.to_dict() for r in recipes]}
|
|
|
|
@route_get('recipes/search', args={'query': str})
|
|
def search_for_recipe(params):
|
|
"""
|
|
Searches for recipes (by name).
|
|
"""
|
|
recipes = Recipe.objects.filter(name__icontains=params['query'])
|
|
return {'recipes': [r.to_dict() for r in recipes]}
|
|
|
|
@route_get('recipes/search-ingredient', args={'query': str})
|
|
def search_for_recipe_by_ingredient(params):
|
|
"""
|
|
Searches for recipes (by name).
|
|
"""
|
|
recipes = Recipe.objects.filter(ingredients__text__icontains=params['query'])
|
|
return {'recipes': [r.to_dict() for r in recipes]}
|
|
|
|
@route_post('recipes/import', args={'url': str})
|
|
def import_recipe(params):
|
|
"""
|
|
Tries to import a recipe from a URL.
|
|
If something goes wrong, returns a 400 error with an explanation.
|
|
"""
|
|
url = params['url']
|
|
if Recipe.objects.filter(url=url).exists():
|
|
raise BadRequest(f"{url} has already been imported.")
|
|
try:
|
|
recipes = scrape_url(url)
|
|
except:
|
|
raise BadRequest(f"Error reading {url}")
|
|
if len(recipes) == 0:
|
|
raise BadRequest(f"No recipes found at {url}")
|
|
try:
|
|
recipes_created = []
|
|
for r in recipes:
|
|
recipe = Recipe(name=r['name'])
|
|
ingredients = r['recipeIngredient']
|
|
steps = [step['text'] for step in r['recipeInstructions']]
|
|
recipe.save()
|
|
for i in ingredients:
|
|
ingredient = Ingredient(recipe=recipe, text=i)
|
|
ingredient.save()
|
|
for s in steps:
|
|
order = recipe.steps.count() + 1
|
|
step = Step(recipe=recipe, text=s, order=order)
|
|
step.save()
|
|
recipes_created.append(recipe.to_dict())
|
|
return {'recipes': recipes_created}
|
|
except KeyError:
|
|
raise BadRequest(f"Found a recipe at {url}, but it had an unexpected format.")
|
|
|
|
@route_post("recipes/add-note", args={'recipe_id': int, 'note': str})
|
|
def add_note(params):
|
|
"""Adds a note to a recipe
|
|
"""
|
|
recipe = Recipe.objects.get(id=params['recipe_id'])
|
|
note = Note(recipe=recipe, text=params['note'])
|
|
note.save()
|
|
return recipe.to_dict()
|
|
|
|
|
|
|