This repository has been archived on 2023-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
python-blog/articles/admin.py

140 lines
4.3 KiB
Python
Raw Normal View History

2020-08-16 18:39:27 +02:00
from django import forms
2020-08-16 18:15:19 +02:00
from django.contrib import admin, messages
2020-08-14 15:53:42 +02:00
from django.contrib.admin import register
2020-08-14 14:53:30 +02:00
from django.contrib.auth.admin import UserAdmin
2020-08-16 18:39:27 +02:00
from django.db import models
from django.shortcuts import redirect
2020-08-14 14:53:30 +02:00
2020-08-18 18:49:41 +02:00
from .models import Article, Comment, Page, User
2020-08-14 14:53:30 +02:00
admin.site.register(User, UserAdmin)
2020-08-14 15:53:42 +02:00
@register(Article)
class ArticleAdmin(admin.ModelAdmin):
2020-08-18 21:29:52 +02:00
ordering = ["status", "-published_at"]
2020-08-14 15:53:42 +02:00
list_display = [
"title",
"status",
"author",
"created_at",
"published_at",
"updated_at",
2020-08-26 13:28:31 +02:00
"views_count",
2020-08-14 15:53:42 +02:00
]
list_display_links = ["title"]
list_filter = ["status"]
date_hierarchy = "created_at"
fieldsets = [
(
"Metadata",
{
"fields": (
2020-08-16 19:45:38 +02:00
("title", "slug"),
("author", "status"),
("published_at", "created_at", "updated_at"),
"views_count",
2020-08-14 15:53:42 +02:00
)
},
),
("Content", {"fields": ("content",)}),
]
readonly_fields = [
"created_at",
"updated_at",
"views_count",
"status",
"published_at",
]
2020-08-16 18:39:27 +02:00
formfield_overrides = {
2020-08-18 21:30:09 +02:00
models.TextField: {"widget": forms.Textarea(attrs={"style": "width: 98%"})},
2020-08-16 18:39:27 +02:00
}
2020-08-16 19:45:38 +02:00
prepopulated_fields = {"slug": ("title",)}
change_form_template = "articles/article_change_form.html"
2020-08-16 18:15:19 +02:00
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()
2020-08-16 18:15:19 +02:00
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()
2020-08-16 18:15:19 +02:00
messages.success(request, f"{len(queryset)} articles unpublished.")
unpublish.short_description = "Unpublish selected articles"
actions = [publish, unpublish]
2020-08-16 18:39:27 +02:00
class Media:
css = {"all": ("admin_articles.css",)}
2020-08-17 09:57:24 +02:00
2020-08-18 21:30:23 +02:00
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)
2020-08-17 09:57:24 +02:00
@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",)}),
]
2020-08-18 18:49:41 +02:00
@register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = (
"username",
"email",
"content",
"article",
"created_at",
"status",
"user_notified",
)
2020-08-18 21:40:54 +02:00
list_filter = ("status",)
2020-08-18 18:49:41 +02:00
search_fields = ("username", "email", "content")
2020-08-18 21:40:54 +02:00
actions = ["approve_comments", "reject_comments"]
2020-08-18 18:49:41 +02:00
def approve_comments(self, request, queryset):
count = queryset.update(status=Comment.APPROVED, user_notified=False)
messages.success(request, f"Approved {count} message(s).")
2020-08-18 18:49:41 +02:00
2020-08-18 21:40:54 +02:00
def reject_comments(self, request, queryset):
count = queryset.update(status=Comment.REJECTED, user_notified=False)
messages.success(request, f"Rejected {count} message(s).")