/
fields.py
99 lines (82 loc) · 3.3 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
from django.db import models
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
class ModelNameFormField(forms.CharField):
@staticmethod
def get_model_from_string(model_path):
try:
app_label, model_name = model_path.rsplit('.models.')
return models.get_model(app_label, model_name)
except:
return None
def clean(self, value):
"""
Validates that the input matches the regular expression. Returns a
Unicode object.
"""
value = super(ModelNameFormField, self).clean(value)
if value == u'':
return value
if not ModelNameFormField.get_model_from_string(value):
raise ValidationError(
_('Model could not be imported: %(value)s. Please use a valid model path.'),
code='invalid',
params={'value': value},
)
return value
class ModelNameField(models.CharField):
@staticmethod
def get_model_from_string(model_path):
return ModelNameFormField.get_model_from_string(model_path)
def formfield(self, **kwargs):
# This is a fairly standard way to set up some defaults
# while letting the caller override them.
defaults = {'form_class': ModelNameFormField}
defaults.update(kwargs)
return super(ModelNameField, self).formfield(**defaults)
class TemplateFormField(forms.CharField):
def clean(self, value):
"""
Validates that the input can be compiled as a template.
"""
value = super(TemplateFormField, self).clean(value)
from django.template import Template, TemplateSyntaxError
try:
Template(value)
except TemplateSyntaxError, error:
raise ValidationError(error)
return value
class TemplateCharField(models.CharField):
def formfield(self, **kwargs):
# This is a fairly standard way to set up some defaults
# while letting the caller override them.
defaults = {'form_class': TemplateFormField}
defaults.update(kwargs)
return super(TemplateCharField, self).formfield(**defaults)
class TemplateTextField(models.TextField):
def formfield(self, **kwargs):
# This is a fairly standard way to set up some defaults
# while letting the caller override them.
defaults = {'form_class': TemplateFormField}
defaults.update(kwargs)
return super(TemplateTextField, self).formfield(**defaults)
class RegexpExpressionFormField(forms.CharField):
def clean(self, value):
"""
Validates that the input can be compiled as a Regular Expression.
"""
value = super(RegexpExpressionFormField, self).clean(value)
import re
try:
re.compile(value)
except Exception, error:
raise ValidationError(error)
return value
class RegexpExpressionField(models.CharField):
def formfield(self, **kwargs):
# This is a fairly standard way to set up some defaults
# while letting the caller override them.
defaults = {'form_class': RegexpExpressionFormField}
defaults.update(kwargs)
return super(RegexpExpressionField, self).formfield(**defaults)