From d88a28fa42c519bb807fda96eb0a43807297ef2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=C5=A1ek=20Chalupn=C3=AD=C4=8Dek?= Date: Wed, 26 Jul 2023 16:49:18 +0200 Subject: [PATCH] allow same name for category in different languages (#189) * allow same name for category in different languages * fixing flake8 issues * updated migration for django 4.1 --- .../migrations/0003_auto_20230725_1547.py | 28 +++++++++++++++++++ djangocms_alias/models.py | 2 +- tests/test_models.py | 27 ++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 djangocms_alias/migrations/0003_auto_20230725_1547.py diff --git a/djangocms_alias/migrations/0003_auto_20230725_1547.py b/djangocms_alias/migrations/0003_auto_20230725_1547.py new file mode 100644 index 00000000..3b575cf1 --- /dev/null +++ b/djangocms_alias/migrations/0003_auto_20230725_1547.py @@ -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')}, + ), + ] diff --git a/djangocms_alias/models.py b/djangocms_alias/models.py index f6af0d63..861f0ec0 100644 --- a/djangocms_alias/models.py +++ b/djangocms_alias/models.py @@ -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: diff --git a/tests/test_models.py b/tests/test_models.py index 44762c63..bde14b6d 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -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 @@ -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)