|
|
@ -4,8 +4,8 @@ from django.utils.crypto import get_random_string |
|
|
|
try: |
|
|
|
try: |
|
|
|
from django_scopes import scopes_disabled |
|
|
|
from django_scopes import scopes_disabled |
|
|
|
except ImportError: |
|
|
|
except ImportError: |
|
|
|
from xenua.decorators import no_op as scopes_disabled |
|
|
|
scopes_disabled = False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RandomSlugMixin(models.Model): |
|
|
|
class RandomSlugMixin(models.Model): |
|
|
|
""" |
|
|
|
""" |
|
|
@ -29,14 +29,12 @@ class RandomSlugMixin(models.Model): |
|
|
|
if not self.slug: |
|
|
|
if not self.slug: |
|
|
|
self.slug = self.generate_new_slug() |
|
|
|
self.slug = self.generate_new_slug() |
|
|
|
|
|
|
|
|
|
|
|
@scopes_disabled() |
|
|
|
|
|
|
|
def generate_new_slug(self): |
|
|
|
def generate_new_slug(self): |
|
|
|
slug = get_random_string(self.slug_length, self.slug_chars) |
|
|
|
slug = get_random_string(self.slug_length, self.slug_chars) |
|
|
|
if self.__class__.objects.filter(slug=slug).exists(): |
|
|
|
if self.__class__.objects.filter(slug=slug).exists(): |
|
|
|
return self.generate_new_slug() |
|
|
|
return self.generate_new_slug() |
|
|
|
return slug |
|
|
|
return slug |
|
|
|
|
|
|
|
|
|
|
|
@scopes_disabled() |
|
|
|
|
|
|
|
@property |
|
|
|
@property |
|
|
|
def slugs_available(self): |
|
|
|
def slugs_available(self): |
|
|
|
return self.slug_count_total - self.__class__.objects.count() |
|
|
|
return self.slug_count_total - self.__class__.objects.count() |
|
|
@ -49,6 +47,10 @@ class RandomSlugMixin(models.Model): |
|
|
|
def slug_length(self): |
|
|
|
def slug_length(self): |
|
|
|
return self.__class__._meta.get_field('slug').max_length |
|
|
|
return self.__class__._meta.get_field('slug').max_length |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if scopes_disabled: |
|
|
|
|
|
|
|
generate_new_slug = scopes_disabled(generate_new_slug) |
|
|
|
|
|
|
|
slugs_available = scopes_disabled(slugs_available) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RandomSlugPKMixin(RandomSlugMixin): |
|
|
|
class RandomSlugPKMixin(RandomSlugMixin): |
|
|
|
slug = models.CharField(max_length=6, unique=True, primary_key=True) |
|
|
|
slug = models.CharField(max_length=6, unique=True, primary_key=True) |
|
|
|