Use a status field for comments

This commit is contained in:
Gabriel Augendre 2020-08-18 21:40:54 +02:00
parent bae6f249a2
commit 9a610a482b
4 changed files with 44 additions and 9 deletions

View file

@ -116,13 +116,13 @@ class PageAdmin(ArticleAdmin):
@register(Comment) @register(Comment)
class CommentAdmin(admin.ModelAdmin): class CommentAdmin(admin.ModelAdmin):
list_display = ("username", "email", "content", "article", "created_at", "approved") list_display = ("username", "email", "content", "article", "created_at", "status")
list_filter = ("approved",) list_filter = ("status",)
search_fields = ("username", "email", "content") search_fields = ("username", "email", "content")
actions = ["approve_comments", "censor_comments"] actions = ["approve_comments", "reject_comments"]
def approve_comments(self, request, queryset): def approve_comments(self, request, queryset):
queryset.update(approved=True) queryset.update(status=Comment.APPROVED)
def censor_comments(self, request, queryset): def reject_comments(self, request, queryset):
queryset.update(approved=False) queryset.update(status=Comment.REJECTED)

View file

@ -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,
),
),
]

View file

@ -102,6 +102,14 @@ class Page(Article):
class Comment(AdminUrlMixin, models.Model): class Comment(AdminUrlMixin, models.Model):
PENDING = "pending"
APPROVED = "approved"
REJECTED = "rejected"
STATUS_CHOICES = (
(PENDING, "Pending"),
(APPROVED, "Approved"),
(REJECTED, "Rejected"),
)
username = models.CharField( username = models.CharField(
max_length=255, help_text="Will be displayed with your comment." 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" Article, on_delete=models.CASCADE, related_name="comments"
) )
created_at = models.DateTimeField(auto_now_add=True) 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: class Meta:
ordering = ["created_at"] ordering = ["created_at"]

View file

@ -5,7 +5,7 @@ from django.views import generic
from django.views.generic.edit import FormMixin from django.views.generic.edit import FormMixin
from articles.forms import CommentForm from articles.forms import CommentForm
from articles.models import Article from articles.models import Article, Comment
class ArticlesListView(generic.ListView): class ArticlesListView(generic.ListView):
@ -52,7 +52,7 @@ class ArticleDetailView(FormMixin, generic.DetailView):
article = self.object article = self.object
if hasattr(article, "article"): if hasattr(article, "article"):
article = article.article article = article.article
context["comments"] = article.comments.filter(approved=True) context["comments"] = article.comments.filter(status=Comment.APPROVED)
return context return context
def get_object(self, queryset=None): def get_object(self, queryset=None):