Skip to content

Commit

Permalink
Fix tests (#349)
Browse files Browse the repository at this point in the history
* fix tests

* fix linting

---------

Co-authored-by: Fabian Braun <fsbraun@gmx.de>
  • Loading branch information
FinalAngel and fsbraun authored Sep 11, 2023
1 parent ad24c93 commit ca50367
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 108 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 @@ Changelog
Unreleased
==========
* feat: Reversable generic foreign key lookup from version
* fix: formatted files through ruff to fix tests
* fix: Remove version check when evaluating CMS PageContent objects

2.0.0rc1
Expand Down
56 changes: 13 additions & 43 deletions djangocms_versioning/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@ def _get_edit_link(self, obj, request, disabled=False):
disabled = True

url = reverse(
"admin:{app}_{model}_edit_redirect".format(
app=version._meta.app_label, model=version._meta.model_name
),
f"admin:{version._meta.app_label}_{version._meta.model_name}_edit_redirect",
args=(version.pk,),
)
return self.admin_action_button(
Expand Down Expand Up @@ -676,9 +674,7 @@ def _get_archive_link(self, obj, request, disabled=False):
# Don't display the link if it can't be archived
return ""
archive_url = reverse(
"admin:{app}_{model}_archive".format(
app=obj._meta.app_label, model=self.model._meta.model_name
),
f"admin:{obj._meta.app_label}_{self.model._meta.model_name}_archive",
args=(obj.pk,),
)
return self.admin_action_button(
Expand All @@ -696,9 +692,7 @@ def _get_publish_link(self, obj, request):
# Don't display the link if it can't be published
return ""
publish_url = reverse(
"admin:{app}_{model}_publish".format(
app=obj._meta.app_label, model=self.model._meta.model_name
),
f"admin:{obj._meta.app_label}_{self.model._meta.model_name}_publish",
args=(obj.pk,),
)
return self.admin_action_button(
Expand All @@ -718,9 +712,7 @@ def _get_unpublish_link(self, obj, request, disabled=False):
# Don't display the link if it can't be unpublished
return ""
unpublish_url = reverse(
"admin:{app}_{model}_unpublish".format(
app=obj._meta.app_label, model=self.model._meta.model_name
),
f"admin:{obj._meta.app_label}_{self.model._meta.model_name}_unpublish",
args=(obj.pk,),
)
return self.admin_action_button(
Expand Down Expand Up @@ -757,9 +749,7 @@ def _get_edit_link(self, obj, request, disabled=False):
keepsideframe = obj.versionable.content_model_is_sideframe_editable

edit_url = reverse(
"admin:{app}_{model}_edit_redirect".format(
app=obj._meta.app_label, model=self.model._meta.model_name
),
f"admin:{obj._meta.app_label}_{self.model._meta.model_name}_edit_redirect",
args=(obj.pk,),
)
return self.admin_action_button(
Expand All @@ -780,9 +770,7 @@ def _get_revert_link(self, obj, request, disabled=False):
return ""

revert_url = reverse(
"admin:{app}_{model}_revert".format(
app=obj._meta.app_label, model=self.model._meta.model_name
),
f"admin:{obj._meta.app_label}_{self.model._meta.model_name}_revert",
args=(obj.pk,),
)
return self.admin_action_button(
Expand All @@ -801,9 +789,7 @@ def _get_discard_link(self, obj, request, disabled=False):
return ""

discard_url = reverse(
"admin:{app}_{model}_discard".format(
app=obj._meta.app_label, model=self.model._meta.model_name
),
f"admin:{obj._meta.app_label}_{self.model._meta.model_name}_discard",
args=(obj.pk,),
)
return self.admin_action_button(
Expand All @@ -828,9 +814,7 @@ def _get_unlock_link(self, obj, request):
if request.user.has_perm("djangocms_versioning.delete_versionlock"):
disabled = False

unlock_url = reverse("admin:{app}_{model}_unlock".format(
app=obj._meta.app_label, model=self.model._meta.model_name,
), args=(obj.pk,))
unlock_url = reverse(f"admin:{obj._meta.app_label}_{self.model._meta.model_name}_unlock", args=(obj.pk,))
return self.admin_action_button(
unlock_url,
icon="unlock",
Expand Down Expand Up @@ -884,9 +868,7 @@ def compare_versions(self, request, queryset):

# Build the link for the version comparison of the two selected versions
url = reverse(
"admin:{app}_{model}_compare".format(
app=self.model._meta.app_label, model=self.model._meta.model_name
),
f"admin:{self.model._meta.app_label}_{self.model._meta.model_name}_compare",
args=(queryset[0].pk,),
)
url += "?compare_to=%d" % queryset[1].pk
Expand Down Expand Up @@ -932,10 +914,7 @@ def archive_view(self, request, object_id):
"version_number": version.number,
"object_id": object_id,
"archive_url": reverse(
"admin:{app}_{model}_archive".format(
app=self.model._meta.app_label,
model=self.model._meta.model_name,
),
f"admin:{self.model._meta.app_label}_{self.model._meta.model_name}_archive",
args=(version.content.pk,),
),
"back_url": self.back_link(request, version),
Expand Down Expand Up @@ -1012,10 +991,7 @@ def unpublish_view(self, request, object_id):
"version_number": version.number,
"object_id": object_id,
"unpublish_url": reverse(
"admin:{app}_{model}_unpublish".format(
app=self.model._meta.app_label,
model=self.model._meta.model_name,
),
f"admin:{self.model._meta.app_label}_{self.model._meta.model_name}_unpublish",
args=(version.content.pk,),
),
"back_url": self.back_link(request, version),
Expand Down Expand Up @@ -1130,10 +1106,7 @@ def revert_view(self, request, object_id):
"draft_version": draft_version,
"object_id": object_id,
"revert_url": reverse(
"admin:{app}_{model}_revert".format(
app=self.model._meta.app_label,
model=self.model._meta.model_name,
),
f"admin:{self.model._meta.app_label}_{self.model._meta.model_name}_revert",
args=(version.content.pk,),
),
"back_url": self.back_link(request, version),
Expand Down Expand Up @@ -1172,10 +1145,7 @@ def discard_view(self, request, object_id):
"draft_version": version,
"object_id": object_id,
"revert_url": reverse(
"admin:{app}_{model}_revert".format(
app=self.model._meta.app_label,
model=self.model._meta.model_name,
),
f"admin:{self.model._meta.app_label}_{self.model._meta.model_name}_revert",
args=(version.content.pk,),
),
"back_url": self.back_link(request, version),
Expand Down
8 changes: 2 additions & 6 deletions djangocms_versioning/cms_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ def handle_versioning_setting(self, cms_config):
for versionable in cms_config.versioning:
if not isinstance(versionable, BaseVersionableItem):
raise ImproperlyConfigured(
"{!r} is not a subclass of djangocms_versioning.datastructures.BaseVersionableItem".format(
versionable
)
f"{versionable!r} is not a subclass of djangocms_versioning.datastructures.BaseVersionableItem"
)
# NOTE: Do not use the cached property here as this is
# still changing and needs to be calculated on the fly
Expand All @@ -107,9 +105,7 @@ def handle_versioning_add_to_confirmation_context_setting(self, cms_config):
for key, value in add_to_context.items():
if key not in supported_keys:
raise ImproperlyConfigured(
"{!r} is not a supported dict key in the versioning_add_to_confirmation_context setting".format(
key
)
f"{key!r} is not a supported dict key in the versioning_add_to_confirmation_context setting"
)
if key not in self.add_to_context:
self.add_to_context[key] = collections.OrderedDict()
Expand Down
31 changes: 12 additions & 19 deletions djangocms_versioning/cms_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def _add_publish_button(self):
proxy_model = self._get_proxy_model()
version = Version.objects.get_for_content(self.toolbar.obj)
publish_url = reverse(
"admin:{app}_{model}_publish".format(
app=proxy_model._meta.app_label, model=proxy_model.__name__.lower()
),
f"admin:{proxy_model._meta.app_label}_{proxy_model.__name__.lower()}_publish",
args=(version.pk,),
)
item.add_button(
Expand Down Expand Up @@ -103,9 +101,7 @@ def _add_edit_button(self, disabled=False):
version = Version.objects.get_for_content(self.toolbar.obj)
if version.check_edit_redirect.as_bool(self.request.user):
edit_url = reverse(
"admin:{app}_{model}_edit_redirect".format(
app=proxy_model._meta.app_label, model=proxy_model.__name__.lower()
),
f"admin:{proxy_model._meta.app_label}_{proxy_model.__name__.lower()}_edit_redirect",
args=(version.pk,),
)
pks_for_grouper = version.versionable.for_content_grouping_values(
Expand All @@ -132,9 +128,7 @@ def _add_unlock_button(self):
version = Version.objects.get_for_content(self.toolbar.obj)
if version.check_unlock.as_bool(self.request.user):
unlock_url = reverse(
"admin:{app}_{model}_unlock".format(
app=proxy_model._meta.app_label, model=proxy_model.__name__.lower()
),
f"admin:{proxy_model._meta.app_label}_{proxy_model.__name__.lower()}_unlock",
args=(version.pk,),
)
can_unlock = self.request.user.has_perm("djangocms_versioning.delete_versionlock")
Expand Down Expand Up @@ -176,10 +170,7 @@ def _add_revert_button(self, disabled=False):
version = Version.objects.get_for_content(self.toolbar.obj)
if version.check_revert.as_bool(self.request.user):
revert_url = reverse(
"admin:{app}_{model}_revert".format(
app=proxy_model._meta.app_label,
model=proxy_model._meta.model_name,
),
f"admin:{proxy_model._meta.app_label}_{proxy_model._meta.model_name}_revert",
args=(version.pk,),
)
item.add_button(
Expand Down Expand Up @@ -218,9 +209,10 @@ def _add_versioning_menu(self):
if version.source:
name = _("Compare to {source}").format(source=_(version.source.short_name()))
proxy_model = self._get_proxy_model()
url = reverse("admin:{app}_{model}_compare".format(
app=proxy_model._meta.app_label, model=proxy_model.__name__.lower()
), args=(version.source.pk,))
url = reverse(
f"admin:{proxy_model._meta.app_label}_{proxy_model.__name__.lower()}_compare",
args=(version.source.pk,)
)

url += "?" + urlencode({
"compare_to": version.pk,
Expand All @@ -232,9 +224,10 @@ def _add_versioning_menu(self):
versioning_menu.add_item(Break())
versioning_menu.add_link_item(
_("Discard Changes"),
url=reverse("admin:{app}_{model}_discard".format(
app=proxy_model._meta.app_label, model=proxy_model.__name__.lower()
), args=(version.pk,))
url=reverse(
f"admin:{proxy_model._meta.app_label}_{proxy_model.__name__.lower()}_discard",
args=(version.pk,)
)
)

def _get_published_page_version(self):
Expand Down
16 changes: 4 additions & 12 deletions djangocms_versioning/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ def register_versionadmin_proxy(versionable, admin_site=None):
if versionable.version_model_proxy in admin_site._registry:
# Attempting to register the proxy again is a no-op.
warnings.warn(
"{!r} is already registered with admin.".format(
versionable.version_model_proxy
),
f"{versionable.version_model_proxy!r} is already registered with admin.",
UserWarning,
stacklevel=2
)
Expand Down Expand Up @@ -181,9 +179,7 @@ def _version_list_url(versionable, **params):
proxy = versionable.version_model_proxy
return add_url_parameters(
admin_reverse(
"{app}_{model}_changelist".format(
app=proxy._meta.app_label, model=proxy._meta.model_name
)
f"{proxy._meta.app_label}_{proxy._meta.model_name}_changelist"
),
**params
)
Expand Down Expand Up @@ -238,9 +234,7 @@ def get_editable_url(content_obj):
# Or else, the standard edit view should be used
else:
url = admin_reverse(
"{app}_{model}_change".format(
app=content_obj._meta.app_label, model=content_obj._meta.model_name
),
f"{content_obj._meta.app_label}_{content_obj._meta.model_name}_change",
args=(content_obj.pk,),
)
return url
Expand Down Expand Up @@ -274,9 +268,7 @@ def get_preview_url(content_obj: models.Model, language: typing.Union[str, None]
# Or else, the standard change view should be used
else:
url = admin_reverse(
"{app}_{model}_change".format(
app=content_obj._meta.app_label, model=content_obj._meta.model_name
),
f"{content_obj._meta.app_label}_{content_obj._meta.model_name}_change",
args=[content_obj.pk],
)
if language:
Expand Down
12 changes: 3 additions & 9 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,7 @@ def test_content_link_editable_object(self):
)
self.assertEqual(
self.site._registry[Version].content_link(version),
'<a target="_top" class="js-close-sideframe" href="{url}">{label}</a>'.format(
url=preview_url, label=version.content
),
f'<a target="_top" class="js-close-sideframe" href="{preview_url}">{version.content}</a>',
)

def test_content_link_non_editable_object_with_preview_url(self):
Expand Down Expand Up @@ -2324,9 +2322,7 @@ def test_changelist_view_displays_correct_breadcrumbs_for_extra_grouping_values(
url = self.get_admin_url(versionable.version_model_proxy, "changelist")
# Specify English here - this should mean the version picked up
# for the breadcrumbs is the English one, not the French one
url += "?page={page_id}&language=en".format(
page_id=str(page_content_en.page_id)
)
url += f"?page={str(page_content_en.page_id)}&language=en"

with self.login_user_context(self.superuser):
response = self.client.get(url)
Expand All @@ -2351,9 +2347,7 @@ def test_changelist_view_redirects_on_url_params_that_arent_grouping_params(self
page_content = factories.PageContentWithVersionFactory()
versionable = VersioningCMSConfig.versioning[0]
url = self.get_admin_url(versionable.version_model_proxy, "changelist")
url += "?title={title}&page={page_id}".format(
title=page_content.title, page_id=str(page_content.page_id)
)
url += f"?title={page_content.title}&page={str(page_content.page_id)}"

with self.login_user_context(self.superuser):
response = self.client.get(url)
Expand Down
5 changes: 1 addition & 4 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ def test_grouper_selector_non_default_label(self):
)
version.publish(version.created_by)
form_class = grouper_form_factory(PageContent, version.content.language)
label = "{title} (/{path}/)".format(
title=version.content.title,
path=version.content.page.get_path(version.content.language),
)
label = f"{version.content.title} (/{version.content.page.get_path(version.content.language)}/)"
self.assertIn(
(version.content.page.pk, label), form_class.base_fields["page"].choices
)
16 changes: 4 additions & 12 deletions tests/test_locking.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,7 @@ def test_notify_version_author_version_unlocked_email_sent_for_different_user(se
title=draft_version.content,
description=_("Unlocked"),
)
expected_body = "The following draft version has been unlocked by {by_user} for their use.".format(
by_user=self.user_has_unlock_perms
)
expected_body = f"The following draft version has been unlocked by {self.user_has_unlock_perms} for their use."
expected_version_url = get_full_url(
get_object_preview_url(draft_version.content)
)
Expand Down Expand Up @@ -600,9 +598,7 @@ def test_notify_version_author_version_unlocked_email_contents_users_full_name_u
with self.login_user_context(user):
self.client.post(draft_unlock_url, follow=True)

expected_body = "The following draft version has been unlocked by {by_user} for their use.".format(
by_user=user.get_full_name()
)
expected_body = f"The following draft version has been unlocked by {user.get_full_name()} for their use."

self.assertEqual(len(mail.outbox), 1)
self.assertIn(expected_body, mail.outbox[0].body)
Expand All @@ -623,9 +619,7 @@ def test_notify_version_author_version_unlocked_email_contents_users_username_us
with self.login_user_context(user):
self.client.post(draft_unlock_url, follow=True)

expected_body = "The following draft version has been unlocked by {by_user} for their use.".format(
by_user=user.username
)
expected_body = f"The following draft version has been unlocked by {user.username} for their use."

self.assertEqual(len(mail.outbox), 1)
self.assertIn(expected_body, mail.outbox[0].body)
Expand Down Expand Up @@ -813,9 +807,7 @@ def test_enabled_unlock_button_when_content_is_locked(self):
toolbar = get_toolbar(version.content, user=self.get_superuser(), content_mode=True)
proxy_model = toolbar._get_proxy_model()
expected_unlock_url = reverse(
"admin:{app}_{model}_unlock".format(
app=proxy_model._meta.app_label, model=proxy_model.__name__.lower()
),
f"admin:{proxy_model._meta.app_label}_{proxy_model.__name__.lower()}_unlock",
args=(version.pk,),
)
toolbar.post_template_populate()
Expand Down
4 changes: 1 addition & 3 deletions tests/test_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ def test_version_menu_label(self):
toolbar.post_template_populate()
version_menu = toolbar.toolbar.get_menu("version")

expected_label = "Version #{number} ({state})".format(
number=version.number, state=dict(VERSION_STATES)[version.state]
)
expected_label = f"Version #{version.number} ({dict(VERSION_STATES)[version.state]})"

self.assertEqual(expected_label, version_menu.name)

Expand Down

0 comments on commit ca50367

Please sign in to comment.