Created
June 7, 2018 08:45
-
-
Save cypreess/122e28518dcec629842b7586d45ba52f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import shortuuid | |
from django.db import models | |
def shorten_name(name): | |
if len(name) <= 3: | |
return name.lower() | |
else: | |
return name[0].lower() + ''.join([ch for ch in name[1:].lower() if ch not in 'aeiouy'][:2]) | |
def generate_uuid(prefix=''): | |
assert len(prefix) == 3, 'Prefix must be 3 characters long for Short UUID' | |
return (prefix + '_' if prefix else '') + shortuuid.uuid() | |
class ShortPrefixedIdModel(models.Model): | |
id = models.CharField(primary_key=True, max_length=26, editable=False) | |
@classmethod | |
def create_id(cls): | |
return generate_uuid(prefix=getattr(cls, 'ID_PREFIX', shorten_name(cls.__name__))) | |
def save(self, *args, **kwargs): | |
# Provide default value for ID field | |
if self._state.adding: | |
self.id = self.create_id() | |
super().save(*args, **kwargs) | |
def __str__(self): | |
return self.id | |
class Meta: | |
abstract = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment