cheesy/moku/models/recipe.py

87 lines
2.4 KiB
Python
Raw Normal View History

2024-03-25 21:46:47 +00:00
from django.db import models
from django.urls import reverse
2024-03-26 12:18:47 +00:00
from django.utils.translation import gettext_lazy as _
2024-03-25 21:46:47 +00:00
from shortuuid.django_fields import ShortUUIDField
class RecipeManager(models.Manager):
"""Manages recipe objects more efficiently by pre-fetching steps."""
2024-03-25 21:46:47 +00:00
def get_queryset(self):
2024-03-26 12:18:47 +00:00
return (
super()
.get_queryset()
2024-03-25 21:46:47 +00:00
.prefetch_related(
models.Prefetch("steps", queryset=RecipeStep.objects.order_by("order"))
)
2024-03-26 12:18:47 +00:00
)
2024-03-25 21:46:47 +00:00
class Recipe(models.Model):
"""Represents a single recipe on the site."""
2024-03-25 21:46:47 +00:00
uuid = ShortUUIDField(
verbose_name=_("unique id"),
max_length=22,
length=22,
help_text=_("the unique id that identifies this recipe."),
)
title = models.CharField(
verbose_name=_("recipe title"),
max_length=64,
help_text=_(
"give the recipe a title, just so you know what the recipe is for."
),
2024-03-25 21:46:47 +00:00
)
created_by = models.ForeignKey(
"User",
related_name="recipes",
db_index=True,
db_column="created_by_user_id",
on_delete=models.CASCADE,
)
2024-03-26 12:18:47 +00:00
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
2024-03-25 21:46:47 +00:00
objects = RecipeManager()
def __str__(self):
return f"{self.title}"
2024-03-25 21:46:47 +00:00
def get_absolute_url(self):
return reverse("recipe.show", kwargs={"uuid": self.uuid})
class RecipeStep(models.Model):
"""Represents a single step belonging to a recipe."""
2024-03-25 21:46:47 +00:00
uuid = ShortUUIDField(
verbose_name=_("step id"),
max_length=22,
length=22,
help_text=_("the unique id that identifies this step."),
)
instructions = models.CharField(
verbose_name=_("step instructions"),
max_length=256,
2024-03-26 12:18:47 +00:00
help_text=_(
"the instructions for this step of the recipe. try to keep it clear and "
"concise!"
),
2024-03-25 21:46:47 +00:00
)
order = models.IntegerField(
verbose_name=_("step number"),
default=0,
db_index=True,
2024-03-26 12:18:47 +00:00
help_text=_(
"which step in the recipe is this. this affects the order the recipe steps "
"are shown."
),
2024-03-25 21:46:47 +00:00
)
recipe = models.ForeignKey(
2024-03-26 12:18:47 +00:00
"Recipe", related_name="steps", db_index=True, on_delete=models.CASCADE
2024-03-25 21:46:47 +00:00
)
def __str__(self):
return f"step #{self.order + 1} of {self.recipe}"