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