split protocol into steps

This commit is contained in:
Cedric Girard 2019-11-08 16:46:41 +01:00
parent e04d691403
commit a41542b478
Signed by: X-dark
GPG key ID: E7D0E125DB9519E4
5 changed files with 46 additions and 6 deletions

View file

@ -1,14 +1,18 @@
from django.contrib import admin
from .models import Recipe, Ingredient, IngredientUsage
from .models import Recipe, Ingredient, IngredientUsage, Step
class IngredientUsageInline(admin.TabularInline):
model = IngredientUsage
extra = 1
class StepInline(admin.TabularInline):
model = Step
extra = 1
class RecipeAdmin(admin.ModelAdmin):
inlines = (IngredientUsageInline,)
fields = [ 'name', 'protocol' ]
inlines = (IngredientUsageInline,StepInline)
fields = [ 'name' ]
admin.site.register(Recipe,RecipeAdmin)
admin.site.register(Ingredient)

View file

@ -0,0 +1,27 @@
# Generated by Django 2.2.6 on 2019-11-07 16:28
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('recipes', '0002_recipe_protocol'),
]
operations = [
migrations.RemoveField(
model_name='recipe',
name='protocol',
),
migrations.CreateModel(
name='Step',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('step', models.PositiveSmallIntegerField()),
('protocol', models.TextField(default='')),
('recipe', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='recipes.Recipe')),
],
),
]

View file

@ -9,11 +9,15 @@ class Ingredient(models.Model):
class Recipe(models.Model):
name = models.CharField(max_length=128)
ingredients = models.ManyToManyField(Ingredient, through='IngredientUsage')
protocol = models.TextField(default='')
def __str__(self):
return self.name
class Step(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
step = models.PositiveSmallIntegerField()
protocol = models.TextField(default='')
class IngredientUsage(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)

View file

@ -7,4 +7,8 @@
</ul>
<h2>Steps</h2>
<p>{{ recipe.protocol }}</p>
<ul>
{% for step in steps %}
<li>{{ step.protocol }}</li>
{% endfor %}
</ul>

View file

@ -1,6 +1,6 @@
from django.views import generic
from .models import Recipe, IngredientUsage
from .models import Recipe, IngredientUsage, Step
class IndexView(generic.ListView):
@ -17,4 +17,5 @@ class DetailView(generic.DetailView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['ingredient_list'] = IngredientUsage.objects.filter(recipe=self.object.pk)
context['steps'] = Step.objects.filter(recipe=self.object.pk).order_by('step')
return context