Esempio n. 1
0
    def get_fields(self, fields):
        from uliweb.utils.generic import make_form_field

        s = []
        for f in fields:
            if isinstance(f, (tuple, list)) and isinstance(f[1], BaseField):
                f[1].name = f[0]
                d = f[1].to_json()
                s.append(d)
            elif isinstance(f, dict):
                p = self.model.properties.get(f['name'])
                if p:
                    field = make_form_field(f, self.model)
                    j = field.to_json()
                    j.update(f)

                    s.append(j)
                else:
                    if 'type' not in f:
                        raise ValueError("Column definition should has 'type' property, but not found in {!r}".format(f))
                    s.append(f)
            elif isinstance(f, (str, unicode)):
                if f not in self.model.properties:
                    raise ValueError("Column {} should be defined in Model {}".format(f, self.model.__name__))
                field = make_form_field(f, self.model)
                j = field.to_json()
                j['name'] = f

                s.append(j)
            else:
                raise QueryViewError("Form field data format {!r} is not support, should"
                                    "be ('name', Field) or {}".format(f))
        self.fields = s
        return self.fields
Esempio n. 2
0
    def _make_form_field(self, model, field):
        from uliweb.utils.generic import make_form_field
        from uliweb.form import get_field_cls

        if isinstance(field, str):
            return field, make_form_field(field, model, use_default_value=False)
        elif isinstance(field, dict):
            if 'field' in field:
                return field['name'], field['field']
            else:
                _type = field.get('type')
                if _type:
                    field_cls = get_field_cls(_type)
                else:
                    field_cls = None
                field['extra'] = field.get('kwargs', {})
                return field['name'], make_form_field(field, model=model,
                                                      use_default_value=False,
                                                      field_cls=field_cls)
        else:
            raise ValueError("Field type is not right, should be str or dict, but %r found" % type(field))
Esempio n. 3
0
    def get_fields(self, fields):
        from uliweb.utils.generic import make_form_field, get_field_model

        s = []
        for f in fields:
            if isinstance(f, (tuple, list)) and isinstance(f[1], BaseField):
                f[1].name = f[0]
                d = f[1].to_json()
                s.append(d)
            elif isinstance(f, dict):
                col, model = get_field_model(f['name'], self.model)
                if col:
                    field = make_form_field(f, model)
                    j = field.to_json()
                    j.update(f)

                    s.append(j)
                else:
                    if 'type' not in f:
                        raise ValueError(
                            "Column definition should has 'type' property, but not found in {!r}"
                            .format(f))
                    s.append(f)
            elif isinstance(f, (str, unicode)):
                col, model = get_field_model(f, self.model)
                if not col:
                    raise ValueError(
                        "Column {} could not be found in {}".format(
                            f, self.model.__name__))
                field = make_form_field(f, model)
                j = field.to_json()
                j['name'] = f

                s.append(j)
            else:
                raise QueryViewError(
                    "Form field data format {!r} is not support, should"
                    "be ('name', Field) or {{}}".format(f))
        self.fields = s
        return self.fields