Use a status field for comments
This commit is contained in:
parent
bae6f249a2
commit
9a610a482b
4 changed files with 44 additions and 9 deletions
|
@ -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)
|
||||
|
|
27
articles/migrations/0015_auto_20200818_2138.py
Normal file
27
articles/migrations/0015_auto_20200818_2138.py
Normal 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,
|
||||
),
|
||||
),
|
||||
]
|
|
@ -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"]
|
||||
|
|
|
@ -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):
|
||||
|
|
Reference in a new issue