104 selection=self.addSingleField, |
105 selection=self.addSingleField, |
105 pick_multi=self.addMultiField, |
106 pick_multi=self.addMultiField, |
106 pick_quant=self.addQuantField, |
107 pick_quant=self.addQuantField, |
107 ) |
108 ) |
108 |
109 |
109 self.kwargs['data'] = data |
110 # set cleaner methods for fields |
|
111 self.kwargs['data'] = self.setCleaners() |
|
112 |
110 super(SurveyTakeForm, self).__init__(*args, **self.kwargs) |
113 super(SurveyTakeForm, self).__init__(*args, **self.kwargs) |
|
114 |
|
115 def setCleaners(self): |
|
116 """Set cleaner methods for dynamic fields. |
|
117 |
|
118 Used for storing textual input as Text instead of StringProperty. Passing |
|
119 a dict of field names/values (from survey_content/survey_record) as the |
|
120 kwarg 'data', it's possible to set clean_[field_id] methods for validation. |
|
121 This method populates the data dict and uses setattr to add field cleaner |
|
122 methods. |
|
123 """ |
|
124 |
|
125 # prefix for method names |
|
126 clean = 'clean_' |
|
127 |
|
128 # data is passed to super's __init__ as the 'data' kwarg |
|
129 data = {} |
|
130 |
|
131 schema = {} |
|
132 if self.survey_content: |
|
133 schema = eval(self.survey_content.schema) |
|
134 |
|
135 for key, val in schema.items(): |
|
136 if val['type'] == 'long_answer': |
|
137 |
|
138 # store > 500 chars per long answer |
|
139 setattr(self, clean + key, |
|
140 lambda key=key: db.Text(self.cleaned_data.get(key)) |
|
141 ) |
|
142 |
|
143 if val['has_comment']: |
|
144 comment = COMMENT_PREFIX + key |
|
145 |
|
146 #store > 500 chars per comment field |
|
147 setattr(self, clean + comment, |
|
148 lambda comment=comment: db.Text(self.cleaned_data.get(comment)) |
|
149 ) |
|
150 |
|
151 # put comment in self.data |
|
152 data[comment] = getattr(self.survey_record, comment, None) |
|
153 |
|
154 # put user's (record) or default (content) value for field in self.data |
|
155 data[key] = (getattr(self.survey_record, key, None) or |
|
156 getattr(self.survey_content, key)) |
|
157 |
|
158 return data |
111 |
159 |
112 def getFields(self, post_dict=None): |
160 def getFields(self, post_dict=None): |
113 """Build the SurveyContent (questions) form fields. |
161 """Build the SurveyContent (questions) form fields. |
114 |
162 |
115 params: |
163 params: |
180 value = value.split(',') |
228 value = value.split(',') |
181 elif from_content and is_multi: |
229 elif from_content and is_multi: |
182 value = [] |
230 value = [] |
183 |
231 |
184 # record field value for validation |
232 # record field value for validation |
185 #if not from_content: |
233 if not from_content: |
186 self.data[field] = value |
234 self.data[field] = value |
187 |
235 |
188 # find correct field type |
236 # find correct field type |
189 addField = self.fields_map[schema.getType(field)] |
237 addField = self.fields_map[schema.getType(field)] |
190 |
238 |
191 # check if question is required, it's never required when editing |
239 # check if question is required, it's never required when editing |
380 attrs: the attrs for the widget |
428 attrs: the attrs for the widget |
381 tip: tooltip text for this field |
429 tip: tooltip text for this field |
382 """ |
430 """ |
383 attrs['class'] = 'comment' |
431 attrs['class'] = 'comment' |
384 widget = widgets.Textarea(attrs=attrs) |
432 widget = widgets.Textarea(attrs=attrs) |
385 comment_field = CharField(help_text=tip, required=False, |
433 comment_field = CharField(help_text=tip, required=False, |
386 label='Add a Comment (optional)', widget=widget, initial=comment) |
434 label='Add a Comment (optional)', widget=widget, initial=comment) |
387 self.survey_fields[COMMENT_PREFIX + field] = comment_field |
435 self.survey_fields[COMMENT_PREFIX + field] = comment_field |
388 |
436 |
389 |
437 |
390 class Meta(object): |
438 class Meta(object): |