Skip to content

Commit

Permalink
feat: initial fix for django 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
joshyu committed Mar 6, 2024
1 parent 539c0df commit bd85f4d
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 53 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Unreleased
==========

* fix: Treebeard support improved by inheriting a treebeard template
* Python 3.10 support added
* Python 3.7 support removed
* Django 4.2 support added
* Django CMS 4.1 support added

2.1.6 (2022-09-07)
==================
Expand Down
1 change: 0 additions & 1 deletion djangocms_moderation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
__version__ = "2.1.6"

Check warning on line 2 in djangocms_moderation/__init__.py

View workflow job for this annotation

GitHub Actions / flake8

blank line at end of file
default_app_config = "djangocms_moderation.apps.ModerationConfig"
78 changes: 54 additions & 24 deletions djangocms_moderation/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,17 @@ def has_add_permission(self, request):
def has_delete_permission(self, request, obj=None):
return False

@admin.display(
description=_("Status")
)
def show_user(self, obj):
_name = obj.get_by_user_name()
return gettext("By {user}").format(user=_name)

show_user.short_description = _("Status")

@admin.display(

Check failure on line 84 in djangocms_moderation/admin.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)
description=_("Form Submission")
)
def form_submission(self, obj):
instance = get_form_submission_for_step(
obj.moderation_request, obj.step_approved
Expand All @@ -96,7 +101,6 @@ def form_submission(self, obj):
'<a href="{}" target="_blank">{}</a>', url, obj.step_approved.role.name
)

form_submission.short_description = _("Form Submission")

def get_readonly_fields(self, request, obj=None):

Check failure on line 105 in djangocms_moderation/admin.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)
if obj.user_can_moderate(request.user) or obj.user_is_author(request.user):
Expand All @@ -107,6 +111,7 @@ def get_readonly_fields(self, request, obj=None):
return self.fields


@admin.register(ModerationRequestTreeNode)
class ModerationRequestTreeAdmin(TreeAdmin):
"""
This admin is purely for the change list of Moderation Requests using the treebeard nodes to
Expand Down Expand Up @@ -177,14 +182,16 @@ def get_list_display(self, request):
]
return list_display

@admin.display(
description=_("actions")
)
def list_display_actions(self, obj):
"""Display links to state change endpoints
"""
return format_html_join(
"", "{}", ((action(obj),) for action in self.get_list_display_actions())
)

list_display_actions.short_description = _("actions")

def get_list_display_actions(self):

Check failure on line 196 in djangocms_moderation/admin.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)
actions = []
Expand All @@ -210,6 +217,9 @@ def _get_configured_fields(self, request):

return fields

@admin.display(
description=_('ID')
)
def get_id(self, obj):
return format_html(
'<a href="{url}">{id}</a>',
Expand All @@ -219,22 +229,30 @@ def get_id(self, obj):
),
id=obj.moderation_request_id,
)
get_id.short_description = _('ID')

@admin.display(
description=_('Content type')
)
def get_content_type(self, obj):
return ContentType.objects.get_for_model(
obj.moderation_request.version.versionable.grouper_model
)
get_content_type.short_description = _('Content type')

@admin.display(
description=_('Title')
)
def get_title(self, obj):
return obj.moderation_request.version.content
get_title.short_description = _('Title')

@admin.display(
description=_('Author')
)
def get_version_author(self, obj):
return obj.moderation_request.version.created_by
get_version_author.short_description = _('Author')

@admin.display(
description=_("Preview")
)
def get_preview_link(self, obj):
content = obj.moderation_request.version.content
if is_editable_model(content.__class__):
Expand All @@ -254,8 +272,10 @@ def get_preview_link(self, obj):
object_preview_url,
)

get_preview_link.short_description = _("Preview")

@admin.display(

Check failure on line 276 in djangocms_moderation/admin.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)
description=_('Reviewer')
)
def get_reviewer(self, obj):
last_action = obj.moderation_request.get_last_action()
if not last_action:
Expand All @@ -264,7 +284,6 @@ def get_reviewer(self, obj):
next_step = obj.moderation_request.get_next_required()
return next_step.role.name
return last_action._get_user_name(last_action.by_user)
get_reviewer.short_description = _('Reviewer')

def get_status(self, obj):
# We can have moderation requests without any action (e.g. the
Expand Down Expand Up @@ -466,6 +485,7 @@ def _traverse_moderation_nodes(node_item):
return HttpResponseRedirect(redirect_url)


@admin.register(ModerationRequest)
class ModerationRequestAdmin(admin.ModelAdmin):
class Media:
js = ('admin/js/jquery.init.js', 'djangocms_moderation/js/actions.js',)
Expand Down Expand Up @@ -813,11 +833,13 @@ def changelist_view(self, request, extra_context=None):
return tree_node_admin.changelist_view(request, extra_context)


@admin.register(Role)
class RoleAdmin(admin.ModelAdmin):
list_display = ["name", "user", "group", "confirmation_page"]
fields = ["name", "user", "group", "confirmation_page"]


@admin.register(CollectionComment)
class CollectionCommentAdmin(admin.ModelAdmin):
list_display = ["date_created", "message", "author"]
fields = ["collection", "message", "author"]
Expand Down Expand Up @@ -900,17 +922,20 @@ def get_readonly_fields(self, request, obj=None):
return self.list_display


@admin.register(RequestComment)
class RequestCommentAdmin(admin.ModelAdmin):
list_display = ["date_created", "message", "get_author"]
fields = ["moderation_request", "message", "author"]

class Media:
css = {"all": ("djangocms_moderation/css/comments_changelist.css",)}

@admin.display(
description=_("User")
)
def get_author(self, obj):
return obj.author_name

get_author.short_description = _("User")

def get_changeform_initial_data(self, request):

Check failure on line 940 in djangocms_moderation/admin.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)
data = {"author": request.user}
Expand Down Expand Up @@ -990,6 +1015,7 @@ def get_extra(self, request, obj=None, **kwargs):
return 1


@admin.register(Workflow)
class WorkflowAdmin(admin.ModelAdmin):
inlines = [WorkflowStepInline]
list_display = ["name", "is_default"]
Expand All @@ -1002,6 +1028,7 @@ class WorkflowAdmin(admin.ModelAdmin):
]


@admin.register(ModerationCollection)
class ModerationCollectionAdmin(admin.ModelAdmin):
class Media:
js = ("admin/js/jquery.init.js", "djangocms_moderation/js/actions.js",)
Expand Down Expand Up @@ -1033,19 +1060,23 @@ def get_list_display(self, request):
def job_id(self, obj):
return obj.pk

@admin.display(
description=_('reviewers')
)
def commaseparated_reviewers(self, obj):
reviewers = self.model.objects.reviewers(obj)
return ", ".join(map(get_user_model().get_full_name, reviewers))
commaseparated_reviewers.short_description = _('reviewers')

@admin.display(
description=_("actions")
)
def list_display_actions(self, obj):
"""Display links to state change endpoints
"""
return format_html_join(
"", "{}", ((action(obj),) for action in self.get_list_display_actions())
)

list_display_actions.short_description = _("actions")

def get_list_display_actions(self):

Check failure on line 1081 in djangocms_moderation/admin.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)
actions = [self.get_edit_link, self.get_requests_link]
Expand Down Expand Up @@ -1136,6 +1167,7 @@ def has_delete_permission(self, request, obj=None):
return False


@admin.register(ConfirmationPage)
class ConfirmationPageAdmin(PlaceholderAdminMixin, admin.ModelAdmin):
view_on_site = True

Expand All @@ -1153,6 +1185,7 @@ def _url(regex, fn, name, **kwargs):
return url_patterns + super().get_urls()


@admin.register(ConfirmationFormSubmission)
class ConfirmationFormSubmissionAdmin(admin.ModelAdmin):
list_display = ["moderation_request", "for_step", "submitted_at"]
fields = [
Expand All @@ -1178,16 +1211,23 @@ def change_view(self, request, object_id, form_url="", extra_context=None):
request, object_id, form_url, extra_context=extra_context
)

@admin.display(
description=_("Request")
)
def moderation_request(self, obj):
return obj.moderation_request_id

moderation_request.short_description = _("Request")

@admin.display(

Check failure on line 1221 in djangocms_moderation/admin.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)
description=_("By User")
)
def show_user(self, obj):
return obj.get_by_user_name()

show_user.short_description = _("By User")

@admin.display(

Check failure on line 1228 in djangocms_moderation/admin.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)
description=_("Form Data")
)
def form_data(self, obj):
data = obj.get_form_data()
return format_html_join(
Expand All @@ -1199,15 +1239,5 @@ def form_data(self, obj):
),
)

form_data.short_description = _("Form Data")


Check warning on line 1243 in djangocms_moderation/admin.py

View workflow job for this annotation

GitHub Actions / flake8

blank line at end of file
admin.site.register(ModerationRequestTreeNode, ModerationRequestTreeAdmin)
admin.site.register(ModerationRequest, ModerationRequestAdmin)
admin.site.register(CollectionComment, CollectionCommentAdmin)
admin.site.register(RequestComment, RequestCommentAdmin)
admin.site.register(ModerationCollection, ModerationCollectionAdmin)
admin.site.register(Role, RoleAdmin)
admin.site.register(Workflow, WorkflowAdmin)
admin.site.register(ConfirmationPage, ConfirmationPageAdmin)
admin.site.register(ConfirmationFormSubmission, ConfirmationFormSubmissionAdmin)
4 changes: 2 additions & 2 deletions djangocms_moderation/admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ def add_items_to_collection(modeladmin, request, queryset):
args=(),
),
version_ids=",".join(version_ids),
return_to_url=request.META.get("HTTP_REFERER", ""),
return_to_url=request.headers.get("referer", ""),
)
return HttpResponseRedirect(admin_url)
else:
modeladmin.message_user(
request, _("No suitable items found to add to moderation collection")
)
return HttpResponseRedirect(request.META.get("HTTP_REFERER", ""))
return HttpResponseRedirect(request.headers.get("referer", ""))


add_items_to_collection.short_description = _("Add to moderation collection")
Expand Down
2 changes: 1 addition & 1 deletion djangocms_moderation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.http import is_safe_url
from django.utils.http import url_has_allowed_host_and_scheme

Check failure on line 9 in djangocms_moderation/views.py

View workflow job for this annotation

GitHub Actions / flake8

'django.utils.http.url_has_allowed_host_and_scheme' imported but unused
from django.utils.translation import gettext_lazy as _, ngettext
from django.views.generic import FormView

Expand Down
7 changes: 7 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import django


if django.VERSION < (4, 2): # TODO: remove when dropping support for Django < 4.2
from django.test.testcases import TransactionTestCase

TransactionTestCase.assertQuerySetEqual = TransactionTestCase.assertQuerysetEqual
12 changes: 6 additions & 6 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_form_init_approved_action(self):
)
field_moderator = form.fields["moderator"]
self.assertEqual(field_moderator.empty_label, "Any Role 2")
self.assertQuerysetEqual(
self.assertQuerySetEqual(
field_moderator.queryset,
User.objects.filter(pk__in=[self.user2.pk]),
transform=lambda x: x,
Expand All @@ -48,7 +48,7 @@ def test_form_init_cancelled_action(self):
active_request=self.moderation_request1,
)
field_moderator = form.fields["moderator"]
self.assertQuerysetEqual(field_moderator.queryset, User.objects.none())
self.assertQuerySetEqual(field_moderator.queryset, User.objects.none())
self.assertIsInstance(field_moderator.widget, HiddenInput)

def test_form_init_rejected_action(self):
Expand All @@ -61,7 +61,7 @@ def test_form_init_rejected_action(self):
active_request=self.moderation_request1,
)
field_moderator = form.fields["moderator"]
self.assertQuerysetEqual(field_moderator.queryset, User.objects.none())
self.assertQuerySetEqual(field_moderator.queryset, User.objects.none())
self.assertIsInstance(field_moderator.widget, HiddenInput)

def test_form_save(self):
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_add_items_to_collection(self):
form = CollectionItemsForm(data=data, user=self.user)
self.assertTrue(form.is_valid())
versions = form.clean_versions()
self.assertQuerysetEqual(
self.assertQuerySetEqual(
versions,
Version.objects.filter(pk__in=[pg1_version.pk, pg2_version.pk]),
transform=lambda x: x,
Expand All @@ -172,7 +172,7 @@ def test_attempt_add_with_item_already_in_collection(self):
form = CollectionItemsForm(data=data, user=self.user)
self.assertTrue(form.is_valid())
versions = form.clean_versions()
self.assertQuerysetEqual(
self.assertQuerySetEqual(
versions,
Version.objects.filter(pk__in=[pg_version.pk]),
transform=lambda x: x,
Expand Down Expand Up @@ -253,7 +253,7 @@ def test_collection_choice_should_be_limited_to_current_user_and_collecting_stat
if not form.is_valid():
self.assertIn("collection", form.errors)

self.assertQuerysetEqual(
self.assertQuerySetEqual(
form.fields["collection"].queryset,
ModerationCollection.objects.filter(
pk__in=[collection1.pk, collection2.pk]
Expand Down
Loading

0 comments on commit bd85f4d

Please sign in to comment.