mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-11-02 11:28:57 +03:00
Re-format all files
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
from datetime import datetime
|
||||
|
||||
from django.utils.text import slugify
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.text import slugify
|
||||
|
||||
from .autoslug_field import LocalizedAutoSlugField
|
||||
from ..util import get_language_codes
|
||||
from ..mixins import AtomicSlugRetryMixin
|
||||
from ..util import get_language_codes
|
||||
from ..value import LocalizedValue
|
||||
from .autoslug_field import LocalizedAutoSlugField
|
||||
|
||||
|
||||
class LocalizedUniqueSlugField(LocalizedAutoSlugField):
|
||||
"""Automatically provides slugs for a localized
|
||||
field upon saving."
|
||||
"""Automatically provides slugs for a localized field upon saving.".
|
||||
|
||||
An improved version of :see:LocalizedAutoSlugField,
|
||||
which adds:
|
||||
@@ -27,30 +26,26 @@ class LocalizedUniqueSlugField(LocalizedAutoSlugField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initializes a new instance of :see:LocalizedUniqueSlugField."""
|
||||
|
||||
kwargs['uniqueness'] = kwargs.pop('uniqueness', get_language_codes())
|
||||
kwargs["uniqueness"] = kwargs.pop("uniqueness", get_language_codes())
|
||||
|
||||
super(LocalizedUniqueSlugField, self).__init__(
|
||||
*args,
|
||||
**kwargs
|
||||
)
|
||||
super(LocalizedUniqueSlugField, self).__init__(*args, **kwargs)
|
||||
|
||||
self.populate_from = kwargs.pop('populate_from')
|
||||
self.include_time = kwargs.pop('include_time', False)
|
||||
self.populate_from = kwargs.pop("populate_from")
|
||||
self.include_time = kwargs.pop("include_time", False)
|
||||
|
||||
def deconstruct(self):
|
||||
"""Deconstructs the field into something the database
|
||||
can store."""
|
||||
"""Deconstructs the field into something the database can store."""
|
||||
|
||||
name, path, args, kwargs = super(
|
||||
LocalizedUniqueSlugField, self).deconstruct()
|
||||
LocalizedUniqueSlugField, self
|
||||
).deconstruct()
|
||||
|
||||
kwargs['populate_from'] = self.populate_from
|
||||
kwargs['include_time'] = self.include_time
|
||||
kwargs["populate_from"] = self.populate_from
|
||||
kwargs["include_time"] = self.include_time
|
||||
return name, path, args, kwargs
|
||||
|
||||
def pre_save(self, instance, add: bool):
|
||||
"""Ran just before the model is saved, allows us to built
|
||||
the slug.
|
||||
"""Ran just before the model is saved, allows us to built the slug.
|
||||
|
||||
Arguments:
|
||||
instance:
|
||||
@@ -65,10 +60,13 @@ class LocalizedUniqueSlugField(LocalizedAutoSlugField):
|
||||
"""
|
||||
|
||||
if not isinstance(instance, AtomicSlugRetryMixin):
|
||||
raise ImproperlyConfigured((
|
||||
'Model \'%s\' does not inherit from AtomicSlugRetryMixin. '
|
||||
'Without this, the LocalizedUniqueSlugField will not work.'
|
||||
) % type(instance).__name__)
|
||||
raise ImproperlyConfigured(
|
||||
(
|
||||
"Model '%s' does not inherit from AtomicSlugRetryMixin. "
|
||||
"Without this, the LocalizedUniqueSlugField will not work."
|
||||
)
|
||||
% type(instance).__name__
|
||||
)
|
||||
|
||||
slugs = LocalizedValue()
|
||||
|
||||
@@ -83,20 +81,21 @@ class LocalizedUniqueSlugField(LocalizedAutoSlugField):
|
||||
if instance.pk is not None:
|
||||
current_slug = getattr(instance, self.name).get(lang_code)
|
||||
if current_slug is not None:
|
||||
stripped_slug = current_slug[0:current_slug.rfind('-')]
|
||||
current_slug_end_index = current_slug.rfind("-")
|
||||
stripped_slug = current_slug[0:current_slug_end_index]
|
||||
if slug == stripped_slug:
|
||||
slugs.set(lang_code, current_slug)
|
||||
continue
|
||||
|
||||
if self.include_time:
|
||||
slug += '-%d' % datetime.now().microsecond
|
||||
slug += "-%d" % datetime.now().microsecond
|
||||
|
||||
retries = getattr(instance, 'retries', 0)
|
||||
retries = getattr(instance, "retries", 0)
|
||||
if retries > 0:
|
||||
# do not add another - if we already added time
|
||||
if not self.include_time:
|
||||
slug += '-'
|
||||
slug += '%d' % retries
|
||||
slug += "-"
|
||||
slug += "%d" % retries
|
||||
|
||||
slugs.set(lang_code, slug)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user