diff --git a/cookbook/forms.py b/cookbook/forms.py index 1355bd9c..4ef327c8 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 00000000..974be08c --- /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 181d51be..db5b85b5 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 b64a1ce1..db5d0ffa 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 01d07f46..30f27c94 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: