mirror of
				https://github.com/SectorLabs/django-localized-fields.git
				synced 2025-10-30 18:48:56 +03:00 
			
		
		
		
	Add flag to disable LocalizedUniqueSlugField
This commit is contained in:
		| @@ -32,6 +32,7 @@ class LocalizedUniqueSlugField(LocalizedAutoSlugField): | |||||||
|  |  | ||||||
|         kwargs["uniqueness"] = kwargs.pop("uniqueness", get_language_codes()) |         kwargs["uniqueness"] = kwargs.pop("uniqueness", get_language_codes()) | ||||||
|  |  | ||||||
|  |         self.enabled = kwargs.pop("enabled", True) | ||||||
|         self.immutable = kwargs.pop("immutable", False) |         self.immutable = kwargs.pop("immutable", False) | ||||||
|  |  | ||||||
|         super(LocalizedUniqueSlugField, self).__init__(*args, **kwargs) |         super(LocalizedUniqueSlugField, self).__init__(*args, **kwargs) | ||||||
| @@ -69,6 +70,9 @@ class LocalizedUniqueSlugField(LocalizedAutoSlugField): | |||||||
|             The localized slug that was generated. |             The localized slug that was generated. | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
|  |         if not self.enabled: | ||||||
|  |             return getattr(instance, self.name) | ||||||
|  |  | ||||||
|         if not isinstance(instance, AtomicSlugRetryMixin): |         if not isinstance(instance, AtomicSlugRetryMixin): | ||||||
|             raise ImproperlyConfigured( |             raise ImproperlyConfigured( | ||||||
|                 ( |                 ( | ||||||
|   | |||||||
| @@ -1,5 +1,7 @@ | |||||||
| import copy | import copy | ||||||
|  |  | ||||||
|  | import pytest | ||||||
|  |  | ||||||
| from django import forms | from django import forms | ||||||
| from django.conf import settings | from django.conf import settings | ||||||
| from django.db import models | from django.db import models | ||||||
| @@ -215,6 +217,27 @@ class LocalizedSlugFieldTestCase(TestCase): | |||||||
|         for lang_code, lang_name in settings.LANGUAGES: |         for lang_code, lang_name in settings.LANGUAGES: | ||||||
|             assert obj.slug.get(lang_code) == "title-%s" % lang_name.lower() |             assert obj.slug.get(lang_code) == "title-%s" % lang_name.lower() | ||||||
|  |  | ||||||
|  |     @classmethod | ||||||
|  |     def test_disable(cls): | ||||||
|  |         """Tests whether disabling auto-slugging works.""" | ||||||
|  |  | ||||||
|  |         Model = get_fake_model( | ||||||
|  |             { | ||||||
|  |                 "title": LocalizedField(), | ||||||
|  |                 "slug": LocalizedUniqueSlugField( | ||||||
|  |                     populate_from="title", enabled=False | ||||||
|  |                 ), | ||||||
|  |             } | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         obj = Model() | ||||||
|  |         obj.title = "test" | ||||||
|  |  | ||||||
|  |         # should raise IntegrityError because auto-slugging | ||||||
|  |         # is disabled and the slug field is NULL | ||||||
|  |         with pytest.raises(IntegrityError): | ||||||
|  |             obj.save() | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     def test_allows_override_when_immutable(cls): |     def test_allows_override_when_immutable(cls): | ||||||
|         """Tests whether setting a value manually works and does not get |         """Tests whether setting a value manually works and does not get | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user