def test_date_field(self):
date_json = {
"guidance": "Please enter a date",
"id": "period-to",
"label": "Period to",
"mandatory": True,
"type": "Date",
"validation": {
"messages": {
"INVALID_DATE": "The date entered is not valid. Please correct your answer.",
"MANDATORY": "Please provide an answer to continue."
}
}
}
unbound_field = get_field(date_json, date_json['label'], error_messages, self.answer_store)
self.assertTrue(unbound_field.field_class == FormField)
self.assertEquals(unbound_field.kwargs['label'], date_json['label'])
self.assertEquals(unbound_field.kwargs['description'], date_json['guidance'])
python类FormField()的实例源码
def test_month_year_date_field(self):
date_json = {
"guidance": "",
"id": "month-year-answer",
"label": "Date",
"mandatory": True,
"options": [],
"q_code": "11",
"type": "MonthYearDate",
"validation": {
"messages": {
"INVALID_DATE": "The date entered is not valid. Please correct your answer.",
"MANDATORY": "Please provide an answer to continue."
}
}
}
unbound_field = get_field(date_json, date_json['label'], error_messages, self.answer_store)
self.assertTrue(unbound_field.field_class == FormField)
self.assertEquals(unbound_field.kwargs['label'], date_json['label'])
self.assertEquals(unbound_field.kwargs['description'], date_json['guidance'])
def setUp(self):
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
class UserForm(BaseForm):
user_id = IntegerField()
timeout = StringField()
TemplateDestinationForm.user = FormField(UserForm)
def get_date_field(answer, label, guidance, error_messages):
return FormField(
get_date_form(answer, error_messages=error_messages),
label=label,
description=guidance,
)
def get_month_year_field(answer, label, guidance, error_messages):
return FormField(
get_month_year_form(answer, error_messages),
label=label,
description=guidance,
)
household_composition_form.py 文件源码
项目:eq-survey-runner
作者: ONSdigital
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def generate_household_composition_form(block_json, data, error_messages):
class HouseHoldCompositionForm(FlaskForm):
question_errors = {}
household = FieldList(FormField(get_name_form(block_json, error_messages)), min_entries=1)
def map_errors(self):
ordered_errors = []
if 'household' in self.errors and self.errors['household']:
for index, field in enumerate(self.household):
ordered_errors += map_field_errors(field.errors, index)
return ordered_errors
def answer_errors(self, input_id):
return [error[1] for error in self.map_errors() if input_id == error[0]]
def remove_person(self, index_to_remove):
popped = []
while index_to_remove != len(self.household.data):
popped.append(self.household.pop_entry())
popped.reverse()
for field in popped[1:]:
self.household.append_entry(field.data)
def serialise(self, location):
"""
Returns a list of answers representing the form data
:param location: The location to associate the form data with
:return:
"""
return serialise_composition_answers(location, self.household.data)
return HouseHoldCompositionForm(MultiDict(data))