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

141 lines
4.2 KiB
Python
Raw Normal View History

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
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"]
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"
def refresh_draft_key(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.refresh_draft_key()
messages.success(request, f"{len(queryset)} draft keys refreshed.")
refresh_draft_key.short_description = "Refresh draft key of selected articles"
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
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-12-28 08:42:30 +01:00
def read_time(self, instance: Article):
return f"{instance.get_read_time()} min"
def has_custom_css(self, instance: Article):
return bool(instance.custom_css)
has_custom_css.boolean = True
2021-03-03 17:30:38 +01:00
@register(Tag)
class TagAdmin(admin.ModelAdmin):
list_display = ["name"]
search_fields = ["name"]