Skip to content

Commit

Permalink
feat: Removed expiry date from version table (#59)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Bernardvdv authored Jun 24, 2022
1 parent e57ee39 commit 5feacb8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 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: 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)
==================
Expand Down
1 change: 1 addition & 0 deletions djangocms_content_expiry/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
50 changes: 43 additions & 7 deletions djangocms_content_expiry/monkeypatch/admin.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load static i18n %}

<a class="btn cms-versioning-action-btn related-widget-wrapper-link" href="{{ url }}" title="{% trans 'Additional content settings' %}">
<img src="{% static 'djangocms_content_expiry/svg/file.svg' %}">
</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/file.svg' %}" /></span></a>
<a class="btn cms-moderation-action-btn js-moderation-action related-widget-wrapper-link cms-versioning-action-btn" 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>
31 changes: 28 additions & 3 deletions tests/test_monkeypatch.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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)

0 comments on commit 5feacb8

Please sign in to comment.