From 30f8e404285dd102f054d6b28471d3e07c673f96 Mon Sep 17 00:00:00 2001 From: Vipul Narang Date: Wed, 19 Jun 2024 14:25:12 +0530 Subject: [PATCH] Added multi select feature in Snippet admin --- CHANGELOG.rst | 2 ++ djangocms_snippet/admin.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fdfbf9a4..7cf240bd 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,8 @@ Changelog Unreleased ========== +* feat: Added multiselect feature and add to moderation collection + 4.1.0 (2024-05-16) ================== diff --git a/djangocms_snippet/admin.py b/djangocms_snippet/admin.py index c6e13e53..9753da4b 100644 --- a/djangocms_snippet/admin.py +++ b/djangocms_snippet/admin.py @@ -1,3 +1,4 @@ +from django.apps import apps from django.conf import settings from django.contrib import admin from django.contrib.admin import helpers @@ -30,6 +31,21 @@ djangocms_versioning_enabled = False +def is_moderation_enabled(): + """ + Returns True if the Snippet model is enabled for moderation. + If it is not, or djangocms_moderation is not installed, returns False. + + :returns: True or False + """ + try: + moderation_config = apps.get_app_config("djangocms_moderation") + except LookupError: + return False + + return Snippet in moderation_config.cms_extension.moderated_models + + @admin.register(Snippet) class SnippetAdmin(*snippet_admin_classes): list_display = ('name',) @@ -169,3 +185,24 @@ def has_delete_permission(self, request, obj=None): get_model_permission_codename(self.model, 'add'), ) return False + + def get_actions(self, request): + """ + If djangocms-moderation is enabled, adds admin action to allow multiple snippets to be added to a moderation + collection. + + :param request: Request object + :returns: dict of admin actions + """ + actions = super().get_actions(request) + if not is_moderation_enabled(): + return actions + + from djangocms_moderation.admin_actions import add_items_to_collection + + actions["add_items_to_collection"] = ( + add_items_to_collection, + "add_items_to_collection", + add_items_to_collection.short_description + ) + return actions