-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathfields.py
47 lines (35 loc) · 1.5 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
from django.db import models
from django.utils.translation import gettext_lazy as _
from djangocms_attributes_field import fields
from .constants import TAG_CHOICES
class AttributesField(fields.AttributesField):
def __init__(self, *args, **kwargs):
if 'verbose_name' not in kwargs:
kwargs['verbose_name'] = _('Attributes')
if 'blank' not in kwargs:
kwargs['blank'] = True
super().__init__(*args, **kwargs)
class TagTypeField(models.CharField):
def __init__(self, *args, **kwargs):
if 'verbose_name' not in kwargs:
kwargs['verbose_name'] = _('Tag type')
if 'choices' not in kwargs:
kwargs['choices'] = TAG_CHOICES
if 'default' not in kwargs:
kwargs['default'] = TAG_CHOICES[0][0]
if 'max_length' not in kwargs:
kwargs['max_length'] = 255
if 'help_text' not in kwargs:
kwargs['help_text'] = _('Select the HTML tag to be used.')
super().__init__(*args, **kwargs)
class IntegerRangeField(models.IntegerField):
def __init__(self, verbose_name=None, name=None, min_value=None, max_value=None, **kwargs):
self.min_value, self.max_value = min_value, max_value
models.IntegerField.__init__(self, verbose_name, name, **kwargs)
def formfield(self, **kwargs):
defaults = {
'min_value': self.min_value,
'max_value': self.max_value,
}
defaults.update(kwargs)
return super().formfield(**defaults)