diff --git a/cookbook/forms.py b/cookbook/forms.py index d83bd528a..2642fe8ce 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -88,13 +88,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/0091_recipe_servings.py b/cookbook/migrations/0091_recipe_servings.py new file mode 100644 index 000000000..0b023bf7e --- /dev/null +++ b/cookbook/migrations/0091_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', '0090_auto_20201214_1359'), + ] + + operations = [ + migrations.AddField( + model_name='recipe', + name='servings', + field=models.IntegerField(default=1), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 7d1b09583..87d6e62fe 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -197,6 +197,7 @@ class NutritionInformation(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 4b9a21d1c..e0800ed12 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -153,7 +153,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', 'nutrition'] + fields = ['id', 'name', 'image', 'keywords', 'steps', 'working_time', 'waiting_time', 'created_by', 'created_at', 'updated_at', 'internal', 'nutrition', 'servings'] 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 a71aab8e6..8d28581f0 100644 --- a/cookbook/templates/forms/edit_internal_recipe.html +++ b/cookbook/templates/forms/edit_internal_recipe.html @@ -62,6 +62,9 @@ {% trans 'Waiting Time' %} + {% trans 'Servings' %} + + {% trans 'Keywords' %} - - [[element.name]] + + + [[element.name]] + + [[element.servings]] @@ -140,7 +143,7 @@ + placeholder="{% trans 'Serving Count' %}" style="margin-bottom: 8px"> @@ -243,6 +246,9 @@ {% trans 'Recipe' %} [[ plan_detail.recipe_name ]] + + {% trans 'Serving Count' %} + [[ plan_detail.recipe_multiplier ]] @@ -615,7 +621,7 @@ id: Math.round(Math.random() * 1000) + 10000, recipe: recipe.id, recipe_name: recipe.name, - recipe_multiplier: (this.new_note_multiplier > 1) ? this.new_note_multiplier : 1, + recipe_multiplier: (this.new_note_multiplier > 1) ? this.new_note_multiplier : recipe.servings, title: this.new_note_title, note: this.new_note_text, is_new: true diff --git a/cookbook/templates/recipe_view.html b/cookbook/templates/recipe_view.html index 055384039..3bc7fb5e5 100644 --- a/cookbook/templates/recipe_view.html +++ b/cookbook/templates/recipe_view.html @@ -39,7 +39,7 @@ {% trans 'Add to Book' %} - + {% trans 'Add to Shopping' %} - + @@ -516,6 +516,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', @@ -523,9 +525,16 @@ recipe: undefined, has_ingredients: false, has_times: false, - ingredient_factor: 1, + servings: recipe_servings, + }, + computed: { + ingredient_factor: function () { + return this.servings / recipe_servings + }, + shopping_url: function () { + return `{% url 'view_shopping' %}?r=[${this.recipe.id},${this.servings}]` + }, }, - mounted: function () { this.loadRecipe() }, @@ -577,9 +586,6 @@ } }, - getShoppingUrl: function () { - return `{% url 'view_shopping' %}?r=[${this.recipe.id},${this.ingredient_factor}]` - }, calculateAmount: function (amount) { {% if request.user.userpreference.use_fractions %} let return_string = '' diff --git a/cookbook/templates/shopping_list.html b/cookbook/templates/shopping_list.html index c84d1b9f6..3f55ff0ef 100644 --- a/cookbook/templates/shopping_list.html +++ b/cookbook/templates/shopping_list.html @@ -349,7 +349,12 @@ multiplier_cache() { let cache = {} this.shopping_list.recipes.forEach((r) => { - cache[r.id] = !(Number.isNaN(r.multiplier)) ? parseFloat(r.multiplier) : 1 + let multiplier = r.servings; + if(!Number.isNaN(r.multiplier)){ + multiplier = parseFloat(r.multiplier) + } + cache[r.id] = multiplier / r.servings; + }) return cache }, @@ -621,7 +626,8 @@ "id": Math.random() * 1000, "recipe": recipe.id, "recipe_name": recipe.name, - "multiplier": multiplier + "multiplier": multiplier, + "servings": recipe.servings, } this.shopping_list.recipes.push(slr)