From 652b4bf2af529e266411a885b5288bcfb8c62519 Mon Sep 17 00:00:00 2001 From: tourn Date: Sun, 30 Aug 2020 15:59:25 +0200 Subject: [PATCH] Add serving count to recipe --- cookbook/forms.py | 3 ++- cookbook/migrations/0075_recipe_servings.py | 18 ++++++++++++++++++ cookbook/models.py | 1 + cookbook/serializer.py | 2 +- .../templates/forms/edit_internal_recipe.html | 3 +++ cookbook/templates/recipe_view.html | 12 +++++++++--- 6 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 cookbook/migrations/0075_recipe_servings.py diff --git a/cookbook/forms.py b/cookbook/forms.py index 395209b7d..c1de68bc9 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -84,13 +84,14 @@ class InternalRecipeForm(forms.ModelForm): class Meta: model = Recipe - fields = ('name', 'image', 'working_time', 'waiting_time', 'keywords') + fields = ('name', 'image', 'working_time', 'waiting_time', 'servings', 'keywords') labels = { 'name': _('Name'), 'keywords': _('Keywords'), 'working_time': _('Preparation time in minutes'), 'waiting_time': _('Waiting time (cooking/baking) in minutes'), + 'servings': _('Number of servings'), } widgets = {'keywords': MultiSelectWidget} diff --git a/cookbook/migrations/0075_recipe_servings.py b/cookbook/migrations/0075_recipe_servings.py new file mode 100644 index 000000000..1d84b65e5 --- /dev/null +++ b/cookbook/migrations/0075_recipe_servings.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.7 on 2020-08-30 13:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0074_remove_keyword_created_by'), + ] + + operations = [ + migrations.AddField( + model_name='recipe', + name='servings', + field=models.IntegerField(default=1), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index a6a2d8629..1496958d4 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -175,6 +175,7 @@ class Step(models.Model): class Recipe(models.Model): name = models.CharField(max_length=128) + servings = models.IntegerField(default=1) image = models.ImageField(upload_to='recipes/', blank=True, null=True) storage = models.ForeignKey(Storage, on_delete=models.PROTECT, blank=True, null=True) file_uid = models.CharField(max_length=256, default="", blank=True) diff --git a/cookbook/serializer.py b/cookbook/serializer.py index a843b56ee..01df252c0 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -139,7 +139,7 @@ class RecipeSerializer(WritableNestedModelSerializer): class Meta: model = Recipe - fields = ['id', 'name', 'image', 'keywords', 'steps', 'working_time', 'waiting_time', 'created_by', 'created_at', 'updated_at', 'internal'] + fields = ['id', 'name', 'image', 'keywords', 'steps', 'working_time', 'waiting_time', 'servings', 'created_by', 'created_at', 'updated_at', 'internal'] read_only_fields = ['image', 'created_by', 'created_at'] diff --git a/cookbook/templates/forms/edit_internal_recipe.html b/cookbook/templates/forms/edit_internal_recipe.html index 6685e665f..e617d528d 100644 --- a/cookbook/templates/forms/edit_internal_recipe.html +++ b/cookbook/templates/forms/edit_internal_recipe.html @@ -62,6 +62,9 @@
+ + +
+ v-model="servings"/>
@@ -448,6 +448,8 @@ let csrftoken = Cookies.get('csrftoken'); Vue.http.headers.common['X-CSRFToken'] = csrftoken; + const recipe_servings = {{ recipe.servings }} + let app = new Vue({ delimiters: ['[[', ']]'], el: '#id_base_container', @@ -455,9 +457,13 @@ recipe: undefined, has_ingredients: false, has_times: false, - ingredient_factor: 1, + servings: recipe_servings, + }, + computed: { + ingredient_factor: function () { + return this.servings / recipe_servings + } }, - mounted: function () { this.loadRecipe() },