Last active
October 1, 2019 04:34
-
-
Save bryanchow/8b5bd3c90ab41c82f68d95f7d96f28ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://gist.github.com/bryanchow/8b5bd3c90ab41c82f68d95f7d96f28ed | |
import shortuuid | |
from django.conf import settings | |
DEFAULT_ALPHABET = "0123456789abcdef" | |
def make_unique_code(length=None, namespace=None, alphabet=None): | |
""" | |
Generate a concise, unique identifier code. | |
""" | |
alphabet = ( | |
alphabet or getattr(settings, 'UNIQUE_CODE_ALPHABET', DEFAULT_ALPHABET) | |
) | |
shortuuid.set_alphabet(alphabet) | |
if namespace: | |
code = shortuuid.uuid(name=namespace) | |
else: | |
code = shortuuid.uuid() | |
return code[:length] if length is not None else code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment