From df1c7ae9d3c98d5076eefb8d2bdcdb51dc41edc1 Mon Sep 17 00:00:00 2001 From: Bernard Van Der Vyver Date: Wed, 29 Jun 2022 16:58:39 +0100 Subject: [PATCH] fix: Compliance number should not be editable when version is not draft (#62) * Compliance number should not be editable when version is not draft * flake error * added tests for unpublished and archived * Making a change to force CI to run * flake, isort --- CHANGELOG.rst | 1 + djangocms_content_expiry/admin.py | 13 +++++- tests/test_admin.py | 76 ++++++++++++++++++++++++++++++- 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8dcce72..f12f4e4 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,7 @@ unreleased ========== * feat: Expiry field removed from version table and replaced with compliance number. Added additional content settings action to version table * fix: Changed model title to uppercase +* fix: Compliance number should not be editable when version is not draft 1.3.1 (2022-06-22) ================== diff --git a/djangocms_content_expiry/admin.py b/djangocms_content_expiry/admin.py index 94844bf..55370ef 100644 --- a/djangocms_content_expiry/admin.py +++ b/djangocms_content_expiry/admin.py @@ -10,7 +10,7 @@ from django.utils.html import format_html_join from django.utils.translation import ugettext_lazy as _ -from djangocms_versioning.constants import PUBLISHED +from djangocms_versioning.constants import DRAFT, PUBLISHED from djangocms_versioning.helpers import get_preview_url from .conf import DEFAULT_CONTENT_EXPIRY_EXPORT_DATE_FORMAT @@ -284,6 +284,17 @@ def get_exported_queryset(self, request): return cl.get_queryset(request) + def get_readonly_fields(self, request, obj=None): + """ + Compliance number should only be editable in draft versions + :param request: Request object + :param obj: Content expiry object + :return: Read-only form fields + """ + if obj and obj.version.state != DRAFT: + return self.readonly_fields + ('compliance_number',) + return self.readonly_fields + @admin.register(DefaultContentExpiryConfiguration) class DefaultContentExpiryConfigurationAdmin(admin.ModelAdmin): diff --git a/tests/test_admin.py b/tests/test_admin.py index 9ed10bd..4928ae2 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -11,7 +11,7 @@ from cms.test_utils.testcases import CMSTestCase from bs4 import BeautifulSoup -from djangocms_versioning.constants import DRAFT, PUBLISHED +from djangocms_versioning.constants import ARCHIVED, DRAFT, PUBLISHED, UNPUBLISHED from djangocms_content_expiry.admin import ContentExpiryAdmin from djangocms_content_expiry.conf import DEFAULT_CONTENT_EXPIRY_EXPORT_DATE_FORMAT @@ -103,6 +103,80 @@ def test_change_form_title(self): self.assertEqual(response.status_code, 200) self.assertEqual(response.context_data['title'], 'Additional Content Settings') + def test_change_form_compliance_number_field_is_editable_when_draft_version(self): + """ + Compliance number field should be an input field and editable when version is draft + """ + content_expiry = PollContentExpiryFactory(version__state=DRAFT) + + endpoint = self.get_admin_url(ContentExpiry, "change", content_expiry.pk) + draft_expected_content = f'' + + with self.login_user_context(self.get_superuser()): + response = self.client.get(endpoint) + + self.assertEqual(response.status_code, 200) + + decoded_response = response.content.decode("utf-8") + + self.assertIn(draft_expected_content, decoded_response) + + def test_change_form_compliance_number_field_is_not_editable_in_published_version(self): + """ + Compliance number field should not be editable when version is published + """ + content_expiry = PollContentExpiryFactory(version__state=PUBLISHED) + + endpoint = self.get_admin_url(ContentExpiry, "change", content_expiry.pk) + published_expected_response = f'
{content_expiry.compliance_number}
' + + with self.login_user_context(self.get_superuser()): + response = self.client.get(endpoint) + + self.assertEqual(response.status_code, 200) + + decoded_response = response.content.decode("utf-8") + + self.assertIn(published_expected_response, decoded_response) + + def test_change_form_compliance_number_field_is_not_editable_in_archived_version(self): + """ + Compliance number field should not be editable when version is archived + """ + content_expiry = PollContentExpiryFactory(version__state=ARCHIVED) + + endpoint = self.get_admin_url(ContentExpiry, "change", content_expiry.pk) + archived_expected_response = f'
{content_expiry.compliance_number}
' + + with self.login_user_context(self.get_superuser()): + response = self.client.get(endpoint) + + self.assertEqual(response.status_code, 200) + + decoded_response = response.content.decode("utf-8") + + self.assertIn(archived_expected_response, decoded_response) + + def test_change_form_compliance_number_field_is_not_editable_in_unpublished_version(self): + """ + Compliance number field should not be editable when version is unpublished + """ + content_expiry = PollContentExpiryFactory(version__state=UNPUBLISHED) + + endpoint = self.get_admin_url(ContentExpiry, "change", content_expiry.pk) + unpublished_expected_response = f'
{content_expiry.compliance_number}
' + + with self.login_user_context(self.get_superuser()): + response = self.client.get(endpoint) + + self.assertEqual(response.status_code, 200) + + decoded_response = response.content.decode("utf-8") + + self.assertIn(unpublished_expected_response, decoded_response) + class ContentExpiryChangelistTestCase(CMSTestCase): def setUp(self):