-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
43 lines (35 loc) · 1.32 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import random
import string
from django.utils.text import slugify
def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def unique_slug_generator(instance, new_slug=None):
"""
This is for a Django project and it assumes your instance
has a model with a slug field and a title character (char) field.
"""
if new_slug is not None:
slug = new_slug
else:
slug = slugify(instance.title)
Klass = instance.__class__ # it allows you to set 'Klass' to the actual class of any instance
qs_exists = Klass.objects.filter(slug=slug).exists()
if qs_exists:
new_slug = "{slug}-{randstr}".format(
slug=slug,
randstr=random_string_generator(size=4)
)
return unique_slug_generator(instance, new_slug=new_slug)
return slug
def unique_key(instance):
"""
:param instance:The instance of class
:return: key created
"""
size = random.randint(25, 30)
key = random_string_generator(size)
Klass = instance.__class__ # it allows you to set 'Klass' to the actual class of any instance
qs_exists = Klass.objects.filter(key=key).exists()
if qs_exists:
return unique_order_id(instance)
return key