-
Notifications
You must be signed in to change notification settings - Fork 53
/
forms.py
122 lines (83 loc) · 2.67 KB
/
forms.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
try:
from collections import OrderedDict
except ImportError: # Python 2.6, Django < 1.7
from django.utils.datastructures import SortedDict as OrderedDict # NOQA
from django import forms
from django.contrib.admin import widgets as admin_widgets
from django.core.exceptions import ValidationError
from betterforms.multiform import MultiForm, MultiModelForm
from .models import User, Profile, Badge, Author
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('name',)
class ProfileForm(forms.ModelForm):
name = forms.CharField(label='Namespace Clash')
class Meta:
model = Profile
fields = ('name', 'display_name',)
class UserProfileMultiForm(MultiModelForm):
form_classes = OrderedDict((
('user', UserForm),
('profile', ProfileForm),
))
class RaisesErrorForm(forms.Form):
name = forms.CharField()
hidden = forms.CharField(widget=forms.HiddenInput)
class Media:
js = ('test.js',)
def clean(self):
raise ValidationError('It broke')
class ErrorMultiForm(MultiForm):
form_classes = {
'errors': RaisesErrorForm,
'errors2': RaisesErrorForm,
}
class FileForm(forms.Form):
# we use this widget to test the media property
date = forms.DateTimeField(widget=admin_widgets.AdminSplitDateTime)
image = forms.ImageField()
hidden = forms.CharField(widget=forms.HiddenInput)
class NeedsFileField(MultiForm):
form_classes = OrderedDict((
('file', FileForm),
('errors', RaisesErrorForm),
))
class BadgeForm(forms.ModelForm):
class Meta:
model = Badge
fields = ('name', 'color',)
class BadgeMultiForm(MultiModelForm):
form_classes = {
'badge1': BadgeForm,
'badge2': BadgeForm,
}
class NonModelForm(forms.Form):
field1 = forms.CharField()
class MixedForm(MultiModelForm):
form_classes = {
'badge': BadgeForm,
'non_model': NonModelForm,
}
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ('name', 'books',)
class ManyToManyMultiForm(MultiModelForm):
form_classes = {
'badge': BadgeForm,
'author': AuthorForm,
}
class OptionalFileForm(forms.Form):
myfile = forms.FileField(required=False)
class Step1Form(MultiModelForm):
# This is required because the WizardView introspects it, but we don't have
# a way of determining this dynamically, so just set it to an empty
# dictionary.
base_fields = {}
form_classes = {
'myfile': OptionalFileForm,
'profile': ProfileForm,
}
class Step2Form(forms.Form):
confirm = forms.BooleanField(required=True)