From b180def20ca736940aab2a3097575d51acd956f3 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 3 Sep 2020 21:29:28 +0200 Subject: [PATCH] Allow disabling comments on article/page --- articles/admin.py | 5 +++-- .../0019_article_comments_allowed.py | 18 ++++++++++++++++++ articles/models.py | 1 + .../templates/articles/article_detail.html | 4 +++- .../templates/articles/comment_snippet.html | 4 +++- articles/views/html.py | 18 +++++++++++++----- 6 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 articles/migrations/0019_article_comments_allowed.py diff --git a/articles/admin.py b/articles/admin.py index 4ddac26..134115d 100644 --- a/articles/admin.py +++ b/articles/admin.py @@ -29,8 +29,9 @@ class ArticleAdmin(admin.ModelAdmin): { "fields": ( ("title", "slug"), - ("author", "status"), - ("published_at", "created_at", "updated_at"), + ("author", "comments_allowed"), + ("status", "published_at"), + ("created_at", "updated_at"), "views_count", ) }, diff --git a/articles/migrations/0019_article_comments_allowed.py b/articles/migrations/0019_article_comments_allowed.py new file mode 100644 index 0000000..9f3995a --- /dev/null +++ b/articles/migrations/0019_article_comments_allowed.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1 on 2020-09-03 19:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("articles", "0018_auto_20200821_1232"), + ] + + operations = [ + migrations.AddField( + model_name="article", + name="comments_allowed", + field=models.BooleanField(default=True), + ), + ] diff --git a/articles/models.py b/articles/models.py index 065eddf..d3d1261 100644 --- a/articles/models.py +++ b/articles/models.py @@ -48,6 +48,7 @@ class Article(AdminUrlMixin, models.Model): author = models.ForeignKey(User, on_delete=models.PROTECT, default=1) views_count = models.IntegerField(default=0) slug = models.SlugField(unique=True, max_length=255) + comments_allowed = models.BooleanField(default=True) objects = ArticleManager() with_pages = models.Manager() diff --git a/articles/templates/articles/article_detail.html b/articles/templates/articles/article_detail.html index 18956d4..78477c4 100644 --- a/articles/templates/articles/article_detail.html +++ b/articles/templates/articles/article_detail.html @@ -11,5 +11,7 @@ {{ article.get_formatted_content|safe }} - {% include 'articles/comment_snippet.html' %} + {% if article.comments_allowed %} + {% include 'articles/comment_snippet.html' %} + {% endif %} {% endblock %} diff --git a/articles/templates/articles/comment_snippet.html b/articles/templates/articles/comment_snippet.html index e608df6..207e644 100644 --- a/articles/templates/articles/comment_snippet.html +++ b/articles/templates/articles/comment_snippet.html @@ -15,7 +15,9 @@

{% empty %} - No reaction yet, write your own! +

+ No reaction yet, write your own! +

{% endfor %}
{% csrf_token %} diff --git a/articles/views/html.py b/articles/views/html.py index 07d06c2..cf8f848 100644 --- a/articles/views/html.py +++ b/articles/views/html.py @@ -1,3 +1,5 @@ +from typing import Union + from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models import F @@ -5,7 +7,7 @@ from django.views import generic from django.views.generic.edit import FormMixin from articles.forms import CommentForm -from articles.models import Article, Comment +from articles.models import Article, Comment, Page class ArticlesListView(generic.ListView): @@ -55,10 +57,10 @@ class ArticleDetailView(FormMixin, generic.DetailView): context["comments"] = article.comments.filter(status=Comment.APPROVED) return context - def get_object(self, queryset=None): - obj = super().get_object(queryset) + def get_object(self, queryset=None) -> Union[Article, Page]: + obj = super().get_object(queryset) # type: Article if hasattr(obj, "page"): - obj = obj.page + obj = obj.page # type: Page if not self.request.user.is_authenticated: obj.views_count = F("views_count") + 1 obj.save(update_fields=["views_count"]) @@ -66,8 +68,14 @@ class ArticleDetailView(FormMixin, generic.DetailView): return obj def post(self, request, *args, **kwargs): - self.object = self.get_object() + self.object = self.get_object() # type: Union[Article, Page] form = self.get_form() + + if not self.object.comments_allowed: + messages.error(self.request, "Comments are disabled on this article.") + # Bypassing self.form_invalid because we don't want its error message + return super().form_invalid(form) + if form.is_valid(): return self.form_valid(form) else: