mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-04-24 19:32:53 +03:00
Add flag to disable LocalizedUniqueSlugField
This commit is contained in:
parent
62e1e805c7
commit
bd8924224e
@ -32,6 +32,7 @@ class LocalizedUniqueSlugField(LocalizedAutoSlugField):
|
||||
|
||||
kwargs["uniqueness"] = kwargs.pop("uniqueness", get_language_codes())
|
||||
|
||||
self.enabled = kwargs.pop("enabled", True)
|
||||
self.immutable = kwargs.pop("immutable", False)
|
||||
|
||||
super(LocalizedUniqueSlugField, self).__init__(*args, **kwargs)
|
||||
@ -69,6 +70,9 @@ class LocalizedUniqueSlugField(LocalizedAutoSlugField):
|
||||
The localized slug that was generated.
|
||||
"""
|
||||
|
||||
if not self.enabled:
|
||||
return getattr(instance, self.name)
|
||||
|
||||
if not isinstance(instance, AtomicSlugRetryMixin):
|
||||
raise ImproperlyConfigured(
|
||||
(
|
||||
|
@ -1,5 +1,7 @@
|
||||
import copy
|
||||
|
||||
import pytest
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
@ -215,6 +217,27 @@ class LocalizedSlugFieldTestCase(TestCase):
|
||||
for lang_code, lang_name in settings.LANGUAGES:
|
||||
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
|
||||
def test_allows_override_when_immutable(cls):
|
||||
"""Tests whether setting a value manually works and does not get
|
||||
|
Loading…
x
Reference in New Issue
Block a user