def modelform_factory(model, autocomplete_fields=None, autocomplete_exclude=None, autocomplete_names=None, registry=None, **kwargs): """ Wrap around Django's django_modelform_factory, using our ModelForm and setting autocomplete_fields and autocomplete_exclude. """ if 'form' not in kwargs.keys(): kwargs['form'] = ModelForm attrs = {'model': model} if autocomplete_fields is not None: attrs['autocomplete_fields'] = autocomplete_fields if autocomplete_exclude is not None: attrs['autocomplete_exclude'] = autocomplete_exclude if autocomplete_names is not None: attrs['autocomplete_names'] = autocomplete_names # If parent form class already has an inner Meta, the Meta we're # creating needs to inherit from the parent's inner meta. parent = (object,) if hasattr(kwargs['form'], 'Meta'): parent = (kwargs['form'].Meta, object) Meta = type(str('Meta'), parent, attrs) kwargs['form'] = type(kwargs['form'].__name__, (kwargs['form'],), {'Meta': Meta}) if not issubclass(kwargs['form'], ModelForm): raise Exception('form kwarg must be an autocomplete_light ModelForm') return django_modelform_factory(model, **kwargs)
def modelform_factory(model, autocomplete_exclude=None, registry=None, **kwargs): """ Wraps around Django's django_modelform_factory, using get_widgets_dict. autocomplete_exclude List of model field names to ignore registry Registry to use. Basically, it will use the dict returned by get_widgets_dict in order and pass it to django's modelform_factory, and return the resulting modelform. """ if registry is None: from .registry import registry widgets = get_widgets_dict(model, registry=registry, autocomplete_exclude=autocomplete_exclude) widgets.update(kwargs.pop('widgets', {})) kwargs['widgets'] = widgets if 'form' not in kwargs.keys(): kwargs['form'] = FixedModelForm return django_modelform_factory(model, **kwargs)
def modelform_factory(model, autocomplete_exclude=None, registry=None, **kwargs): """ Wraps around Django's django_modelform_factory, using get_widgets_dict. autocomplete_exclude List of model field names to ignore registry Registry to use. Basically, it will use the dict returned by get_widgets_dict in order and pass it to django's modelform_factory, and return the resulting modelform. """ if registry is None: from .registry import registry widgets = get_widgets_dict(model, registry=registry, autocomplete_exclude=autocomplete_exclude) widgets.update(kwargs.pop('widgets', {})) kwargs['widgets'] = widgets return django_modelform_factory(model, form=FixedModelForm, **kwargs)
def modelform_factory(model, autocomplete_exclude=None, **kwargs): """ Wraps around Django's django_modelform_factory, using get_widgets_dict. Basically, it will use the dict returned by get_widgets_dict in order and pass it to django's modelform_factory, and return the resulting modelform. """ widgets = get_widgets_dict(model, autocomplete_exclude=autocomplete_exclude) widgets.update(kwargs.pop('widgets', {})) kwargs['widgets'] = widgets return django_modelform_factory(model, **kwargs)
def movenodeform_factory(model, form=MoveNodeForm, fields=None, exclude=None, formfield_callback=None, widgets=None): """Dynamically build a MoveNodeForm subclass with the proper Meta. :param Node model: The subclass of :py:class:`Node` that will be handled by the form. :param form: The form class that will be used as a base. By default, :py:class:`MoveNodeForm` will be used. :return: A :py:class:`MoveNodeForm` subclass """ _exclude = _get_exclude_for_model(model, exclude) return django_modelform_factory(model, form, fields, _exclude, formfield_callback, widgets)
def movenodeform_factory(model, form=MoveNodeForm, fields=None, exclude=None, formfield_callback=None, widgets=None): """Dynamically build a MoveNodeForm subclass with the proper Meta. :param model: :return: A MoveNodeForm subclass Example of a generated class:: class AL_TestNodeForm(MoveNodeForm): class Meta: model = models.AL_TestNode exclude = ('sib_order', 'parent') """ _exclude = _get_exclude_for_model(model, exclude) return django_modelform_factory( model, form, fields, _exclude, formfield_callback, widgets)
def modelform_factory(model, **kwargs): '''Build a modelform for the given model, For the user model also add attribute based fields. ''' from authentic2 import models form = kwargs.pop('form', None) fields = kwargs.get('fields') or [] required = list(kwargs.pop('required', []) or []) d = {} # KV attributes are only supported for the user model currently modelform = None if issubclass(model, get_user_model()): if not form: form = BaseUserForm attributes = models.Attribute.objects.all() for attribute in attributes: if attribute.name not in fields: continue d[attribute.name] = attribute.get_form_field() for field in app_settings.A2_REQUIRED_FIELDS: if not field in required: required.append(field) if not form or not hasattr(form, 'Meta'): meta_d = {'model': model, 'fields': '__all__'} meta = type('Meta', (), meta_d) d['Meta'] = meta if not form: # fallback form = forms.ModelForm modelform = None if required: def __init__(self, *args, **kwargs): super(modelform, self).__init__(*args, **kwargs) for field in required: if field in self.fields: self.fields[field].required = True d['__init__'] = __init__ modelform = type(model.__name__ + 'ModelForm', (form, ), d) kwargs['form'] = modelform modelform.required_css_class = 'form-field-required' return django_modelform_factory(model, **kwargs)
def movenodeform_factory(model, form=MoveNodeForm, fields=None, exclude=None, formfield_callback=None, widgets=None): """Dynamically build a MoveNodeForm subclass with the proper Meta. :param Node model: The subclass of :py:class:`Node` that will be handled by the form. :param form: The form class that will be used as a base. By default, :py:class:`MoveNodeForm` will be used. :return: A :py:class:`MoveNodeForm` subclass """ _exclude = _get_exclude_for_model(model, exclude) return django_modelform_factory( model, form, fields, _exclude, formfield_callback, widgets)
def modelform_factory(model, autocomplete_fields=None, autocomplete_exclude=None, autocomplete_names=None, registry=None, **kwargs): """ Wrap around Django's django_modelform_factory, using our ModelForm and setting autocomplete_fields and autocomplete_exclude. """ if 'form' not in kwargs.keys(): kwargs['form'] = ModelForm attrs = {'model': model} if autocomplete_fields is not None: attrs['autocomplete_fields'] = autocomplete_fields if autocomplete_exclude is not None: attrs['autocomplete_exclude'] = autocomplete_exclude if autocomplete_names is not None: attrs['autocomplete_names'] = autocomplete_names # If parent form class already has an inner Meta, the Meta we're # creating needs to inherit from the parent's inner meta. parent = (object, ) if hasattr(kwargs['form'], 'Meta'): parent = (kwargs['form'].Meta, object) Meta = type(str('Meta'), parent, attrs) # We have to handle Meta.fields/Meta.exclude here because else Django will # raise a warning. if 'fields' in kwargs: Meta.fields = kwargs.pop('fields') if 'exclude' in kwargs: Meta.exclude = kwargs.pop('exclude') kwargs['form'] = type(kwargs['form'].__name__, (kwargs['form'], ), {'Meta': Meta}) if not issubclass(kwargs['form'], ModelForm): raise Exception('form kwarg must be an autocomplete_light ModelForm') return django_modelform_factory(model, **kwargs)
def movenodeform_factory(model, form=MoveNodeForm, fields=None, exclude=None, formfield_callback=None, widgets=None): """Dynamically build a MoveNodeForm subclass with the proper Meta. :param model: :return: A MoveNodeForm subclass Example of a generated class:: class AL_TestNodeForm(MoveNodeForm): class Meta: model = models.AL_TestNode exclude = ('sib_order', 'parent') """ _exclude = _get_exclude_for_model(model, exclude) return django_modelform_factory(model, form, fields, _exclude, formfield_callback, widgets)
def modelform_factory(model, **kwargs): '''Build a modelform for the given model, For the user model also add attribute based fields. ''' form = kwargs.pop('form', None) fields = kwargs.get('fields') or [] required = list(kwargs.pop('required', []) or []) d = {} # KV attributes are only supported for the user model currently modelform = None if issubclass(model, get_user_model()): if not form: form = BaseUserForm attributes = models.Attribute.objects.all() for attribute in attributes: if attribute.name not in fields: continue d[attribute.name] = attribute.get_form_field() for field in app_settings.A2_REQUIRED_FIELDS: if not field in required: required.append(field) if not form or not hasattr(form, 'Meta'): meta_d = {'model': model, 'fields': '__all__'} meta = type('Meta', (), meta_d) d['Meta'] = meta if not form: # fallback form = forms.ModelForm modelform = None if required: def __init__(self, *args, **kwargs): super(modelform, self).__init__(*args, **kwargs) for field in required: if field in self.fields: self.fields[field].required = True d['__init__'] = __init__ modelform = type(model.__name__ + 'ModelForm', (form,), d) kwargs['form'] = modelform return django_modelform_factory(model, **kwargs)