-
Notifications
You must be signed in to change notification settings - Fork 0
/
different
191 lines (170 loc) · 6.02 KB
/
different
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
#TODO create Class
'''
Main advantages:
+ Possibility for adding defaults.
+ Offers a minimum of how many forms to display at the start.
+ Easier integration into templates.
+ Uses modelforms so the forms are easier to understand.
Disadvantages/To dos:
- No option for deletion (forms with only empty values should probably be deleted)
- More difficult integration into views (right now).
- No option for displaying more than one empty form.
'''
MINIMUM = 5
EXTRA_NR = 2
def _get_empty_forms(modelform, minimum=MINIMUM):
'''Returns a list of empty modelforms.
'''
forms = []
for i in range(minimum):
form = modelform(prefix=i)
forms.append(form)
return forms
def _get_default_forms(modelform, defaults, minimum=MINIMUM):
'''Returns a list of modelforms with default values.
'''
forms = _get_empty_forms(modelform, minimum)
if len(defaults) > len(forms):
raise Exception
i = 0
for default in defaults:
forms[i] = modelform(initial = default, prefix=i)
i += 1
return forms
def _get_nr_of_forms(queryset, minimum=MINIMUM):
'''Returns the number of forms which have to be displayed.'''
nr_of_forms = len(queryset) + EXTRA_NR
if nr_of_forms < minimum:
nr_of_forms = minimum
return nr_of_forms
def get_db_forms(modelform, queryset, defaults, minimum=MINIMUM):
'''
Returns a list of modelforms filled with queryset values,
plus at least one extra form, so that the minimum number of
forms is at least minimum.
Empty forms are filled with default values, if applicable.
Input:
modelform: one of your forms, f.e. MyForm()
queryset: a list of instances, f.e.
queryset = MyModel.objects.all()
defaults: a list of dictionaries, f.e.:
defaults = [
{'first_name': 'Jakob', 'instrument': 'First cello'},
{'instrument': 'Second cello'},
]
minimum: the minimum number of MyForms you want to put in the list.
'''
nr_of_forms = _get_nr_of_forms(queryset, minimum)
i = 0
forms = _get_default_forms(modelform, defaults, nr_of_forms)
for row in queryset:
forms[i] = modelform(initial=vars(row), prefix=i, instance=row)
i += 1
return forms
def _is_populated(modelform, data, prefix):
'''Checks whether data has been entered into the given
fields of a form with the given prefix.
Requires that fields are specified in modelforms Meta function
'''
fields = modelform._meta.fields
for field in fields:
if prefix:
name = unicode(prefix) + '-' + field
else:
name = field
if data[name]:
return True
return False
def get_posted_forms(modelform, data, queryset, minimum=MINIMUM, collection=None):
'''Returns forms which had queryset data entered, as well as all
forms which had request.post data.
Input:
modelform: one of your forms, f.e. MyForm()
data: request.POST
queryset: a list of instances, f.e.
queryset = MyModel.objects.all()
defaults: a list of dictionaries, f.e.:
defaults = [
{'first_name': 'Jakob', 'instrument': 'First cello'},
{'instrument': 'Second cello'},
]
minimum: the minimum number of MyForms you want to put in the list.
collection: None. Not used right now.
'''
nr_of_forms = _get_nr_of_forms(queryset, minimum)
i = 0
forms = []
for instance in queryset:
forms.append(modelform(data, prefix=i, instance=instance))
i += 1
while i < nr_of_forms:
if _is_populated(modelform, data, i):
if collection:
model = modelform._meta.model
instance = model(criterion_collection=collection)
forms.append(modelform(data, prefix=i, instance=instance))
else:
forms.append(modelform(data, prefix=i))
i += 1
return forms
def get_posted_and_empty_forms(modelform, data, queryset, minimum=MINIMUM, collection=None):
"""Returns a mixture of validated forms and empty forms.
If there are any errors in the forms, this mixture is returned to the user.
Input:
modelform: one of your forms, f.e. MyForm()
data: request.POST
queryset: a list of instances, f.e.
queryset = MyModel.objects.all()
defaults: a list of dictionaries, f.e.:
defaults = [
{'first_name': 'Jakob', 'instrument': 'First cello'},
{'instrument': 'Second cello'},
]
minimum: the minimum number of MyForms you want to put in the list.
collection: None. Not used right now.
"""
nr_of_forms = _get_nr_of_forms(queryset, minimum)
i = 0
forms = []
for instance in queryset:
form = modelform(data, prefix=i, instance=instance)
form.is_valid()
forms.append(form)
i += 1
while i < nr_of_forms:
if _is_populated(modelform, data, i):
if collection:
model = modelform._meta.model
instance = model(criterion_collection=collection)
form = modelform(data, prefix=i, instance=instance)
else:
form = modelform(data, prefix=i)
form.is_valid()
else:
form = modelform(prefix=i)
forms.append(form)
i += 1
return forms
def validate_forms(posted_forms):
"""Checks whether the given list of forms is valid. If all are,
returns the list of validated forms. Else, stops checking and
returns False.
"""
for form in posted_forms:
if not form.is_valid():
return False
return posted_forms
def _set_readonly(forms, readonly):
i = 0
for row in readonly:
for el in row:
forms[i].fields[el].widget.attrs['readonly'] = 'readonly'
i += 1
return forms
def set_fields_to_readonly(forms, defaults):
''' set fields to readonly.'''
readonly = []
for default in defaults:
readonly.append( default.keys() )
forms = _set_readonly(forms, readonly)
return forms