From 5feacb880d87df7277fd61464d84a3e345a6130b Mon Sep 17 00:00:00 2001 From: Bernard Van Der Vyver Date: Fri, 24 Jun 2022 15:29:24 +0100 Subject: [PATCH] feat: Removed expiry date from version table (#59) * Removed expiry date from version table and replaced with compliance number. Added additional content settings icon on the version table * added additional_content_settings_icon.html template * Added testcase to ensure the icon is available on the version table * isort --- CHANGELOG.rst | 1 + djangocms_content_expiry/constants.py | 1 + djangocms_content_expiry/monkeypatch/admin.py | 50 ++++++++++++++++--- .../additional_content_settings_icon.html | 5 ++ .../calendar_icon.html | 2 +- tests/test_monkeypatch.py | 31 ++++++++++-- 6 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 djangocms_content_expiry/templates/djangocms_content_expiry/admin/icons/additional_content_settings_icon.html diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 736052a..b32c572 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,7 @@ Changelog unreleased ========== +* feat: Expiry field removed from version table and replaced with compliance number. Added additional content settings action to version table 1.3.1 (2022-06-22) ================== diff --git a/djangocms_content_expiry/constants.py b/djangocms_content_expiry/constants.py index 0f8b80f..b1ea1dd 100644 --- a/djangocms_content_expiry/constants.py +++ b/djangocms_content_expiry/constants.py @@ -2,5 +2,6 @@ CONTENT_EXPIRY_EXPIRE_FIELD_LABEL = _("expiry date") +CONTENT_EXPIRY_COMPLIANCE_FIELD_LABEL = _("compliance number") CONTENT_EXPIRY_CHANGELIST_PAGECONTENT_EXCLUSION_CACHE_KEY = "djangocms_content_expiry_changelist_pagecontent_exclusion" diff --git a/djangocms_content_expiry/monkeypatch/admin.py b/djangocms_content_expiry/monkeypatch/admin.py index b3308b5..f13866b 100644 --- a/djangocms_content_expiry/monkeypatch/admin.py +++ b/djangocms_content_expiry/monkeypatch/admin.py @@ -1,34 +1,70 @@ from django.conf.urls import url from django.shortcuts import redirect +from django.template.loader import render_to_string from django.urls import reverse from djangocms_moderation import admin as moderation_admin from djangocms_moderation.models import ModerationCollection, ModerationRequest from djangocms_versioning import admin -from djangocms_content_expiry.constants import CONTENT_EXPIRY_EXPIRE_FIELD_LABEL +from djangocms_content_expiry.constants import CONTENT_EXPIRY_COMPLIANCE_FIELD_LABEL from djangocms_content_expiry.models import ContentExpiry -def expires(self, obj): +def _get_expiry_link(self, obj, request): + """ + Generate a content expiry link for the Versioning Admin + """ + expiry_url = reverse( + "admin:{app}_{model}_change".format( + app=ContentExpiry._meta.app_label, model=ContentExpiry._meta.model_name + ), + args=(obj.contentexpiry.pk,), + ) + return render_to_string( + 'djangocms_content_expiry/admin/icons/additional_content_settings_icon.html', + { + "url": f"{expiry_url}?_popup=1", + } + ) + + +admin.VersionAdmin._get_expiry_link = _get_expiry_link + + +def get_state_actions(func): + """ + Add additional content settings action to Versioning state actions + """ + def inner(self, *args, **kwargs): + state_list = func(self, *args, **kwargs) + state_list.append(self._get_expiry_link) + return state_list + return inner + + +admin.VersionAdmin.get_state_actions = get_state_actions(admin.VersionAdmin.get_state_actions) + + +def compliance_number(self, obj): version = ContentExpiry.objects.filter(version=obj.pk) if version: - return version[0].expires + return version[0].compliance_number return "" -expires.short_description = CONTENT_EXPIRY_EXPIRE_FIELD_LABEL -admin.VersionAdmin.expire = expires +compliance_number.short_description = CONTENT_EXPIRY_COMPLIANCE_FIELD_LABEL +admin.VersionAdmin.compliance_number = compliance_number def get_list_display(func): """ - Register the expires field with the Versioning Admin + Register the compliance number field with the Versioning Admin """ def inner(self, request): list_display = func(self, request) created_by_index = list_display.index('created_by') - return list_display[:created_by_index] + ('expire',) + list_display[created_by_index:] + return list_display[:created_by_index] + ('compliance_number',) + list_display[created_by_index:] return inner diff --git a/djangocms_content_expiry/templates/djangocms_content_expiry/admin/icons/additional_content_settings_icon.html b/djangocms_content_expiry/templates/djangocms_content_expiry/admin/icons/additional_content_settings_icon.html new file mode 100644 index 0000000..2a66662 --- /dev/null +++ b/djangocms_content_expiry/templates/djangocms_content_expiry/admin/icons/additional_content_settings_icon.html @@ -0,0 +1,5 @@ +{% load static i18n %} + + + + \ No newline at end of file diff --git a/djangocms_content_expiry/templates/djangocms_content_expiry/calendar_icon.html b/djangocms_content_expiry/templates/djangocms_content_expiry/calendar_icon.html index 1376d13..d83f046 100644 --- a/djangocms_content_expiry/templates/djangocms_content_expiry/calendar_icon.html +++ b/djangocms_content_expiry/templates/djangocms_content_expiry/calendar_icon.html @@ -1,2 +1,2 @@ {% load static i18n %} - + diff --git a/tests/test_monkeypatch.py b/tests/test_monkeypatch.py index 3119126..bd5fde3 100644 --- a/tests/test_monkeypatch.py +++ b/tests/test_monkeypatch.py @@ -1,6 +1,7 @@ import datetime from django.contrib import admin +from django.template.loader import render_to_string from django.test import RequestFactory from django.urls import reverse @@ -9,7 +10,7 @@ from djangocms_moderation import constants from djangocms_versioning.constants import PUBLISHED -from djangocms_content_expiry.constants import CONTENT_EXPIRY_EXPIRE_FIELD_LABEL +from djangocms_content_expiry.constants import CONTENT_EXPIRY_COMPLIANCE_FIELD_LABEL from djangocms_content_expiry.models import ContentExpiry from djangocms_content_expiry.test_utils.factories import ( ChildModerationRequestTreeNodeFactory, @@ -19,6 +20,7 @@ UserFactory, ) from djangocms_content_expiry.test_utils.polls import factories +from djangocms_content_expiry.test_utils.polls.cms_config import PollsCMSConfig class ContentExpiryMonkeyPatchTestCase(CMSTestCase): @@ -49,6 +51,7 @@ def setUp(self): collection_id=self.collection.pk, mr_id=self.moderation_request1.pk, ) + self.versionable = PollsCMSConfig.versioning[0] def test_extended_admin_monkey_patch_list_display_expires(self): """ @@ -65,8 +68,8 @@ def test_extended_admin_monkey_patch_list_display_expires(self): list_display = version_admin.get_list_display(request) # List display field should have been added by monkeypatch - self.assertIn('expire', list_display) - self.assertEqual(CONTENT_EXPIRY_EXPIRE_FIELD_LABEL, version_admin.expire.short_description) + self.assertIn('compliance_number', list_display) + self.assertEqual(CONTENT_EXPIRY_COMPLIANCE_FIELD_LABEL, version_admin.compliance_number.short_description) def test_extended_moderation_admin_update_existing_expiry_record(self): """ @@ -225,3 +228,25 @@ def test_extended_moderation_admin_update_no_compliance_number_record(self): self.assertEqual(ContentExpiry.objects.count(), 2) self.assertEqual(ContentExpiry.objects.first().compliance_number, ContentExpiry.objects.last().compliance_number) + + def test_extended_versioning_admin_additional_content_settings_icon(self): + """ + The additional content settings icon should be added to the version table + """ + endpoint = self.get_admin_url(ContentExpiry, "change", self.content_expiry_primary.pk) + additional_settings_control = render_to_string( + 'djangocms_content_expiry/admin/icons/additional_content_settings_icon.html', + { + "url": f"{endpoint}?_popup=1" + } + ) + + querystring = "?poll=1" + url = self.get_admin_url(self.versionable.version_model_proxy, "changelist") + querystring + + response = self.client.get( + url + ) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, additional_settings_control, html=True)