Skip to content

Commit

Permalink
feat: Copy compliance number (#57)
Browse files Browse the repository at this point in the history
* Release 1.2.1

* Adding functionality to copy a compliance number to all items in a collection

* Added test cases for compliance number copy function

* Changed icon label

* Changed Additional content settings icon and addressed feedback

* Update tests/test_monkeypatch.py

Co-authored-by: Adam Murray <[email protected]>

Co-authored-by: Adam Murray <[email protected]>
  • Loading branch information
Bernardvdv and adam-murray authored Jun 22, 2022
1 parent 1dbf267 commit 521268e
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog

unreleased
==========
* feat: Adding functionality to copy a compliance number to all items in a collection

1.2.1 (2022-06-20)
==================
Expand Down
26 changes: 26 additions & 0 deletions djangocms_content_expiry/cms_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ def content_expiry_site_alias_excluded_set(queryset, request):
)


def get_copy_compliance_number_button(obj):
"""
Return a user friendly link to copy a content expiry compliance number to other Moderation Request Items
link redirects to view which handles this
"""
version = obj.moderation_request.version

if hasattr(version, "contentexpiry"):
content_expiry = version.contentexpiry
view_endpoint = format_html(
"{}?collection__id={}&moderation_request__id={}&_popup=1&copy=compliance",
reverse("admin:djangocms_moderation_moderationrequesttreenode_copy"),
obj.moderation_request.collection.pk,
obj.moderation_request.pk,
)
return render_to_string(
"djangocms_content_expiry/admin/icons/compliance_copy_icon.html", {
"url": view_endpoint,
"content_expiry_id": f"content_expiry_{content_expiry.pk}",
"moderation_request_id": f"moderation_request_{obj.moderation_request.pk}"
}
)
return ""


class ContentExpiryExtension(CMSAppExtension):
def __init__(self):
self.expiry_changelist_queryset_filters = []
Expand All @@ -173,6 +198,7 @@ class ContentExpiryAppConfig(CMSAppConfig):
moderation_request_changelist_actions = [
get_moderation_content_expiry_link,
get_copy_content_expiry_button,
get_copy_compliance_number_button,
]
moderation_request_changelist_fields = [
get_expiry_date,
Expand Down
17 changes: 14 additions & 3 deletions djangocms_content_expiry/monkeypatch/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def inner(self, *args, **kwargs):


def copy_content_expiry_view(self, request):
"""
Copy expiration date and compliance number to all items in a collection
:param request:
:return: Redirect to the moderation changelist to ensure a page reload occurs
"""
compliance_copy = request.GET.get("copy", None)
collection_id = request.GET.getlist("collection__id")
collection_id = int(collection_id[0])
moderation_request_id = request.GET.getlist("moderation_request__id")
Expand All @@ -78,9 +84,14 @@ def copy_content_expiry_view(self, request):
for mr in collection.moderation_requests.all():
mr_version = mr.version
if hasattr(mr_version, "contentexpiry"):
mr_content_expiry = mr_version.contentexpiry
mr_content_expiry.expires = content_expiry.expires
mr_content_expiry.save()
if compliance_copy == "compliance":
mr_content_expiry = mr_version.contentexpiry
mr_content_expiry.compliance_number = content_expiry.compliance_number
mr_content_expiry.save()
else:
mr_content_expiry = mr_version.contentexpiry
mr_content_expiry.expires = content_expiry.expires
mr_content_expiry.save()
else:
ContentExpiry.objects.create(
created_by=request.user,
Expand Down

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% load static i18n %}
<a class="btn cms-moderation-action-btn js-moderation-action" id="copy_content_expiry_id_{{ field_id }}" href="{{ url }}" title="{% trans 'Copy Compliance Number' %}"><span class="svg-juxtaposed-font"><img src="{% static 'djangocms_content_expiry/svg/clone.svg' %}" /></span></a>
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{% load static i18n %}
<a class="btn cms-moderation-action-btn js-moderation-action related-widget-wrapper-link" id="change_content_expiry_id_{{ field_id }}" href="{{ url }}" title="{% trans 'Additional content settings' %}"><span class="svg-juxtaposed-font"><img src="{% static 'djangocms_content_expiry/svg/calendar.svg' %}" /></span></a>
<a class="btn cms-moderation-action-btn js-moderation-action related-widget-wrapper-link" id="change_content_expiry_id_{{ field_id }}" href="{{ url }}" title="{% trans 'Additional content settings' %}"><span class="svg-juxtaposed-font"><img src="{% static 'djangocms_content_expiry/svg/file.svg' %}" /></span></a>
69 changes: 69 additions & 0 deletions tests/test_monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,72 @@ def test_extended_moderation_admin_no_redirect_invalid_mr_to_copy(self):
self.assertEqual(ContentExpiry.objects.count(), 1)
# We should still be redirected
self.assertEqual(response.status_code, 302)

def test_extended_moderation_admin_update_existing_compliance_number_record(self):
"""
If the target of the copy already has a compliance number, the record should be updated, rather than recreated
"""
# Create db data for additional content expiry!
self.content_expiry_secondary = factories.PollContentExpiryFactory(
expires=self.expires_secondary,
version__state=PUBLISHED
)
self.moderation_request2 = ModerationRequestFactory(
collection=self.collection,
version=self.content_expiry_secondary.version,
)
self.root2 = RootModerationRequestTreeNodeFactory(
moderation_request=self.moderation_request2
)
ChildModerationRequestTreeNodeFactory(
moderation_request=self.moderation_request1, parent=self.root1
)

# Check that compliance numbers are different before we hit the copy endpoint!
self.assertEqual(ContentExpiry.objects.count(), 2)
self.assertNotEqual(ContentExpiry.objects.first().compliance_number,
ContentExpiry.objects.last().compliance_number)

response = self.client.post(self.url + "&copy=compliance")

# Ensure request is a redirect as expected!
self.assertEqual(response.status_code, 302)
# Since we already have two content expiry records, we should see an update rather than a creation
self.assertEqual(ContentExpiry.objects.count(), 2)
self.assertEqual(ContentExpiry.objects.first().compliance_number,
ContentExpiry.objects.last().compliance_number)

def test_extended_moderation_admin_update_no_compliance_number_record(self):
"""
If the user copies a content expiry to a moderation request which does not have a compliance number associated
with it, one should be not be created as it is not required
"""
content_expiry = factories.PollContentExpiryFactory(
expires=self.expires_secondary,
version__state=PUBLISHED,
compliance_number="",
)
moderation_request = ModerationRequestFactory(
collection=self.collection,
version=content_expiry.version,
)
RootModerationRequestTreeNodeFactory(
moderation_request=moderation_request
)

# Check that compliance numbers are not set before we hit the copy endpoint!
content_expiry_record = ContentExpiry.objects.first()
# Removing the compliance number set in the setup
content_expiry_record.compliance_number = ""
# Both records should not contain a compliance number as it has not been set
self.assertEqual(content_expiry_record.compliance_number, "")
self.assertEqual(ContentExpiry.objects.last().compliance_number, "")

response = self.client.post(self.url + "&copy=compliance")

# Ensure request is a redirect as expected!
self.assertEqual(response.status_code, 302)
# The compliance number has not been added so should not be created in the copied versions
self.assertEqual(ContentExpiry.objects.count(), 2)
self.assertEqual(ContentExpiry.objects.first().compliance_number,
ContentExpiry.objects.last().compliance_number)

0 comments on commit 521268e

Please sign in to comment.