python类FieldList()的实例源码

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))
attachments.py 文件源码 项目:pillar 作者: armadillica 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def attachment_form_group_create(schema_prop):
    """Creates a wtforms.FieldList for attachments."""

    file_select_form_group = _attachment_build_single_field(schema_prop)
    field = wtforms.FieldList(CustomFormField(file_select_form_group), min_entries=1)

    return field
household_relationship_form.py 文件源码 项目:eq-survey-runner 作者: ONSdigital 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
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
forms.py 文件源码 项目:pillar 作者: armadillica 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def add_form_properties(form_class, node_type):
    """Add fields to a form based on the node and form schema provided.
    :type node_schema: dict
    :param node_schema: the validation schema used by Cerberus
    :type form_class: class
    :param form_class: The form class to which we append fields
    :type form_schema: dict
    :param form_schema: description of how to build the form (which fields to
            show and hide)
    """

    for prop_name, schema_prop, form_prop in iter_node_properties(node_type):

        # Recursive call if detects a dict
        field_type = schema_prop['type']

        if field_type == 'dict':
            assert prop_name == 'attachments'
            field = attachments.attachment_form_group_create(schema_prop)
        elif field_type == 'list':
            if prop_name == 'files':
                schema = schema_prop['schema']['schema']
                file_select_form = build_file_select_form(schema)
                field = FieldList(CustomFormField(file_select_form),
                                  min_entries=1)
            elif 'allowed' in schema_prop['schema']:
                choices = [(c, c) for c in schema_prop['schema']['allowed']]
                field = SelectMultipleField(choices=choices)
            else:
                field = SelectMultipleField(choices=[])
        elif 'allowed' in schema_prop:
            select = []
            for option in schema_prop['allowed']:
                select.append((str(option), str(option)))
            field = SelectField(choices=select)
        elif field_type == 'datetime':
            if form_prop.get('dateonly'):
                field = DateField(prop_name, default=date.today())
            else:
                field = DateTimeField(prop_name, default=datetime.now())
        elif field_type == 'integer':
            field = IntegerField(prop_name, default=0)
        elif field_type == 'float':
            field = FloatField(prop_name, default=0.0)
        elif field_type == 'boolean':
            field = BooleanField(prop_name)
        elif field_type == 'objectid' and 'data_relation' in schema_prop:
            if schema_prop['data_relation']['resource'] == 'files':
                field = FileSelectField(prop_name)
            else:
                field = StringField(prop_name)
        elif schema_prop.get('maxlength', 0) > 64:
            field = TextAreaField(prop_name)
        else:
            field = StringField(prop_name)

        setattr(form_class, prop_name, field)


问题


面经


文章

微信
公众号

扫码关注公众号