Initial commit
This commit is contained in:
BIN
cookbook/app/database.sqlite
Normal file
BIN
cookbook/app/database.sqlite
Normal file
Binary file not shown.
41
cookbook/app/models.py
Normal file
41
cookbook/app/models.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from banjo.models import (
|
||||
Model,
|
||||
StringField,
|
||||
IntegerField,
|
||||
FloatField,
|
||||
ForeignKey,
|
||||
)
|
||||
from django.db.models import DateTimeField
|
||||
|
||||
class Recipe(Model):
|
||||
name = StringField()
|
||||
url = StringField()
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"name": self.name,
|
||||
"ingredients": [ing.text for ing in self.ingredients.all()],
|
||||
"instructions": [step.text for step in self.steps.order_by("order")],
|
||||
"notes": [n.to_dict() for n in self.notes.order_by("creation")],
|
||||
}
|
||||
|
||||
class Ingredient(Model):
|
||||
text = StringField()
|
||||
recipe = ForeignKey("Recipe", related_name="ingredients")
|
||||
|
||||
class Step(Model):
|
||||
recipe = ForeignKey("Recipe", related_name="steps")
|
||||
text = StringField()
|
||||
order = IntegerField()
|
||||
|
||||
class Note(Model):
|
||||
recipe = ForeignKey("Recipe", related_name="notes")
|
||||
creation=DateTimeField(auto_now_add=True)
|
||||
text=StringField()
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"date": self.creation.strftime("%A %B %-d, %Y %I:%M %p"),
|
||||
"note": self.text,
|
||||
}
|
||||
|
74
cookbook/app/views.py
Normal file
74
cookbook/app/views.py
Normal file
@@ -0,0 +1,74 @@
|
||||
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()
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user