Add action buttons in article/page admin

This commit is contained in:
Gabriel Augendre 2020-08-18 12:41:12 +02:00
parent 85f235330e
commit 386b620361
2 changed files with 36 additions and 1 deletions

View file

@ -3,6 +3,7 @@ from django.contrib import admin, messages
from django.contrib.admin import register from django.contrib.admin import register
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.admin import UserAdmin
from django.db import models from django.db import models
from django.shortcuts import redirect
from .models import Article, Page, User from .models import Article, Page, User
@ -36,13 +37,20 @@ class ArticleAdmin(admin.ModelAdmin):
), ),
("Content", {"fields": ("content",)}), ("Content", {"fields": ("content",)}),
] ]
readonly_fields = ["created_at", "updated_at", "views_count"] readonly_fields = [
"created_at",
"updated_at",
"views_count",
"status",
"published_at",
]
formfield_overrides = { formfield_overrides = {
models.TextField: { models.TextField: {
"widget": forms.Textarea(attrs={"cols": "100", "rows": "50"}) "widget": forms.Textarea(attrs={"cols": "100", "rows": "50"})
}, },
} }
prepopulated_fields = {"slug": ("title",)} prepopulated_fields = {"slug": ("title",)}
change_form_template = "articles/article_change_form.html"
def publish(self, request, queryset): def publish(self, request, queryset):
if not request.user.has_perm("articles.change_article"): if not request.user.has_perm("articles.change_article"):
@ -68,6 +76,20 @@ class ArticleAdmin(admin.ModelAdmin):
class Media: class Media:
css = {"all": ("admin_articles.css",)} css = {"all": ("admin_articles.css",)}
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)
@register(Page) @register(Page)
class PageAdmin(ArticleAdmin): class PageAdmin(ArticleAdmin):

View file

@ -0,0 +1,13 @@
{% extends 'admin/change_form.html' %}
{% block submit_buttons_bottom %}
{{ block.super }}
<div class="submit-row">
{% if original.status != original.PUBLISHED %}
<input type="submit" value="Save & publish" name="_publish">
{% elif original.status != original.DRAFT %}
<input type="submit" value="Save & unpublish" name="_unpublish">
{% endif %}
<input type="submit" value="Save & preview" name="_preview">
</div>
{% endblock %}