Generate a dictionary of fields for a given Peewee model.

    See `model_form` docstring for description of parameters.
333169
def model_fields(model, allow_pk=False, only=None, exclude=None,
                 field_args=None, converter=None):
    
    converter = converter or ModelConverter()
    field_args = field_args or {}

    model_fields = list(model._meta.sorted_fields)
    if not allow_pk:
        model_fields.pop(0)

    if only:
        model_fields = [x for x in model_fields if x.name in only]
    elif exclude:
        model_fields = [x for x in model_fields if x.name not in exclude]

    field_dict = {}
    for model_field in model_fields:
        name, field = converter.convert(
            model,
            model_field,
            field_args.get(model_field.name))
        field_dict[name] = field

    return field_dict
140374
def model_field_attr(model, model_field, attr):
    
    fields = dict([(field.name, field) for field in model._meta.fields])
    return getattr(fields[model_field], attr)
183007
def forms(self):
        
        forms = MultiDict()
        for name, item in self.POST.iterallitems():
            if not hasattr(item, 'filename'):
                forms[name] = item
        return forms
416952
def bind_fields_to_model_cls(cls, model_fields):
        """"""
        return dict(
            (field.name, field.bind_model_cls(cls)) for field in model_fields)
46938
def update_model(self, model):
        
        for k, v in self.parse_form().items():
            setattr(model, k, v)
578252
def field_pklist_to_json(self, model, pks):
        
        app_label = model._meta.app_label
        model_name = model._meta.model_name
        return {
            'app': app_label,
            'model': model_name,
            'pks': list(pks),
        }
1197376
def generate(self, field_name, field):
        

        if hasattr(self, 'generate_%s' % field.__class__.__name__.lower()):
            generator = getattr(
                self,
                'generate_%s' % field.__class__.__name__.lower())
            return generator(
                field_name,
                field,
                (field.verbose_name or field_name).capitalize())
        else:
            raise NotImplementedError('%s is not supported by MongoForm' % \
                field.__class__.__name__)
1148689
def _cache_form_details(self, form):
        
        cache = FormCache()
        form['model']['form_key'] = cache.form_id
        form['model']['form_name'] = self.__class__.__name__
        cache.set(
            {
                'model': list(form['model'].keys()),  # In Python 3, dictionary keys are not serializable
                'non_data_fields': self.non_data_fields
            }
        )
990703
def add_fields(self, field_dict):
    
    for key, field in field_dict.items():
      self.add_field(key, field)
674162
def get_model_fields(model, add_reserver_flag=True):
    
    import uliweb.orm as orm

    fields = []
    m = {'type':'type_name', 'hint':'hint',
         'default':'default', 'required':'required'}
    m1 = {'index':'index', 'unique':'unique'}
    for name, prop in model.properties.items():
        if name == 'id':
            continue
        d = {}
        for k, v in m.items():
            d[k] = getattr(prop, v)
        for k, v in m1.items():
            d[k] = bool(prop.kwargs.get(v))
        d['name'] = prop.fieldname or name
        d['verbose_name'] = unicode(prop.verbose_name)
        d['nullable'] = bool(prop.kwargs.get('nullable', orm.__nullable__))
        if d['type'] in ('VARCHAR', 'CHAR', 'BINARY', 'VARBINARY'):
            d['max_length'] = prop.max_length
        if d['type'] in ('Reference', 'OneToOne', 'ManyToMany'):
            d['reference_class'] = prop.reference_class
            #collection_name will be _collection_name, it the original value
            d['collection_name'] = prop._collection_name
        d['server_default'] = prop.kwargs.get('server_default')
        d['_reserved'] = True
        fields.append(d)

    return fields
400038
def get_form_kwargs(self):
        
        kwargs = super(ModelFormMixin, self).get_form_kwargs()
        kwargs['poll'] = self.object
        return kwargs
111031
def create_model(cls, name, field=None):
        
        if field:
            attrs = {'name': name, 'field': field}
        else:
            attrs = {'name': name, 'field': {'_id': ObjectId()}}

        return type(str(name), (cls, ), attrs)()
1233752
def initialize_form(self, instance, model, data=None, extra=None):
        
        model_fields = self.get_fields_from_model(model, self._filter_fields)

        forms = []
        if instance:
            for field_data in instance.list_fields():
                forms.append(
                    AdvancedFilterQueryForm._parse_query_dict(
                        field_data, model))

        formset = AFQFormSetNoExtra if not extra else AFQFormSet
        self.fields_formset = formset(
            data=data,
            initial=forms or None,
            model_fields=model_fields
        )
1280706
def fields_for(context, form, template="includes/form_fields.html"):
    
    context["form_for_fields"] = form
    return get_template(template).render(context)
776558
def _form_loader(self, _):
        '''
        
        '''
        data = {}
        for key in self.request.arguments:
            val = self.get_arguments(key)
            if len(val) == 1:
                data[key] = val[0]
            else:
                data[key] = val
        return data
1137807
def field_dict(self, model):
        
        if self.field_filter:
            return dict(
                [(f.name, f) for f in model._meta.fields
                 if self.field_filter(f)]
            )
        else:
            return dict(
                [(f.name, f) for f in model._meta.fields
                 if not f.rel and
                    not f.primary_key and
                    not f.unique and
                    not isinstance(f, (models.AutoField, models.TextField))]
            )
983649
def formfield(self, **kwargs):
        
        from yacms.generic.forms import KeywordsWidget
        kwargs["widget"] = KeywordsWidget
        return super(KeywordsField, self).formfield(**kwargs)