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

70 lines
2.1 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
2020-08-14 14:53:30 +02:00
2020-08-14 15:53:42 +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):
list_display = [
"title",
"status",
"author",
"created_at",
"published_at",
"updated_at",
]
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"]
2020-08-16 18:39:27 +02:00
formfield_overrides = {
models.TextField: {
"widget": forms.Textarea(attrs={"cols": "100", "rows": "50"})
},
}
2020-08-16 19:45:38 +02:00
prepopulated_fields = {"slug": ("title",)}
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",)}