2016-12-06 12:28:29 -05:00
|
|
|
from django.core.validators import RegexValidator
|
2016-03-01 11:23:03 -05:00
|
|
|
from django.db import models
|
|
|
|
|
2020-02-07 11:36:22 -05:00
|
|
|
from utilities.ordering import naturalize
|
2016-12-06 12:28:29 -05:00
|
|
|
from .forms import ColorSelect
|
|
|
|
|
2018-07-30 14:00:37 -04:00
|
|
|
ColorValidator = RegexValidator(
|
|
|
|
regex='^[0-9a-f]{6}$',
|
|
|
|
message='Enter a valid hexadecimal RGB color code.',
|
|
|
|
code='invalid'
|
|
|
|
)
|
2016-12-06 12:28:29 -05:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2019-02-14 09:39:04 -05:00
|
|
|
# Deprecated: Retained only to ensure successful migration from early releases
|
|
|
|
# Use models.CharField(null=True) instead
|
2016-03-01 11:23:03 -05:00
|
|
|
class NullableCharField(models.CharField):
|
|
|
|
description = "Stores empty values as NULL rather than ''"
|
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
if isinstance(value, models.CharField):
|
|
|
|
return value
|
|
|
|
return value or ''
|
|
|
|
|
|
|
|
def get_prep_value(self, value):
|
|
|
|
return value or None
|
2016-12-06 12:28:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
class ColorField(models.CharField):
|
2018-07-30 14:00:37 -04:00
|
|
|
default_validators = [ColorValidator]
|
2016-12-06 12:28:29 -05:00
|
|
|
description = "A hexadecimal RGB color code"
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs['max_length'] = 6
|
2018-11-27 10:52:24 -05:00
|
|
|
super().__init__(*args, **kwargs)
|
2016-12-06 12:28:29 -05:00
|
|
|
|
|
|
|
def formfield(self, **kwargs):
|
|
|
|
kwargs['widget'] = ColorSelect
|
2018-11-27 10:52:24 -05:00
|
|
|
return super().formfield(**kwargs)
|
2020-02-07 11:36:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
class NaturalOrderingField(models.CharField):
|
|
|
|
"""
|
|
|
|
A field which stores a naturalized representation of its target field, to be used for ordering its parent model.
|
|
|
|
|
|
|
|
:param target_field: Name of the field of the parent model to be naturalized
|
|
|
|
:param naturalize_function: The function used to generate a naturalized value (optional)
|
|
|
|
"""
|
|
|
|
description = "Stores a representation of its target field suitable for natural ordering"
|
|
|
|
|
|
|
|
def __init__(self, target_field, naturalize_function=naturalize, *args, **kwargs):
|
|
|
|
self.target_field = target_field
|
|
|
|
self.naturalize_function = naturalize_function
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def pre_save(self, model_instance, add):
|
|
|
|
"""
|
|
|
|
Generate a naturalized value from the target field
|
|
|
|
"""
|
|
|
|
value = getattr(model_instance, self.target_field)
|
|
|
|
return self.naturalize_function(value, max_length=self.max_length)
|
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
kwargs = super().deconstruct()[3] # Pass kwargs from CharField
|
|
|
|
kwargs['naturalize_function'] = self.naturalize_function
|
|
|
|
return (
|
|
|
|
self.name,
|
|
|
|
'utilities.fields.NaturalOrderingField',
|
|
|
|
['target_field'],
|
|
|
|
kwargs,
|
|
|
|
)
|