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
|
2020-08-18 12:41:12 +02:00
|
|
|
from django.shortcuts import redirect
|
2020-08-14 14:53:30 +02:00
|
|
|
|
2020-12-24 17:49:47 +01: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",
|
|
|
|
{
|
2020-09-12 16:39:07 +02:00
|
|
|
"fields": [
|
2020-08-16 19:45:38 +02:00
|
|
|
("title", "slug"),
|
2020-11-28 20:09:37 +01:00
|
|
|
("author", "keywords"),
|
2020-09-03 21:29:28 +02:00
|
|
|
("status", "published_at"),
|
|
|
|
("created_at", "updated_at"),
|
2020-08-14 22:06:38 +02:00
|
|
|
"views_count",
|
2020-12-24 17:49:47 +01:00
|
|
|
"has_code",
|
2020-12-27 20:00:41 +01:00
|
|
|
"draft_public_url",
|
2020-09-12 16:39:07 +02:00
|
|
|
]
|
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
|
|
|
]
|
2020-08-18 12:41:12 +02:00
|
|
|
readonly_fields = [
|
|
|
|
"created_at",
|
|
|
|
"updated_at",
|
|
|
|
"views_count",
|
|
|
|
"status",
|
|
|
|
"published_at",
|
2020-12-27 20:00:41 +01:00
|
|
|
"draft_public_url",
|
2020-08-18 12:41:12 +02:00
|
|
|
]
|
2020-08-16 19:45:38 +02:00
|
|
|
prepopulated_fields = {"slug": ("title",)}
|
2020-08-18 12:41:12 +02:00
|
|
|
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:
|
2020-08-16 18:40:02 +02:00
|
|
|
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:
|
2020-08-16 18:40:02 +02:00
|
|
|
article.unpublish()
|
2020-08-16 18:15:19 +02:00
|
|
|
messages.success(request, f"{len(queryset)} articles unpublished.")
|
|
|
|
|
|
|
|
unpublish.short_description = "Unpublish selected articles"
|
2020-12-27 20:00:41 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-08-18 12:41:12 +02:00
|
|
|
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)
|