From 8cd94d49e8513cb46c8a08c468b9c4b0f16a88d4 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 16 Jun 2020 12:44:45 +0200 Subject: [PATCH] added comment system preference --- .env.template | 9 ++++ cookbook/forms.py | 5 +- .../migrations/0055_auto_20200616_1236.py | 24 +++++++++ cookbook/models.py | 3 ++ cookbook/templates/recipe_view.html | 51 ++++++++++--------- cookbook/views/views.py | 2 + recipes/settings.py | 2 + 7 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 cookbook/migrations/0055_auto_20200616_1236.py diff --git a/.env.template b/.env.template index d31cb01b3..9ae5d9756 100644 --- a/.env.template +++ b/.env.template @@ -1,4 +1,5 @@ # only set this to true when testing/debugging +# when unset: 1 (true) - dont unset this, just for development DEBUG=0 # hosts the application can run under e.g. recipes.mydomain.com,cooking.mydomain.com,... @@ -18,9 +19,17 @@ POSTGRES_DB=djangodb # Serve mediafiles directly using gunicorn. Basically everyone recommends not doing this. Please use any of the examples # provided that include an additional nxginx container to handle media file serving. # If you know what you are doing turn this back on (1) to serve media files using djangos serve() method. +# when unset: 1 (true) - this is temporary until an appropriate amount of time has passed for everyone to migrate GUNICORN_MEDIA=0 # allow authentication via reverse proxy (e.g. authelia), leave of if you dont know what you are doing # docs: https://github.com/vabene1111/recipes/tree/develop/docs/docker/nginx-proxy%20with%20proxy%20authentication +# when unset: 0 (false) REVERSE_PROXY_AUTH=0 + + +# the default value for the user preference 'comments' (enable/disable commenting system) +# when unset: 1 (true) +COMMENT_PREF_DEFAULT=1 + diff --git a/cookbook/forms.py b/cookbook/forms.py index 6571ae101..338f2b905 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -31,14 +31,15 @@ class UserPreferenceForm(forms.ModelForm): class Meta: model = UserPreference - fields = ('default_unit', 'theme', 'nav_color', 'default_page', 'show_recent', 'search_style', 'plan_share', 'ingredient_decimals') + fields = ('default_unit', 'theme', 'nav_color', 'default_page', 'show_recent', 'search_style', 'plan_share', 'ingredient_decimals', 'comments') 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.') + 'ingredient_decimals': _('Number of decimals to round ingredients.'), + 'comments': _('If you want to be able to create and see comments underneath recipes.') } widgets = { diff --git a/cookbook/migrations/0055_auto_20200616_1236.py b/cookbook/migrations/0055_auto_20200616_1236.py new file mode 100644 index 000000000..e4c8847b9 --- /dev/null +++ b/cookbook/migrations/0055_auto_20200616_1236.py @@ -0,0 +1,24 @@ +# Generated by Django 3.0.7 on 2020-06-16 10:36 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0054_sharelink'), + ] + + operations = [ + migrations.AddField( + model_name='userpreference', + name='comments', + field=models.BooleanField(default=True), + ), + migrations.AlterField( + model_name='sharelink', + name='uuid', + field=models.UUIDField(default=uuid.UUID('a6e8f192-cc03-4dd4-8a03-58d7ab6b7df7')), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index e88f465b1..b557f23d8 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -6,6 +6,8 @@ from django.contrib.auth.models import User from django.utils.translation import gettext as _ from django.db import models +from recipes.settings import COMMENT_PREF_DEFAULT + def get_user_name(self): if not (name := f"{self.first_name} {self.last_name}") == " ": @@ -64,6 +66,7 @@ class UserPreference(models.Model): show_recent = models.BooleanField(default=True) plan_share = models.ManyToManyField(User, blank=True, related_name='plan_share_default') ingredient_decimals = models.IntegerField(default=2) + comments = models.BooleanField(default=COMMENT_PREF_DEFAULT) def __str__(self): return str(self.user) diff --git a/cookbook/templates/recipe_view.html b/cookbook/templates/recipe_view.html index e1945d507..5e2c422fa 100644 --- a/cookbook/templates/recipe_view.html +++ b/cookbook/templates/recipe_view.html @@ -268,36 +268,39 @@ {% endif %} -
-
- -
{% trans 'Comments' %} -
- {% for c in comments %} -
-
- {{ c.updated_at }} {% trans 'by' %} {{ c.created_by.username }}
- {{ c.text }} -
-
+ {% if request.user.userpreference.comments %} +

- {% endfor %} - {% if request.user.is_authenticated %} -
+
{% trans 'Comments' %} +
+ {% for c in comments %} +
+
+ {{ c.updated_at }} {% trans 'by' %} {{ c.created_by.username }}
+ {{ c.text }} +
+
+
+ {% endfor %} -
- {% csrf_token %} -
+ {% if request.user.is_authenticated %} +
+ + + {% csrf_token %} +
-
- +
+ +
-
- -
+ +
+ {% endif %} {% endif %} {% if recipe.storage %} diff --git a/cookbook/views/views.py b/cookbook/views/views.py index c510d78f6..4be10b931 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -215,6 +215,7 @@ def user_settings(request): if form.is_valid(): if not up: 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'] @@ -223,6 +224,7 @@ def user_settings(request): 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.comments = form.cleaned_data['comments'] up.save() if 'user_name_form' in request.POST: diff --git a/recipes/settings.py b/recipes/settings.py index b0f8b9b27..70fc60271 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -25,6 +25,8 @@ GUNICORN_MEDIA = bool(int(os.getenv('GUNICORN_MEDIA', True))) REVERSE_PROXY_AUTH = bool(int(os.getenv('REVERSE_PROXY_AUTH', False))) +COMMENT_PREF_DEFAULT = bool(int(os.getenv('COMMENT_PREF_DEFAULT', True))) + ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS').split(',') if os.getenv('ALLOWED_HOSTS') else ['*'] CORS_ORIGIN_ALLOW_ALL = True