Beispiel #1
0
    def formfields(self):
        """Return a list of form fields from the registered fields."""

        formfields = OrderedDict()

        registered_fields = get_fields()

        for field in self.fields:
            field_type = field.get("type")
            field_value = field.get("value")

            # check we have the field
            if field_type not in registered_fields:
                raise AttributeError(
                    "Could not find a registered field of type %s" %
                    field_type)

            # get the field
            registered_cls = registered_fields[field_type]()
            field_name = registered_cls.get_formfield_name(field_value)
            field_cls = registered_cls.get_formfield(field_value)
            formfields[field_name] = field_cls

        # add fields to uniquely identify the form
        formfields["form_id"] = forms.CharField(widget=forms.HiddenInput)
        formfields["form_reference"] = forms.CharField(
            widget=forms.HiddenInput)

        return formfields
    def test_get_form_class(self):
        fields = self.form.get_form_fields()
        form_class = FormBuilder(fields).get_form_class()

        self.assertEqual(len(form_class().fields), 16)

        formfields = form_class().fields

        for name, field in get_fields().items():
            self.assertIn(name, formfields)
            self.assertIsInstance(formfields[name], field().field_class)

        self.assertIsInstance(formfields["form_id"], forms.CharField)
        self.assertIsInstance(formfields["form_reference"], forms.CharField)
Beispiel #3
0
    def __init__(self, local_blocks=None, **kwargs):
        self._constructor_kwargs = kwargs

        # Note, this is calling BaseStreamBlock's super __init__, not FormFieldStreamBlock's.
        # We don't want BaseStreamBlock.__init__() to run, because it tries to assign to self.child_blocks,
        # which we've overridden with a @property. But we DO want Block.__init__() to run.
        super(blocks.BaseStreamBlock, self).__init__()

        self._child_blocks = self.base_blocks.copy()

        for name, field_class in get_fields().items():

            # ensure the field is a subclass of BaseField.
            if not issubclass(field_class, BaseField):
                raise ImproperlyConfigured("'%s' must be a subclass of '%s'" %
                                           (field_class, BaseField))

            # assign the block
            block = field_class().get_form_block()
            block.set_name(name)
            self._child_blocks[name] = block

        self._dependencies = self._child_blocks.values()
Beispiel #4
0
    def formfields(self):
        """ Return a list of form fields from the registered fields. """

        formfields = OrderedDict()

        registered_fields = get_fields()

        for field in self.fields:
            field_type = field.get('type')
            field_value = field.get('value')

            # check we have the field
            if field_type not in registered_fields:
                raise AttributeError(
                    'Could not find a registered field of type %s' %
                    field_type)

            # check there is a label
            if 'label' not in field_value:
                raise AttributeError(
                    'The block for %s must contain a label of type blocks.CharBlock(required=True)'
                    % field_type)

            # slugify the label for the field name
            field_name = get_slug_from_string(field_value.get('label'))

            # get the field
            registered_cls = registered_fields[field_type]()
            field_cls = registered_cls.get_formfield(field_value)
            formfields[field_name] = field_cls

        # add fields to uniquely identify the form
        formfields['form_id'] = forms.CharField(widget=forms.HiddenInput)
        formfields['form_reference'] = forms.CharField(
            widget=forms.HiddenInput)

        return formfields
 def test_field(self):
     self.assertIn("myfield", fields.get_fields())