From 9a610a482b3a9b132034da037b2d21526d6f5e8f Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Tue, 18 Aug 2020 21:40:54 +0200 Subject: [PATCH] Use a status field for comments --- articles/admin.py | 12 ++++----- .../migrations/0015_auto_20200818_2138.py | 27 +++++++++++++++++++ articles/models.py | 10 ++++++- articles/views/html.py | 4 +-- 4 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 articles/migrations/0015_auto_20200818_2138.py diff --git a/articles/admin.py b/articles/admin.py index e90a21d..5d93d06 100644 --- a/articles/admin.py +++ b/articles/admin.py @@ -116,13 +116,13 @@ class PageAdmin(ArticleAdmin): @register(Comment) class CommentAdmin(admin.ModelAdmin): - list_display = ("username", "email", "content", "article", "created_at", "approved") - list_filter = ("approved",) + list_display = ("username", "email", "content", "article", "created_at", "status") + list_filter = ("status",) search_fields = ("username", "email", "content") - actions = ["approve_comments", "censor_comments"] + actions = ["approve_comments", "reject_comments"] def approve_comments(self, request, queryset): - queryset.update(approved=True) + queryset.update(status=Comment.APPROVED) - def censor_comments(self, request, queryset): - queryset.update(approved=False) + def reject_comments(self, request, queryset): + queryset.update(status=Comment.REJECTED) diff --git a/articles/migrations/0015_auto_20200818_2138.py b/articles/migrations/0015_auto_20200818_2138.py new file mode 100644 index 0000000..9922d95 --- /dev/null +++ b/articles/migrations/0015_auto_20200818_2138.py @@ -0,0 +1,27 @@ +# Generated by Django 3.1 on 2020-08-18 19:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("articles", "0014_auto_20200818_1952"), + ] + + operations = [ + migrations.RemoveField(model_name="comment", name="approved",), + migrations.AddField( + model_name="comment", + name="status", + field=models.CharField( + choices=[ + ("pending", "Pending"), + ("approved", "Approved"), + ("rejected", "Rejected"), + ], + default="pending", + max_length=10, + ), + ), + ] diff --git a/articles/models.py b/articles/models.py index 098276b..bbe4454 100644 --- a/articles/models.py +++ b/articles/models.py @@ -102,6 +102,14 @@ class Page(Article): class Comment(AdminUrlMixin, models.Model): + PENDING = "pending" + APPROVED = "approved" + REJECTED = "rejected" + STATUS_CHOICES = ( + (PENDING, "Pending"), + (APPROVED, "Approved"), + (REJECTED, "Rejected"), + ) username = models.CharField( max_length=255, help_text="Will be displayed with your comment." ) @@ -121,7 +129,7 @@ class Comment(AdminUrlMixin, models.Model): Article, on_delete=models.CASCADE, related_name="comments" ) created_at = models.DateTimeField(auto_now_add=True) - approved = models.BooleanField(default=True) + status = models.CharField(max_length=10, choices=STATUS_CHOICES, default=PENDING) class Meta: ordering = ["created_at"] diff --git a/articles/views/html.py b/articles/views/html.py index d392682..afe6af1 100644 --- a/articles/views/html.py +++ b/articles/views/html.py @@ -5,7 +5,7 @@ from django.views import generic from django.views.generic.edit import FormMixin from articles.forms import CommentForm -from articles.models import Article +from articles.models import Article, Comment class ArticlesListView(generic.ListView): @@ -52,7 +52,7 @@ class ArticleDetailView(FormMixin, generic.DetailView): article = self.object if hasattr(article, "article"): article = article.article - context["comments"] = article.comments.filter(approved=True) + context["comments"] = article.comments.filter(status=Comment.APPROVED) return context def get_object(self, queryset=None):