Skip to content

Commit

Permalink
fix: Compliance number should not be editable when version is not dra…
Browse files Browse the repository at this point in the history
…ft (#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
  • Loading branch information
Bernardvdv authored Jun 29, 2022
1 parent 9e350bb commit df1c7ae
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
==================
Expand Down
13 changes: 12 additions & 1 deletion djangocms_content_expiry/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
76 changes: 75 additions & 1 deletion tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'<input type="text" name="compliance_number" ' \
f'value="{content_expiry.compliance_number}" ' \
f'class="vTextField" maxlength="15" id="id_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(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'<div class="readonly">{content_expiry.compliance_number}</div>'

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'<div class="readonly">{content_expiry.compliance_number}</div>'

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'<div class="readonly">{content_expiry.compliance_number}</div>'

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):
Expand Down

0 comments on commit df1c7ae

Please sign in to comment.