fixed and cleand up a lot of servings related stuff

This commit is contained in:
vabene1111
2020-12-31 12:44:19 +01:00
parent 1e471ad40d
commit 6ef173d82d
8 changed files with 77 additions and 36 deletions

View File

@ -147,7 +147,7 @@ admin.site.register(CookLog, CookLogAdmin)
class ShoppingListRecipeAdmin(admin.ModelAdmin):
list_display = ('id', 'recipe', 'multiplier')
list_display = ('id', 'recipe', 'servings')
admin.site.register(ShoppingListRecipe, ShoppingListRecipeAdmin)

View File

@ -266,12 +266,11 @@ class MealPlanForm(forms.ModelForm):
class Meta:
model = MealPlan
fields = ('recipe', 'title', 'meal_type', 'note', 'recipe_multiplier', 'date', 'shared')
fields = ('recipe', 'title', 'meal_type', 'note', 'servings', 'date', 'shared')
help_texts = {
'shared': _('You can list default users to share recipes with in the settings.'),
'note': _('You can use markdown to format this field. See the <a href="/docs/markdown/">docs here</a>'),
'recipe_multiplier': _('Scaling factor for recipe.')
'note': _('You can use markdown to format this field. See the <a href="/docs/markdown/">docs here</a>')
}
widgets = {'recipe': SelectWidget, 'date': DateWidget, 'shared': MultiSelectWidget}

View File

@ -0,0 +1,30 @@
# Generated by Django 3.1.4 on 2020-12-31 11:36
import datetime
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('cookbook', '0092_recipe_servings'),
]
operations = [
migrations.RenameField(
model_name='mealplan',
old_name='recipe_multiplier',
new_name='servings',
),
migrations.AlterField(
model_name='invitelink',
name='valid_until',
field=models.DateField(default=datetime.date(2021, 1, 14)),
),
migrations.AlterField(
model_name='unit',
name='name',
field=models.CharField(max_length=128, unique=True, validators=[django.core.validators.MinLengthValidator(1)]),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.1.4 on 2020-12-31 11:38
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('cookbook', '0093_auto_20201231_1236'),
]
operations = [
migrations.RenameField(
model_name='shoppinglistrecipe',
old_name='multiplier',
new_name='servings',
),
]

View File

@ -272,7 +272,7 @@ class MealType(models.Model):
class MealPlan(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, blank=True, null=True)
recipe_multiplier = models.DecimalField(default=1, max_digits=8, decimal_places=4)
servings = models.DecimalField(default=1, max_digits=8, decimal_places=4)
title = models.CharField(max_length=64, blank=True, default='')
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
shared = models.ManyToManyField(User, blank=True, related_name='plan_share')
@ -294,7 +294,7 @@ class MealPlan(models.Model):
class ShoppingListRecipe(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, null=True, blank=True)
multiplier = models.DecimalField(default=1, max_digits=8, decimal_places=4)
servings = models.DecimalField(default=1, max_digits=8, decimal_places=4)
def __str__(self):
return f'Shopping list recipe {self.id} - {self.recipe}'

View File

@ -202,23 +202,23 @@ class MealPlanSerializer(serializers.ModelSerializer):
recipe_name = serializers.ReadOnlyField(source='recipe.name')
meal_type_name = serializers.ReadOnlyField(source='meal_type.name')
note_markdown = serializers.SerializerMethodField('get_note_markdown')
recipe_multiplier = CustomDecimalField()
servings = CustomDecimalField()
def get_note_markdown(self, obj):
return markdown(obj.note)
class Meta:
model = MealPlan
fields = ('id', 'title', 'recipe', 'recipe_multiplier', 'note', 'note_markdown', 'date', 'meal_type', 'created_by', 'shared', 'recipe_name', 'meal_type_name')
fields = ('id', 'title', 'recipe', 'servings', 'note', 'note_markdown', 'date', 'meal_type', 'created_by', 'shared', 'recipe_name', 'meal_type_name')
class ShoppingListRecipeSerializer(serializers.ModelSerializer):
recipe_name = serializers.ReadOnlyField(source='recipe.name')
multiplier = CustomDecimalField()
servings = CustomDecimalField()
class Meta:
model = ShoppingListRecipe
fields = ('id', 'recipe', 'recipe_name', 'multiplier')
fields = ('id', 'recipe', 'recipe_name', 'servings')
read_only_fields = ('id',)

View File

@ -142,7 +142,7 @@
class="text-muted">{% trans 'You can use markdown to format this field. See the <a href="/docs/markdown/" target="_blank" rel="noopener noreferrer">docs here</a>' %}</span></small>
<br/>
<br/>
<input type="number" class="form-control" v-model="new_note_multiplier"
<input type="number" class="form-control" v-model="new_note_servings"
placeholder="{% trans 'Serving Count' %}" style="margin-bottom: 8px">
<br/>
<draggable :list="pseudo_note_list"
@ -248,7 +248,7 @@
<br/>
<br/>
<small class="text-muted">{% trans 'Serving Count' %}</small><br/>
<span>[[ plan_detail.recipe_multiplier ]]</span>
<span>[[ plan_detail.servings ]]</span>
</template>
<template v-if="plan_detail.note !== ''">
@ -397,7 +397,7 @@
],
new_note_title: '',
new_note_text: '',
new_note_multiplier: '',
new_note_servings: '',
default_shared_users: [],
user_id_update: [],
user_names: {},
@ -622,7 +622,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 : recipe.servings,
servings: (this.new_note_servings > 1) ? this.new_note_servings : recipe.servings,
title: this.new_note_title,
note: this.new_note_text,
is_new: true
@ -630,7 +630,7 @@
this.new_note_title = ''
this.new_note_text = ''
this.new_note_multiplier = ''
this.new_note_servings = ''
return r
},
@ -639,7 +639,7 @@
id: Math.round(Math.random() * 1000) + 10000,
title: this.new_note_title,
note: this.new_note_text,
recipe_multiplier: 1,
servings: 1,
is_new: true,
}
@ -649,7 +649,7 @@
this.new_note_title = ''
this.new_note_text = ''
this.new_note_multiplier = ''
this.new_note_servings = ''
return new_entry
},
planElementName: function (element) {
@ -692,10 +692,10 @@
let first = true
for (let se of this.shopping_list) {
if (first) {
url += `?r=[${se.recipe},${se.recipe_multiplier}]`
url += `?r=[${se.recipe},${se.servings}]`
first = false
} else {
url += `&r=[${se.recipe},${se.recipe_multiplier}]`
url += `&r=[${se.recipe},${se.servings}]`
}
}
return url

View File

@ -89,13 +89,13 @@
<div class="input-group input-group-sm my-auto">
<div class="input-group-prepend">
<button class="text-muted btn btn-outline-primary shadow-none"
@click="((x.multiplier - 1) > 0) ? x.multiplier -= 1 : 1">-
@click="((x.servings - 1) > 0) ? x.servings -= 1 : 1">-
</button>
</div>
<input class="form-control" type="number" v-model="x.multiplier">
<input class="form-control" type="number" v-model="x.servings">
<div class="input-group-append">
<button class="text-muted btn btn-outline-primary shadow-none"
@click="x.multiplier += 1">
@click="x.servings += 1">
+
</button>
</div>
@ -348,15 +348,10 @@
}
},
computed: {
multiplier_cache() {
servings_cache() {
let cache = {}
this.shopping_list.recipes.forEach((r) => {
let multiplier = r.servings;
if(!Number.isNaN(r.multiplier)){
multiplier = parseFloat(r.multiplier)
}
cache[r.id] = multiplier / r.servings;
cache[r.id] = r.servings;
})
return cache
},
@ -369,7 +364,7 @@
let item = {}
Object.assign(item, element);
if (item.list_recipe !== null) {
item.amount = item.amount * this.multiplier_cache[item.list_recipe]
item.amount = item.amount * this.servings_cache[item.list_recipe]
}
item.unit = ((element.unit !== undefined && element.unit !== null) ? element.unit : {'name': ''})
entries.push(item)
@ -407,7 +402,7 @@
this.edit_mode = true
let loadingRecipes = []
{% for r in recipes %}
loadingRecipes.push(this.loadInitialRecipe({{ r.recipe }}, {{ r.multiplier }}))
loadingRecipes.push(this.loadInitialRecipe({{ r.recipe }}, {{ r.servings }}))
{% endfor %}
Promise.allSettled(loadingRecipes).then(() => {
@ -452,9 +447,9 @@
solid: true
})
},
loadInitialRecipe: function (recipe, multiplier) {
loadInitialRecipe: function (recipe, servings) {
return this.$http.get('{% url 'api:recipe-detail' 123456 %}'.replace('123456', recipe)).then((response) => {
this.addRecipeToList(response.data, multiplier)
this.addRecipeToList(response.data, servings)
}).catch((err) => {
console.log("getRecipes error: ", err);
this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger')
@ -622,14 +617,13 @@
getRecipeUrl: function (id) { //TODO generic function that can be reused else were
return '{% url 'view_recipe' 123456 %}'.replace('123456', id)
},
addRecipeToList: function (recipe, multiplier = 1) {
addRecipeToList: function (recipe, servings = 1) {
let slr = {
"created": true,
"id": Math.random() * 1000,
"recipe": recipe.id,
"recipe_name": recipe.name,
"multiplier": multiplier,
"servings": recipe.servings,
"servings": servings,
}
this.shopping_list.recipes.push(slr)