household_relationship_form.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:eq-survey-runner 作者: ONSdigital 项目源码 文件源码
def generate_relationship_form(block_json, number_of_entries, data, error_messages):

    answer = SchemaHelper.get_first_answer_for_block(block_json)

    class HouseHoldRelationshipForm(FlaskForm):
        question_errors = {}

        def map_errors(self):
            ordered_errors = []

            if self.errors:
                for answer_id, error_list in self.errors.items():
                    for errors in error_list:
                        for error in errors:
                            ordered_errors.append((answer_id, error))

            return ordered_errors

        def answer_errors(self, input_id):
            return [error[1] for error in self.map_errors() if input_id == error[0]]

        def serialise(self, location):
            """
            Returns a list of answers representing the form data
            :param location: The location to associate the form data with
            :return:
            """
            list_field = getattr(self, answer['id'])

            return serialise_relationship_answers(location, answer['id'], list_field.data)

    choices = [('', 'Select relationship')] + build_choices(answer['options'])

    field = FieldList(SelectField(
        label=answer.get('guidance'),
        description=answer.get('label'),
        choices=choices,
        default='',
        validators=get_mandatory_validator(answer, error_messages, 'MANDATORY_TEXTFIELD'),
    ), min_entries=number_of_entries)

    setattr(HouseHoldRelationshipForm, answer['id'], field)

    if data:
        form = HouseHoldRelationshipForm(MultiDict(data))
    else:
        form = HouseHoldRelationshipForm()

    return form
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号