| 112 | |
| 113 | {{{ |
| 114 | from django.conf import settings as django_settings |
| 115 | from django.db import models |
| 116 | from django.utils.translation import gettext_lazy as _ |
| 117 | |
| 118 | from .utils import generate_regular_udid, generate_small_udid |
| 119 | from . import validators as speedy_core_base_validators |
| 120 | |
| 121 | |
| 122 | # Never use this class directly. Only use inherited classes below. |
| 123 | class UDIDField(models.CharField): |
| 124 | class Meta: |
| 125 | abstract = True |
| 126 | |
| 127 | def __init__(self, *args, **kwargs): |
| 128 | given_kwargs = kwargs |
| 129 | defaults = { |
| 130 | 'verbose_name': _('ID'), |
| 131 | 'primary_key': True, |
| 132 | 'db_index': True, |
| 133 | 'unique': True, |
| 134 | } |
| 135 | kwargs = defaults |
| 136 | kwargs.update(given_kwargs) |
| 137 | super().__init__(*args, **kwargs) |
| 138 | |
| 139 | |
| 140 | class SmallUDIDField(UDIDField): |
| 141 | id_generator = staticmethod(generate_small_udid) |
| 142 | |
| 143 | def __init__(self, *args, **kwargs): |
| 144 | given_kwargs = kwargs |
| 145 | defaults = { |
| 146 | 'max_length': django_settings.SMALL_UDID_LENGTH, |
| 147 | 'validators': [speedy_core_base_validators.small_udid_validator], |
| 148 | } |
| 149 | kwargs = defaults |
| 150 | kwargs.update(given_kwargs) |
| 151 | super().__init__(*args, **kwargs) |
| 152 | |
| 153 | |
| 154 | class RegularUDIDField(UDIDField): |
| 155 | id_generator = staticmethod(generate_regular_udid) |
| 156 | |
| 157 | def __init__(self, *args, **kwargs): |
| 158 | given_kwargs = kwargs |
| 159 | defaults = { |
| 160 | 'max_length': django_settings.REGULAR_UDID_LENGTH, |
| 161 | 'validators': [speedy_core_base_validators.regular_udid_validator], |
| 162 | } |
| 163 | kwargs = defaults |
| 164 | kwargs.update(given_kwargs) |
| 165 | super().__init__(*args, **kwargs) |
| 166 | }}} |