Revert "LocalizedUniqueSlugField refactored"

This reverts commit 03df76d6d7.
This commit is contained in:
Swen Kooij
2017-05-25 17:23:39 +03:00
parent bf2995fd27
commit cff22855c2
4 changed files with 75 additions and 98 deletions

View File

@@ -1,17 +1,38 @@
from django.core.checks import Warning
from django.db import transaction
from django.conf import settings
from django.db.utils import IntegrityError
class AtomicSlugRetryMixin:
"""A Mixin keeped for backwards compatibility"""
"""Makes :see:LocalizedUniqueSlugField work by retrying upon
violation of the UNIQUE constraint."""
@classmethod
def check(cls, **kwargs):
errors = super().check(**kwargs)
errors.append(
Warning(
'localized_fields.AtomicSlugRetryMixin is deprecated',
hint='There is no need to use '
'localized_fields.AtomicSlugRetryMixin',
obj=cls
)
def save(self, *args, **kwargs):
"""Saves this model instance to the database."""
max_retries = getattr(
settings,
'LOCALIZED_FIELDS_MAX_RETRIES',
100
)
return errors
if not hasattr(self, 'retries'):
self.retries = 0
with transaction.atomic():
try:
return super().save(*args, **kwargs)
except IntegrityError as ex:
# this is as retarded as it looks, there's no
# way we can put the retry logic inside the slug
# field class... we can also not only catch exceptions
# that apply to slug fields... so yea.. this is as
# retarded as it gets... i am sorry :(
if 'slug' not in str(ex):
raise ex
if self.retries >= max_retries:
raise ex
self.retries += 1
return self.save()