python类SelectMultipleField()的实例源码

fields.py 文件源码 项目:eq-survey-runner 作者: ONSdigital 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def get_select_multiple_field(answer, label, guidance, error_messages):
    validate_with = get_mandatory_validator(answer, error_messages, 'MANDATORY_CHECKBOX')

    return SelectMultipleField(
        label=label,
        description=guidance,
        choices=build_choices(answer['options']),
        validators=validate_with,
    )
test_fields.py 文件源码 项目:eq-survey-runner 作者: ONSdigital 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_checkbox_field(self):
        checkbox_json = {
            "guidance": "",
            "id": "opening-crawler-answer",
            "label": "",
            "mandatory": False,
            "options": [
                {
                    "label": "Luke Skywalker",
                    "value": "Luke Skywalker"
                },
                {
                    "label": "Han Solo",
                    "value": "Han Solo"
                },
                {
                    "label": "The Emperor",
                    "value": "The Emperor"
                },
                {
                    "label": "R2D2",
                    "value": "R2D2"
                },
                {
                    "label": "Senator Amidala",
                    "value": "Senator Amidala"
                },
                {
                    "label": "Yoda",
                    "value": "Yoda"
                }
            ],
            "q_code": "7",
            "type": "Checkbox"
        }

        unbound_field = get_field(checkbox_json, checkbox_json['label'], error_messages, self.answer_store)

        expected_choices = [(option['label'], option['value']) for option in checkbox_json['options']]

        self.assertTrue(unbound_field.field_class == SelectMultipleField)
        self.assertEquals(unbound_field.kwargs['label'], checkbox_json['label'])
        self.assertEquals(unbound_field.kwargs['description'], checkbox_json['guidance'])
        self.assertEquals(unbound_field.kwargs['choices'], expected_choices)
forms.py 文件源码 项目:hedhes 作者: Zo0x 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def factory(cls, data, **kwargs):
        for d in data:
            if d.key == 'default_search_quality':
                setattr(cls, d.key, SelectMultipleField(d.name, choices=app.config.get('AVAILABLE_QUALITIES'), default=d.value, validators=[Optional()]))
            elif d.type == 'list':
                setattr(cls, d.key, SelectMultipleField(d.name, default=d.value))
            elif d.type == 'string':
                setattr(cls, d.key, StringField(d.name, default=d.value))
            elif d.type == 'bool':
                setattr(cls, d.key, BooleanField(d.name, default=d.value))
            elif d.type == 'float':
                setattr(cls, d.key, FloatField(d.name, default=d.value))
            elif d.type == 'int':
                setattr(cls, d.key, IntegerField(d.name, default=d.value))
        return cls(**kwargs)
forms.py 文件源码 项目:pillar 作者: armadillica 项目源码 文件源码 阅读 52 收藏 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)


问题


面经


文章

微信
公众号

扫码关注公众号