-
Notifications
You must be signed in to change notification settings - Fork 16
/
fields.py
216 lines (168 loc) · 8.39 KB
/
fields.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os
from decimal import Decimal
from uuid import uuid4 as uuid
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models import FileField as OriginFileField
from django.db.models.fields import DecimalField as OriginDecimalField
from django.forms import forms
from django.utils.encoding import force_text
from django.utils.translation import ugettext
from chamber import config
from chamber.forms import fields as chamber_fields
from chamber.forms.validators import (
RestrictedFileValidator, AllowedContentTypesByFilenameFileValidator, AllowedContentTypesByContentFileValidator
)
from chamber.models.humanized_helpers import price_humanized
from chamber.utils.datastructures import SequenceChoicesEnumMixin, SubstatesChoicesNumEnum
try:
from sorl.thumbnail import ImageField as OriginImageField
except ImportError:
from django.db.models import ImageField as OriginImageField
class DecimalField(OriginDecimalField):
def __init__(self, *args, **kwargs):
self.step = kwargs.pop('step', 'any')
self.min = kwargs.pop('min', None)
self.max = kwargs.pop('max', None)
kwargs['validators'] = kwargs.get('validators', [])
if self.min is not None:
kwargs['validators'].append(MinValueValidator(self.min))
if self.max is not None:
kwargs['validators'].append(MaxValueValidator(self.max))
super().__init__(*args, **kwargs)
def formfield(self, **kwargs):
defaults = {
'form_class': chamber_fields.DecimalField,
'step': self.step,
'min': self.min,
'max': self.max,
}
defaults.update(kwargs)
return super().formfield(**defaults)
class RestrictedFileFieldMixin:
"""
Same as FileField, but you can specify:
* allowed_content_types - list of allowed content types. Example: ['application/json', 'image/jpeg']
* max_upload_size - a number indicating the maximum file size allowed for upload in MB.
"""
def __init__(self, *args, **kwargs):
max_upload_size = kwargs.pop('max_upload_size', config.CHAMBER_MAX_FILE_UPLOAD_SIZE) * 1024 * 1024
allowed_content_types = kwargs.pop('allowed_content_types', None)
super().__init__(*args, **kwargs)
self.validators.append(RestrictedFileValidator(max_upload_size))
if allowed_content_types:
self.validators = tuple(self.validators) + (
AllowedContentTypesByFilenameFileValidator(allowed_content_types),
AllowedContentTypesByContentFileValidator(allowed_content_types),
)
def generate_filename(self, instance, filename):
"""
removes UTF chars from filename
"""
from unidecode import unidecode
return super().generate_filename(instance, unidecode(force_text(filename)))
class FileField(RestrictedFileFieldMixin, OriginFileField):
pass
class ImageField(RestrictedFileFieldMixin, OriginImageField):
def __init__(self, *args, **kwargs):
allowed_content_types = kwargs.pop('allowed_content_types', config.CHAMBER_DEFAULT_IMAGE_ALLOWED_CONTENT_TYPES)
super().__init__(allowed_content_types=allowed_content_types, *args, **kwargs)
def generate_random_upload_path(instance, filename):
"""
Pass this function to upload_to argument of FileField to store the file on an unguessable path.
The format of the path is class_name/hash/original_filename.
"""
return os.path.join(instance.__class__.__name__.lower(), uuid().hex, filename)
class PrevValuePositiveIntegerField(models.PositiveIntegerField):
def __init__(self, *args, **kwargs):
self.copy_field_name = kwargs.pop('copy_field_name', None)
super().__init__(*args, **kwargs)
def pre_save(self, model_instance, add):
# During migrations no changed_fields is set for a model
if hasattr(model_instance, 'changed_fields') and self.copy_field_name in model_instance.changed_fields:
setattr(
model_instance, self.attname,
getattr(model_instance, self.copy_field_name)
if model_instance.is_adding else model_instance.initial_values[self.copy_field_name]
)
return super().pre_save(model_instance, add)
class SubchoicesPositiveIntegerField(models.PositiveIntegerField):
empty_values = ()
def __init__(self, *args, **kwargs):
self.enum = kwargs.pop('enum', None)
self.supchoices_field_name = kwargs.pop('supchoices_field_name', None)
assert self.enum is None or isinstance(self.enum, SubstatesChoicesNumEnum)
if self.enum:
kwargs['choices'] = self.enum.choices
super().__init__(*args, **kwargs)
def _get_supvalue(self, model_instance):
return getattr(model_instance, self.supchoices_field_name)
def clean(self, value, model_instance):
if self.enum and self._get_supvalue(model_instance) not in self.enum.categories:
return None
else:
return super().clean(value, model_instance)
def _raise_error_if_value_should_be_empty(self, value, subvalue):
if self.enum and subvalue not in self.enum.categories and value is not None:
raise ValidationError(ugettext('Value must be empty'))
def _raise_error_if_value_not_allowed(self, value, subvalue, model_instance):
allowed_values = self.enum.get_allowed_states(getattr(model_instance, self.supchoices_field_name))
if subvalue in self.enum.categories and value not in allowed_values:
raise ValidationError(ugettext('Allowed choices are {}.').format(
', '.join(('{} ({})'.format(*(self.enum.get_label(val), val)) for val in allowed_values))
))
def validate(self, value, model_instance):
if not self.enum:
return
self._raise_error_if_value_should_be_empty(value, self._get_supvalue(model_instance))
self._raise_error_if_value_not_allowed(value, self._get_supvalue(model_instance), model_instance)
class EnumSequenceFieldMixin:
# TODO Once SmartWidget mixin is not in is-core, add formfield method with the appropriate widget
def __init__(self, *args, **kwargs):
self.enum = kwargs.pop('enum', None)
self.prev_field_name = kwargs.pop('prev_field', None)
assert self.enum is None or isinstance(self.enum, SequenceChoicesEnumMixin)
if self.enum:
kwargs['choices'] = self.enum.choices
super().__init__(*args, **kwargs)
def validate(self, value, model_instance):
super().validate(value, model_instance)
if self.enum:
prev_value = model_instance.initial_values[self.attname] if model_instance.is_changing else None
allowed_next_values = self.enum.get_allowed_next_states(prev_value, model_instance)
if ((self.name in model_instance.changed_fields or model_instance.is_adding) and
value not in allowed_next_values):
raise ValidationError(
ugettext('Allowed choices are {}.').format(
', '.join(('{} ({})'.format(*(self.enum.get_label(val), val)) for val in allowed_next_values))))
class EnumSequencePositiveIntegerField(EnumSequenceFieldMixin, models.PositiveIntegerField):
pass
class EnumSequenceCharField(EnumSequenceFieldMixin, models.CharField):
pass
class PriceField(DecimalField):
def __init__(self, *args, **kwargs):
self.currency = kwargs.pop('currency', ugettext('CZK'))
super().__init__(*args, **{
'decimal_places': 2,
'max_digits': 10,
'humanized': lambda val, inst, field: price_humanized(val, inst, currency=field.currency),
**kwargs
})
def formfield(self, **kwargs):
return super(DecimalField, self).formfield(
**{
'form_class': chamber_fields.PriceField,
'currency': self.currency,
**kwargs
}
)
class PositivePriceField(PriceField):
def __init__(self, *args, **kwargs):
kwargs['validators'] = kwargs.get('validators', [])
kwargs['validators'].append(MinValueValidator(Decimal('0.00')))
super().__init__(*args, **kwargs)
def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
del kwargs['validators']
return name, path, args, kwargs