ingredient rounding upgrades

This commit is contained in:
vabene1111
2020-06-11 22:32:45 +02:00
parent 8cc0fcaed2
commit 8ff52f542e
6 changed files with 54 additions and 8 deletions

View File

@ -31,13 +31,14 @@ class UserPreferenceForm(forms.ModelForm):
class Meta: class Meta:
model = UserPreference model = UserPreference
fields = ('default_unit', 'theme', 'nav_color', 'default_page', 'show_recent', 'search_style', 'plan_share') fields = ('default_unit', 'theme', 'nav_color', 'default_page', 'show_recent', 'search_style', 'plan_share', 'ingredient_decimals')
help_texts = { help_texts = {
'nav_color': _('Color of the top navigation bar. Not all colors work with all themes, just try them out!'), 'nav_color': _('Color of the top navigation bar. Not all colors work with all themes, just try them out!'),
'default_unit': _('Default Unit to be used when inserting a new ingredient into a recipe.'), 'default_unit': _('Default Unit to be used when inserting a new ingredient into a recipe.'),
'plan_share': _('Default user to share newly created meal plan entries with.'), 'plan_share': _('Default user to share newly created meal plan entries with.'),
'show_recent': _('Show recently viewed recipes on search page.'), 'show_recent': _('Show recently viewed recipes on search page.'),
'ingredient_decimals': _('Number of decimals to round ingredients.')
} }
widgets = { widgets = {

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-11 20:14
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('cookbook', '0051_auto_20200611_1518'),
]
operations = [
migrations.AddField(
model_name='userpreference',
name='ingredient_decimals',
field=models.IntegerField(default=2),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-11 20:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('cookbook', '0052_userpreference_ingredient_decimals'),
]
operations = [
migrations.AlterField(
model_name='recipeingredient',
name='amount',
field=models.DecimalField(decimal_places=16, default=0, max_digits=32),
),
]

View File

@ -63,6 +63,7 @@ class UserPreference(models.Model):
search_style = models.CharField(choices=SEARCH_STYLE, max_length=64, default=LARGE) search_style = models.CharField(choices=SEARCH_STYLE, max_length=64, default=LARGE)
show_recent = models.BooleanField(default=True) show_recent = models.BooleanField(default=True)
plan_share = models.ManyToManyField(User, blank=True, related_name='plan_share_default') plan_share = models.ManyToManyField(User, blank=True, related_name='plan_share_default')
ingredient_decimals = models.IntegerField(default=2)
def __str__(self): def __str__(self):
return str(self.user) return str(self.user)
@ -163,7 +164,7 @@ class RecipeIngredient(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
ingredient = models.ForeignKey(Ingredient, on_delete=models.PROTECT) ingredient = models.ForeignKey(Ingredient, on_delete=models.PROTECT)
unit = models.ForeignKey(Unit, on_delete=models.PROTECT) unit = models.ForeignKey(Unit, on_delete=models.PROTECT)
amount = models.DecimalField(default=0, decimal_places=2, max_digits=16) amount = models.DecimalField(default=0, decimal_places=16, max_digits=32)
note = models.CharField(max_length=64, null=True, blank=True) note = models.CharField(max_length=64, null=True, blank=True)
def __str__(self): def __str__(self):

View File

@ -327,6 +327,8 @@
<script type="text/javascript"> <script type="text/javascript">
reloadIngredients()
$(function () { $(function () {
$('[data-toggle="popover"]').popover() $('[data-toggle="popover"]').popover()
}); });
@ -335,20 +337,25 @@
trigger: 'focus' trigger: 'focus'
}); });
function roundToTwo(num) { function roundDecimals(num) {
return +(Math.round(num + "e+2") + "e-2"); return +(Math.round(num + "e+{{ request.user.userpreference.ingredient_decimals }}") + "e-{{ request.user.userpreference.ingredient_decimals }}");
} }
function reloadIngredients() { function reloadIngredients() {
factor = Number($('#in_factor').val()); let factor = Number($('#in_factor').val());
ingredients = { let ingredients = {
{% for i in ingredients %} {% for i in ingredients %}
{{ i.pk }}: {{ i.amount|unlocalize }}, {{ i.pk }}: {{ i.amount|unlocalize }},
{% endfor %} {% endfor %}
} }
for (var key in ingredients) { for (let key in ingredients) {
$('#ing_' + key).html(roundToTwo(ingredients[key] * factor)) let val = ''
if (Math.abs(ingredients[key] * factor - roundDecimals(ingredients[key] * factor)) > 0) {
val += '~'
}
$('#ing_' + key).html(val + roundDecimals(ingredients[key] * factor))
} }
} }

View File

@ -214,6 +214,7 @@ def user_settings(request):
up.show_recent = form.cleaned_data['show_recent'] up.show_recent = form.cleaned_data['show_recent']
up.search_style = form.cleaned_data['search_style'] up.search_style = form.cleaned_data['search_style']
up.plan_share.set(form.cleaned_data['plan_share']) up.plan_share.set(form.cleaned_data['plan_share'])
up.ingredient_decimals = form.cleaned_data['ingredient_decimals']
up.save() up.save()
if 'user_name_form' in request.POST: if 'user_name_form' in request.POST: