From 4f49b06704168bc18d01ee81bfec973e62eca73c Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Mon, 13 Apr 2020 22:37:50 +0200 Subject: [PATCH] user setting default ingredient unit --- cookbook/forms.py | 5 +++-- .../0032_userpreference_default_unit.py | 18 ++++++++++++++++++ cookbook/models.py | 1 + .../templates/forms/edit_internal_recipe.html | 2 +- cookbook/views/views.py | 1 + 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 cookbook/migrations/0032_userpreference_default_unit.py diff --git a/cookbook/forms.py b/cookbook/forms.py index 1355bd9c0..4ef327c84 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -31,10 +31,11 @@ class UserPreferenceForm(forms.ModelForm): class Meta: model = UserPreference - fields = ('theme', 'nav_color') + fields = ('default_unit', 'theme', 'nav_color') 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.') } diff --git a/cookbook/migrations/0032_userpreference_default_unit.py b/cookbook/migrations/0032_userpreference_default_unit.py new file mode 100644 index 000000000..974be08c7 --- /dev/null +++ b/cookbook/migrations/0032_userpreference_default_unit.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.4 on 2020-04-13 20:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0031_auto_20200407_1841'), + ] + + operations = [ + migrations.AddField( + model_name='userpreference', + name='default_unit', + field=models.CharField(default='g', max_length=32), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 181d51be3..db5b85b59 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -44,6 +44,7 @@ class UserPreference(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) theme = models.CharField(choices=THEMES, max_length=128, default=FLATLY) nav_color = models.CharField(choices=COLORS, max_length=128, default=PRIMARY) + default_unit = models.CharField(max_length=32, default='g') def __str__(self): return self.user diff --git a/cookbook/templates/forms/edit_internal_recipe.html b/cookbook/templates/forms/edit_internal_recipe.html index b64a1ce12..db5d0ffad 100644 --- a/cookbook/templates/forms/edit_internal_recipe.html +++ b/cookbook/templates/forms/edit_internal_recipe.html @@ -189,7 +189,7 @@ data.push({ ingredient__name: "{% trans 'Ingredient' %}", amount: "100", - unit__name: "g", + unit__name: "{{ request.user.userpreference.default_unit }}", note: "", id: Math.floor(Math.random() * 10000000), delete: false, diff --git a/cookbook/views/views.py b/cookbook/views/views.py index 01d07f46d..30f27c949 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -175,6 +175,7 @@ def settings(request): up = UserPreference(user=request.user) up.theme = form.cleaned_data['theme'] up.nav_color = form.cleaned_data['nav_color'] + up.default_unit = form.cleaned_data['default_unit'] up.save() if 'user_name_form' in request.POST: