Advertisement
Guest User

Django proposal: class FlexiForm

a guest
May 28th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. class FlexiForm(forms.Form):
  2.     def set_fields(self):
  3.         for (f_name, attributes) in self._thefields.items():
  4.             for (att_name, att_value) in attributes.items():
  5.                 setattr(self._theinstance.fields[f_name], att_name, att_value)
  6.  
  7.     def __init__(self, form, args=None, kwargs=None, fields=None):
  8.         self._theform = form
  9.         self._theargs = args or []
  10.         self._thekwargs = kwargs or {}
  11.         self._thefields = fields or {}
  12.         self.bind()
  13.  
  14.     # !: make it possible to use kwargs here
  15.     def bind(self, *args):
  16.         self._theinstance = self._theform(*(list(args) + self._theargs), **self._thekwargs)
  17.         self.set_fields()
  18.         return self
  19.  
  20.     def __getattr__(self, name):
  21.         return getattr(self._theinstance, name)
  22.  
  23.     def __setattr__(self, name, value):
  24.         inst = self
  25.         if name not in ['_theform', '_theargs', '_thekwargs', '_thefields', '_theinstance']:
  26.             inst = self.__theinstance
  27.         super(FlexiForm, inst).__setattr__(name, value)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement