Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: static_alias unnecessarily creates new content objects when used with versioning #202

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions djangocms_alias/templatetags/djangocms_alias_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _get_alias(self, request, static_code, extra_bits):
if is_versioning_enabled() and not request.user.is_authenticated:
return None

# Parlers get_or_create doesn't work well with translations, so we must perform our own get or create
# Parler's get_or_create doesn't work well with translations, so we must perform our own get or create
default_category = Category.objects.filter(translations__name=DEFAULT_STATIC_ALIAS_CATEGORY_NAME).first()
if not default_category:
default_category = Category.objects.create(name=DEFAULT_STATIC_ALIAS_CATEGORY_NAME)
Expand All @@ -124,7 +124,7 @@ def _get_alias(self, request, static_code, extra_bits):

alias = Alias.objects.create(category=default_category, **alias_creation_kwargs)

if not AliasContent._default_manager.filter(alias=alias, language=language).exists():
if not AliasContent._base_manager.filter(alias=alias, language=language).exists():
# Create a first content object if none exists in the given language.
# If versioning is enabled we can only create the records with a logged-in user / staff member
if is_versioning_enabled() and not request.user.is_authenticated:
Expand Down
4 changes: 2 additions & 2 deletions tests/requirements/py311-djmain-cms41-default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# requirements/compile.py
#
asgiref==3.6.0
asgiref==3.7.2
# via django
beautifulsoup4==4.12.2
# via bs4
Expand All @@ -31,7 +31,7 @@ django-classy-tags==4.0.0
# -r requirements.in
# django-cms
# django-sekizai
django-cms==4.1.0rc3
git+https://github.com/django-cms/django-cms@develop-4#egg=django-cms
# via
# -r requirements.in
# djangocms-versioning
Expand Down
4 changes: 2 additions & 2 deletions tests/requirements/py311-djmain-cms41-versioning.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# requirements/compile.py
#
asgiref==3.6.0
asgiref==3.7.2
# via django
beautifulsoup4==4.12.2
# via bs4
Expand All @@ -31,7 +31,7 @@ django-classy-tags==4.0.0
# -r requirements.in
# django-cms
# django-sekizai
django-cms==4.1.0rc3
git+https://github.com/django-cms/django-cms@develop-4#egg=django-cms
# via
# -r requirements.in
# djangocms-versioning
Expand Down
3 changes: 3 additions & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,15 @@ def page_edit_url(lang):

with self.login_user_context(self.superuser):
self.client.get(page_edit_url("en")) # supposed to create the alias and alias content for en
self.client.get(page_edit_url("en")) # supposed to create no additional object
self.client.get(page_edit_url("de")) # supposed to create the alias content for de

alias = AliasModel.objects.get(static_code="template_example_global_alias_code")

self.assertIsNotNone(alias.get_content("en", show_draft_content=True))
self.assertIsNotNone(alias.get_content("de", show_draft_content=True))
# Ensure that exactly two content objects have been created
self.assertEqual(alias.contents(manager="admin_manager").count(), 2)

def test_alias_rendered_when_identifier_is_variable(self):
alias_template = """{% load djangocms_alias_tags %}{% static_alias foo_variable %}""" # noqa: E501
Expand Down
Loading