generated from mwc/project_banjo_app
I’m proud that importing recipes works and the ingredients, steps, and notes show up correctly. I also learned how to set up models and link them with views in Banjo, which was really helpful. This project taught me a lot about building a small app with models, views, and working APIs.
33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
from banjo.models import Model, StringField, IntegerField, ForeignKey
|
|
from datetime import datetime
|
|
|
|
class Recipe(Model):
|
|
name = StringField()
|
|
url = StringField()
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"name": self.name,
|
|
"url": self.url,
|
|
"ingredients": [i.text for i in self.ingredients.all()],
|
|
"steps": [s.text for s 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):
|
|
text = StringField()
|
|
order = IntegerField()
|
|
recipe = ForeignKey("Recipe", related_name="steps")
|
|
|
|
class Note(Model):
|
|
text = StringField()
|
|
creation = StringField(default=lambda: datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
|
recipe = ForeignKey("Recipe", related_name="notes")
|
|
|
|
def to_dict(self):
|
|
return {"date": self.creation, "note": self.text}
|