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/src/articles/admin.py

152 lines
4.9 KiB
Python
Raw Normal View History

2021-12-31 12:08:03 +01:00
from typing import cast
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
2021-12-31 12:08:03 +01:00
from django.db.models import QuerySet
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect
2020-08-14 14:53:30 +02:00
2021-03-03 17:30:38 +01:00
from .models import Article, Tag, 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",
2021-01-02 11:40:50 +01:00
"keywords",
2020-08-14 15:53:42 +02:00
"created_at",
"published_at",
"updated_at",
2020-08-26 13:28:31 +02:00
"views_count",
2020-12-28 08:42:30 +01:00
"has_code",
"is_home",
"has_custom_css",
"read_time",
2020-08-14 15:53:42 +02:00
]
list_display_links = ["title"]
2020-12-28 08:42:30 +01:00
list_filter = [
"status",
"has_code",
"status",
"is_home",
]
2020-08-14 15:53:42 +02:00
date_hierarchy = "created_at"
fieldsets = [
(
"Metadata",
{
"fields": [
2020-08-16 19:45:38 +02:00
("title", "slug"),
2021-03-03 17:30:38 +01:00
("author", "tags"),
("status", "published_at"),
("created_at", "updated_at"),
2020-12-28 08:42:30 +01:00
("views_count", "read_time"),
("has_code", "has_custom_css"),
]
2020-08-14 15:53:42 +02:00
},
),
2020-12-25 11:53:49 +01:00
(
"Content",
2020-12-27 15:49:27 +01:00
{"fields": ("content",)},
),
(
"Custom CSS",
2020-12-25 11:53:49 +01:00
{
2020-12-27 15:49:27 +01:00
"fields": ("custom_css",),
"classes": ("collapse",),
2020-12-25 11:53:49 +01:00
},
),
2020-08-14 15:53:42 +02:00
]
readonly_fields = [
"created_at",
"updated_at",
"views_count",
"status",
"published_at",
2020-12-28 08:42:30 +01:00
"read_time",
"has_custom_css",
]
2020-08-16 19:45:38 +02:00
prepopulated_fields = {"slug": ("title",)}
change_form_template = "articles/article_change_form.html"
2021-03-03 17:30:38 +01:00
search_fields = ["title", "content", "tags__name"]
autocomplete_fields = ["tags"]
2021-03-20 11:36:26 +01:00
show_full_result_count = False
2021-12-31 12:08:03 +01:00
def get_queryset(self, request: HttpRequest) -> QuerySet:
2021-03-20 11:36:26 +01:00
queryset = super().get_queryset(request)
queryset = queryset.prefetch_related("tags")
return queryset
2020-08-16 18:15:19 +02:00
2021-12-28 22:31:53 +01:00
@admin.action(description="Publish selected articles")
2021-12-31 12:08:03 +01:00
def publish(self, request: HttpRequest, queryset: QuerySet) -> None:
2020-08-16 18:15:19 +02:00
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.")
2021-12-28 22:31:53 +01:00
@admin.action(description="Unpublish selected articles")
2021-12-31 12:08:03 +01:00
def unpublish(self, request: HttpRequest, queryset: QuerySet) -> None:
2020-08-16 18:15:19 +02:00
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.")
2021-12-28 22:31:53 +01:00
@admin.action(description="Refresh draft key of selected articles")
2021-12-31 12:08:03 +01:00
def refresh_draft_key(self, request: HttpRequest, queryset: QuerySet) -> None:
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.refresh_draft_key()
messages.success(request, f"{len(queryset)} draft keys refreshed.")
actions = [publish, unpublish, refresh_draft_key]
2020-08-16 18:39:27 +02:00
class Media:
css = {"all": ("admin_articles.css",)}
2020-08-17 09:57:24 +02:00
2021-12-31 12:08:03 +01:00
def response_post_save_add(
self, request: HttpRequest, obj: Article
) -> HttpResponseRedirect:
2020-08-18 21:30:23 +02:00
if "_preview" in request.POST:
2021-12-31 12:08:03 +01:00
return cast(HttpResponseRedirect, redirect("article-detail", slug=obj.slug))
2020-08-18 21:30:23 +02:00
return super().response_post_save_add(request, obj)
2021-12-31 12:08:03 +01:00
def response_change(self, request: HttpRequest, obj: Article) -> HttpResponse:
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-12-28 08:42:30 +01:00
2021-12-31 12:08:03 +01:00
def read_time(self, instance: Article) -> str:
2020-12-28 08:42:30 +01:00
return f"{instance.get_read_time()} min"
2021-12-28 22:31:53 +01:00
@admin.display(boolean=True)
2021-12-31 12:08:03 +01:00
def has_custom_css(self, instance: Article) -> bool:
2020-12-28 08:42:30 +01:00
return bool(instance.custom_css)
2021-03-03 17:30:38 +01:00
@register(Tag)
class TagAdmin(admin.ModelAdmin):
2021-03-04 18:51:38 +01:00
list_display = ["name", "slug"]
search_fields = ["slug", "name"]
list_display_links = ["slug", "name"]
prepopulated_fields = {"slug": ("name",)}