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

104 lines
3.1 KiB
Python
Raw Normal View History

2020-11-10 16:26:27 +01:00
import copy
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
from .models import Article, 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"),
2020-11-28 20:09:37 +01:00
("author", "keywords"),
("status", "published_at"),
("created_at", "updated_at"),
"views_count",
"has_code",
]
2020-08-14 15:53:42 +02:00
},
),
2020-12-25 11:53:49 +01:00
(
"Content",
{
"fields": (
"content",
"custom_css",
)
},
),
2020-08-14 15:53:42 +02:00
]
readonly_fields = [
"created_at",
"updated_at",
"views_count",
"status",
"published_at",
]
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)