From 386b620361a484198dc3353b8343a3e31506346c Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Tue, 18 Aug 2020 12:41:12 +0200 Subject: [PATCH] Add action buttons in article/page admin --- articles/admin.py | 24 ++++++++++++++++++- .../articles/article_change_form.html | 13 ++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 articles/templates/articles/article_change_form.html diff --git a/articles/admin.py b/articles/admin.py index 78de714..bc1873f 100644 --- a/articles/admin.py +++ b/articles/admin.py @@ -3,6 +3,7 @@ from django.contrib import admin, messages from django.contrib.admin import register from django.contrib.auth.admin import UserAdmin from django.db import models +from django.shortcuts import redirect from .models import Article, Page, User @@ -36,13 +37,20 @@ class ArticleAdmin(admin.ModelAdmin): ), ("Content", {"fields": ("content",)}), ] - readonly_fields = ["created_at", "updated_at", "views_count"] + readonly_fields = [ + "created_at", + "updated_at", + "views_count", + "status", + "published_at", + ] formfield_overrides = { models.TextField: { "widget": forms.Textarea(attrs={"cols": "100", "rows": "50"}) }, } prepopulated_fields = {"slug": ("title",)} + change_form_template = "articles/article_change_form.html" def publish(self, request, queryset): if not request.user.has_perm("articles.change_article"): @@ -68,6 +76,20 @@ class ArticleAdmin(admin.ModelAdmin): class Media: 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) class PageAdmin(ArticleAdmin): diff --git a/articles/templates/articles/article_change_form.html b/articles/templates/articles/article_change_form.html new file mode 100644 index 0000000..9d1ff5d --- /dev/null +++ b/articles/templates/articles/article_change_form.html @@ -0,0 +1,13 @@ +{% extends 'admin/change_form.html' %} + +{% block submit_buttons_bottom %} + {{ block.super }} +
+ {% if original.status != original.PUBLISHED %} + + {% elif original.status != original.DRAFT %} + + {% endif %} + +
+{% endblock %}