localized_fields/fields/uniqueslug_field.py
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.
        """

        if not self.enabled:
            return getattr(instance, self.name)

        if not isinstance(instance, AtomicSlugRetryMixin):
            raise ImproperlyConfigured(
                (
                    "Model '%s' does not inherit from AtomicSlugRetryMixin. "
                    "Without this, the LocalizedUniqueSlugField will not work."
                )
                % type(instance).__name__
            )

        slugs = LocalizedValue()

        for lang_code, value in self._get_populate_values(instance):
            if not value:
                continue

            slug = slugify(value, allow_unicode=True)

            current_slug = getattr(instance, self.name).get(lang_code)
            if current_slug and self.immutable:
                slugs.set(lang_code, current_slug)
                continue

            # verify whether it's needed to re-generate a slug,
            # if not, re-use the same slug
            if instance.pk is not None:
                if current_slug is not None:
                    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

            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

            slugs.set(lang_code, slug)

        setattr(instance, self.name, slugs)
        return slugs
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.
        
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 .
def pre_save(self, instance, add: bool):
        if not self.enabled:
            return getattr(instance, self.name)
        if not isinstance(instance, AtomicSlugRetryMixin):
            raise ImproperlyConfigured(
                (
                    "Model '%s' does not inherit from AtomicSlugRetryMixin. "
                    "Without this, the LocalizedUniqueSlugField will not work."
                )
                % type(instance).__name__
            )
        slugs = LocalizedValue()
        for lang_code, value in self._get_populate_values(instance):
            if not value:
                continue
            slug = slugify(value, allow_unicode=True)
            current_slug = getattr(instance, self.name).get(lang_code)
            if current_slug and self.immutable:
                slugs.set(lang_code, current_slug)
                continue
            if instance.pk is not None:
                if current_slug is not None:
                    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
            retries = getattr(instance, "retries", 0)
            if retries > 0:
                if not self.include_time:
                    slug += "-"
                slug += "%d" % retries
            slugs.set(lang_code, slug)
        setattr(instance, self.name, slugs)
        return slugs
def pre_save ( self , instance , add : bool ) : if not self . enabled : return getattr ( instance , self . name ) if not isinstance ( instance , AtomicSlugRetryMixin ) : raise ImproperlyConfigured ( ( "Model '%s' does not inherit from AtomicSlugRetryMixin. " "Without this, the LocalizedUniqueSlugField will not work." ) % type ( instance ) . __name__ ) slugs = LocalizedValue ( ) for lang_code , value in self . _get_populate_values ( instance ) : if not value : continue slug = slugify ( value , allow_unicode = True ) current_slug = getattr ( instance , self . name ) . get ( lang_code ) if current_slug and self . immutable : slugs . set ( lang_code , current_slug ) continue if instance . pk is not None : if current_slug is not None : 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 retries = getattr ( instance , "retries" , 0 ) if retries > 0 : if not self . include_time : slug += "-" slug += "%d" % retries slugs . set ( lang_code , slug ) setattr ( instance , self . name , slugs ) return slugs
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 .
"""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.
        """ # verify whether it's needed to re-generate a slug, # if not, re-use the same slug # do not add another - if we already added time
The localized slug that was generated.
The localized slug that was generated .
self
instance
The model that is being saved.
The model that is being saved .
add
bool
Indicates whether this is a new entry
to the database or an update.
Indicates whether this is a new entry to the database or an update .