mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-12-14 07:42:24 +03:00
Rename LocalizedMagicSlugField to LocalizedUniqueSlugField
This commit is contained in:
68
localized_fields/fields/localized_uniqueslug_field.py
Normal file
68
localized_fields/fields/localized_uniqueslug_field.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from django.conf import settings
|
||||
from django.utils.text import slugify
|
||||
|
||||
from ..localized_value import LocalizedValue
|
||||
from .localized_autoslug_field import LocalizedAutoSlugField
|
||||
from ..util import get_language_codes
|
||||
|
||||
|
||||
class LocalizedUniqueSlugField(LocalizedAutoSlugField):
|
||||
"""Automatically provides slugs for a localized
|
||||
field upon saving."
|
||||
|
||||
An improved version of :see:LocalizedAutoSlugField,
|
||||
which adds:
|
||||
|
||||
- Concurrency safety
|
||||
- Improved performance
|
||||
|
||||
When in doubt, use this over :see:LocalizedAutoSlugField.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initializes a new instance of :see:LocalizedUniqueSlugField."""
|
||||
|
||||
self.populate_from = kwargs.pop('populate_from')
|
||||
kwargs['uniqueness'] = kwargs.pop('uniqueness', get_language_codes())
|
||||
|
||||
super(LocalizedAutoSlugField, self).__init__(
|
||||
*args,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
def pre_save(self, instance, add: bool):
|
||||
"""Ran just before the model is saved, allows us to built
|
||||
the slug.
|
||||
|
||||
Arguments:
|
||||
instance:
|
||||
The model that is being saved.
|
||||
|
||||
add:
|
||||
Indicates whether this is a new entry
|
||||
to the database or an update.
|
||||
|
||||
Returns:
|
||||
The localized slug that was generated.
|
||||
"""
|
||||
|
||||
slugs = LocalizedValue()
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
value = self._get_populate_from_value(
|
||||
instance,
|
||||
self.populate_from,
|
||||
lang_code
|
||||
)
|
||||
|
||||
if not value:
|
||||
continue
|
||||
|
||||
slug = slugify(value, allow_unicode=True)
|
||||
if instance.retries > 0:
|
||||
slug += '-%d' % instance.retries
|
||||
|
||||
slugs.set(lang_code, slug)
|
||||
|
||||
setattr(instance, self.name, slugs)
|
||||
return slugs
|
||||
Reference in New Issue
Block a user