~crocmagnon/blog

ref: f6d6d7b850df58a2f5ce8e8555f9b0328462224d blog/articles/admin.py -rw-r--r-- 4.2 KiB
f6d6d7b8Gabriel Augendre Add task to notify commenters once their comment has been moderated. 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from django import forms
from django.contrib import admin, messages
from django.contrib.admin import register
from django.contrib.auth.admin import UserAdmin
from django.db import models
from django.shortcuts import redirect

from .models import Article, Comment, Page, User

admin.site.register(User, UserAdmin)


@register(Article)
class ArticleAdmin(admin.ModelAdmin):
    ordering = ["status", "-published_at"]
    list_display = [
        "title",
        "status",
        "author",
        "created_at",
        "published_at",
        "updated_at",
    ]
    list_display_links = ["title"]
    list_filter = ["status"]
    date_hierarchy = "created_at"
    fieldsets = [
        (
            "Metadata",
            {
                "fields": (
                    ("title", "slug"),
                    ("author", "status"),
                    ("published_at", "created_at", "updated_at"),
                    "views_count",
                )
            },
        ),
        ("Content", {"fields": ("content",)}),
    ]
    readonly_fields = [
        "created_at",
        "updated_at",
        "views_count",
        "status",
        "published_at",
    ]
    formfield_overrides = {
        models.TextField: {"widget": forms.Textarea(attrs={"style": "width: 98%"})},
    }
    prepopulated_fields = {"slug": ("title",)}
    change_form_template = "articles/article_change_form.html"

    def publish(self, request, queryset):
        if not request.user.has_perm("articles.change_article"):
            messages.warning(request, "You're not allowed to do this.")
            return
        for article in queryset:
            article.publish()
        messages.success(request, f"{len(queryset)} articles published.")

    publish.short_description = "Publish selected articles"

    def unpublish(self, request, queryset):
        if not request.user.has_perm("articles.change_article"):
            messages.warning(request, "You're not allowed to do this.")
            return
        for article in queryset:
            article.unpublish()
        messages.success(request, f"{len(queryset)} articles unpublished.")

    unpublish.short_description = "Unpublish selected articles"
    actions = [publish, unpublish]

    class Media:
        css = {"all": ("admin_articles.css",)}

    def response_post_save_add(self, request, obj: Article):
        if "_preview" in request.POST:
            return redirect("article-detail", slug=obj.slug)
        return super().response_post_save_add(request, obj)

    def response_change(self, request, obj: Article):
        if "_preview" in request.POST:
            obj.save()
            return redirect("article-detail", slug=obj.slug)
        if "_publish" in request.POST:
            obj.publish()
            messages.success(request, "Item has been published")
            return redirect(".")
        if "_unpublish" in request.POST:
            obj.unpublish()
            messages.success(request, "Item has been unpublished")
            return redirect(".")
        return super().response_change(request, obj)


@register(Page)
class PageAdmin(ArticleAdmin):
    list_display = ["position"] + ArticleAdmin.list_display
    fieldsets = [
        (
            "Metadata",
            {
                "fields": (
                    ("title", "slug", "position"),
                    ("author", "status"),
                    ("published_at", "created_at", "updated_at"),
                    "views_count",
                )
            },
        ),
        ("Content", {"fields": ("content",)}),
    ]


@register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = (
        "username",
        "email",
        "content",
        "article",
        "created_at",
        "status",
        "user_notified",
    )
    list_filter = ("status",)
    search_fields = ("username", "email", "content")
    actions = ["approve_comments", "reject_comments"]

    def approve_comments(self, request, queryset):
        count = queryset.update(status=Comment.APPROVED)
        messages.success(request, f"Approved {count} message(s).")

    def reject_comments(self, request, queryset):
        count = queryset.update(status=Comment.REJECTED)
        messages.success(request, f"Rejected {count} message(s).")