mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 04:10:06 -05:00
ingredient rounding upgrades
This commit is contained in:
@@ -31,13 +31,14 @@ class UserPreferenceForm(forms.ModelForm):
|
||||
|
||||
class Meta:
|
||||
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 = {
|
||||
'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.'),
|
||||
'plan_share': _('Default user to share newly created meal plan entries with.'),
|
||||
'show_recent': _('Show recently viewed recipes on search page.'),
|
||||
'ingredient_decimals': _('Number of decimals to round ingredients.')
|
||||
}
|
||||
|
||||
widgets = {
|
||||
|
||||
@@ -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),
|
||||
),
|
||||
]
|
||||
18
cookbook/migrations/0053_auto_20200611_2217.py
Normal file
18
cookbook/migrations/0053_auto_20200611_2217.py
Normal 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),
|
||||
),
|
||||
]
|
||||
@@ -63,6 +63,7 @@ class UserPreference(models.Model):
|
||||
search_style = models.CharField(choices=SEARCH_STYLE, max_length=64, default=LARGE)
|
||||
show_recent = models.BooleanField(default=True)
|
||||
plan_share = models.ManyToManyField(User, blank=True, related_name='plan_share_default')
|
||||
ingredient_decimals = models.IntegerField(default=2)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.user)
|
||||
@@ -163,7 +164,7 @@ class RecipeIngredient(models.Model):
|
||||
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
|
||||
ingredient = models.ForeignKey(Ingredient, 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)
|
||||
|
||||
def __str__(self):
|
||||
|
||||
@@ -327,6 +327,8 @@
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
reloadIngredients()
|
||||
|
||||
$(function () {
|
||||
$('[data-toggle="popover"]').popover()
|
||||
});
|
||||
@@ -335,20 +337,25 @@
|
||||
trigger: 'focus'
|
||||
});
|
||||
|
||||
function roundToTwo(num) {
|
||||
return +(Math.round(num + "e+2") + "e-2");
|
||||
function roundDecimals(num) {
|
||||
return +(Math.round(num + "e+{{ request.user.userpreference.ingredient_decimals }}") + "e-{{ request.user.userpreference.ingredient_decimals }}");
|
||||
}
|
||||
|
||||
function reloadIngredients() {
|
||||
factor = Number($('#in_factor').val());
|
||||
ingredients = {
|
||||
let factor = Number($('#in_factor').val());
|
||||
let ingredients = {
|
||||
{% for i in ingredients %}
|
||||
{{ i.pk }}: {{ i.amount|unlocalize }},
|
||||
{% endfor %}
|
||||
}
|
||||
|
||||
for (var key in ingredients) {
|
||||
$('#ing_' + key).html(roundToTwo(ingredients[key] * factor))
|
||||
for (let key in ingredients) {
|
||||
let val = ''
|
||||
if (Math.abs(ingredients[key] * factor - roundDecimals(ingredients[key] * factor)) > 0) {
|
||||
val += '~'
|
||||
}
|
||||
|
||||
$('#ing_' + key).html(val + roundDecimals(ingredients[key] * factor))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -214,6 +214,7 @@ def user_settings(request):
|
||||
up.show_recent = form.cleaned_data['show_recent']
|
||||
up.search_style = form.cleaned_data['search_style']
|
||||
up.plan_share.set(form.cleaned_data['plan_share'])
|
||||
up.ingredient_decimals = form.cleaned_data['ingredient_decimals']
|
||||
up.save()
|
||||
|
||||
if 'user_name_form' in request.POST:
|
||||
|
||||
Reference in New Issue
Block a user