-
Notifications
You must be signed in to change notification settings - Fork 0
/
3v
200 lines (180 loc) · 6.17 KB
/
3v
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
#TODO create a class
#TODO make private functions
#--- Multiple forms snippet ------------------------------------
TOTAL = 5
def _get_empty_forms(modelform, total=TOTAL):
'''Returns a list of empty modelforms.
Input:
modelform: one of your forms, f.e. MyForm()
total: the number of MyForms you want to put in the list.
'''
forms = []
for i in range(total):
form = modelform(prefix=i)
forms.append(form)
return forms
def get_default_forms(modelform, defaults, total=TOTAL):
'''Returns a list of modelforms with default values.
Input:
modelform: one of your forms, f.e. MyForm()
defaults: a list of dictionaries, f.e.:
defaults = [
{'type': 'Timber products'},
{'type': 'Fuelwood'},
{'type': 'Non-wood forest products'},
{'type': 'Water', 'available': 'N/A'},
{'type': 'Carbon', 'available': 'N/A'},
]
total: the number of MyForms you want to put in the list.
'''
forms = _get_empty_forms(modelform, total)
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_db_forms(modelform, queryset, defaults, total=TOTAL):
'''
Returns a list of modelforms filled with queryset values,
plus at least one extra form, so that the total number of
forms is at least total.
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 = [
{'type': 'Timber products'},
{'type': 'Fuelwood'},
{'type': 'Non-wood forest products'},
{'type': 'Water', 'available': 'N/A'},
{'type': 'Carbon', 'available': 'N/A'},
]
total: the number of MyForms you want to put in the list.
'''
forms = get_default_forms(modelform, defaults, total)
i = 0
for row in queryset:
forms[i] = modelform(initial=vars(row), prefix=i, instance=row)
i += 1
if i >= total:
forms.append(modelform(prefix=i))
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.'''
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_nr_of_forms(queryset, total=TOTAL):
'''Returns the total number of form used, which equals
total, except when the queryset length is total or larger.'''
nr_of_forms = len(queryset) + 1
if nr_of_forms < total:
nr_of_forms = total
return nr_of_forms
def get_posted_forms(modelform, data, queryset, total=TOTAL, 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 = [
{'type': 'Timber products'},
{'type': 'Fuelwood'},
{'type': 'Non-wood forest products'},
{'type': 'Water', 'available': 'N/A'},
{'type': 'Carbon', 'available': 'N/A'},
]
total: the number of MyForms you want to put in the list.'''
nr_of_forms = get_nr_of_forms(queryset, total)
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, total=TOTAL, 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 = [
{'type': 'Timber products'},
{'type': 'Fuelwood'},
{'type': 'Non-wood forest products'},
{'type': 'Water', 'available': 'N/A'},
{'type': 'Carbon', 'available': 'N/A'},
]
total: the number of MyForms you want to put in the list.'''
"""
nr_of_forms = get_nr_of_forms(queryset, total)
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