Skip to content

Commit

Permalink
allow same name for category in different languages (#189)
Browse files Browse the repository at this point in the history
* allow same name for category in different languages

* fixing flake8 issues

* updated migration for django 4.1
  • Loading branch information
vasekch authored Jul 26, 2023
1 parent 9961e68 commit d88a28f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
28 changes: 28 additions & 0 deletions djangocms_alias/migrations/0003_auto_20230725_1547.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.1.8 on 2023-07-25 15:47

from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

dependencies = [
('cms', '__latest__'),
('djangocms_alias', '0002_auto_20200723_1424'),
]

operations = [
migrations.AlterField(
model_name='aliasplugin',
name='cmsplugin_ptr',
field=models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='%(app_label)s_%(class)s', serialize=False, to='cms.cmsplugin'),
),
migrations.AlterField(
model_name='categorytranslation',
name='name',
field=models.CharField(max_length=120, verbose_name='name'),
),
migrations.AlterUniqueTogether(
name='categorytranslation',
unique_together={('language_code', 'master'), ('name', 'language_code')},
),
]
2 changes: 1 addition & 1 deletion djangocms_alias/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class Category(TranslatableModel):
name=models.CharField(
verbose_name=_('name'),
max_length=120,
unique=True,
),
meta={'unique_together': [('name', 'language_code')]},
)

class Meta:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django import get_version
from django.contrib.sites.models import Site
from django.core.exceptions import ValidationError
from django.urls import reverse

from cms.api import add_plugin, create_title
Expand Down Expand Up @@ -420,3 +421,29 @@ def test_category_get_absolute_url(self):
)

self.assertEqual(category.get_absolute_url(), expected)

def test_category_name_same_across_languages(self):
"""
Category name may be the same across languages
"""
category = Category.objects.create(name='Samename A')
category.set_current_language('de')
category.name = "Samename A"
try:
category.validate_unique()
except ValidationError:
self.fail("Same Category name should be allowed across two languages.")

category.set_current_language('en')
self.assertEqual(category.name, 'Samename A')
category.set_current_language('de')
self.assertEqual(category.name, 'Samename A')

def test_category_name_unique_for_language(self):
"""
Category name can't be the same in one language for two different categories
"""
with self.login_user_context(self.superuser):
Category.objects.create(name='Samename B')
c = Category(name='Samename B')
self.assertRaises(ValidationError, c.validate_unique)

0 comments on commit d88a28f

Please sign in to comment.