From 08cccfa13383046e25e0638ba898dec9f406e5e9 Mon Sep 17 00:00:00 2001 From: tourn Date: Mon, 13 Apr 2020 19:56:00 +0200 Subject: [PATCH] Allow mealplan items to have no recipes And display first line of notes in plan --- .../0031_mealplan_recipe_optional.py | 19 +++++++++++++++++++ cookbook/models.py | 8 +++++++- cookbook/templates/meal_plan.html | 5 ++++- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 cookbook/migrations/0031_mealplan_recipe_optional.py diff --git a/cookbook/migrations/0031_mealplan_recipe_optional.py b/cookbook/migrations/0031_mealplan_recipe_optional.py new file mode 100644 index 000000000..76a8a1b67 --- /dev/null +++ b/cookbook/migrations/0031_mealplan_recipe_optional.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.4 on 2020-04-13 17:48 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0030_recipeingredient_note'), + ] + + operations = [ + migrations.AlterField( + model_name='mealplan', + name='recipe', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='cookbook.Recipe'), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 181d51be3..ab3d14287 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -201,10 +201,16 @@ class MealPlan(models.Model): MEAL_TYPES = ((BREAKFAST, _('Breakfast')), (LUNCH, _('Lunch')), (DINNER, _('Dinner')), (OTHER, _('Other')),) user = models.ForeignKey(User, on_delete=models.CASCADE) - recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) + recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, blank=True, null=True) meal = models.CharField(choices=MEAL_TYPES, max_length=128, default=BREAKFAST) note = models.TextField(blank=True) date = models.DateField() + def note_head(self): + try: + return self.note.split('\n')[0] + except: + return '' + def __str__(self): return self.meal + ' (' + str(self.date) + ') ' + str(self.recipe) diff --git a/cookbook/templates/meal_plan.html b/cookbook/templates/meal_plan.html index 0691e473b..653ca57fd 100644 --- a/cookbook/templates/meal_plan.html +++ b/cookbook/templates/meal_plan.html @@ -76,7 +76,10 @@ {% for mp in days_value %} - {{ mp.recipe.name }}
+ {% if mp.recipe %} + {{ mp.recipe.name }}
+ {% endif %} + {{ mp.note_head }} {% endfor %} {% endfor %}