django-project/recipes/models.py

54 lines
2 KiB
Python

"""
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
"""
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
class Recipe(models.Model):
title = models.CharField(max_length=100, null=False, blank=False)
slug = models.SlugField(unique=True)
def __str__(self) -> str:
return self.title
def get_absolute_url(self):
return reverse('recipe', kwargs={'slug': self.slug})
class Version(models.Model):
label = models.CharField(max_length=20, default='Original')
slug = models.SlugField(max_length=20, default='original')
body = models.TextField(null=True, blank=True)
user = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False)
author = models.CharField(max_length=30, blank=True)
recipe = models.ForeignKey(Recipe, on_delete=models.PROTECT, null=False, blank=False, related_name='versions')
def __str__(self) -> str:
return self.recipe.title + ' - ' + self.label
def get_absolute_url(self):
return reverse('version', kwargs={'slug_recipe': self.recipe.slug, 'slug_version': self.slug})
class Ingredient(models.Model):
text = models.CharField(max_length=50, null=False, blank=False)
version = models.ForeignKey(Version, on_delete=models.CASCADE, null=False, blank=False, related_name='ingredients')
def __str__(self) -> str:
return self.text + ' for ' + str(self.version)