app/soc/views/helper/dynaform.py
changeset 1430 ff8cc6b15e6a
parent 1308 35b75ffcbb37
child 1431 7e54ef90c210
equal deleted inserted replaced
1429:cfa0c3f2dc19 1430:ff8cc6b15e6a
    63     # Leave the rest to djangoforms.ModelFormMetaclass.
    63     # Leave the rest to djangoforms.ModelFormMetaclass.
    64     return super(DynaFormMetaclass, cls).__new__(cls, class_name, bases, attrs)
    64     return super(DynaFormMetaclass, cls).__new__(cls, class_name, bases, attrs)
    65 
    65 
    66 
    66 
    67 def newDynaForm(dynamodel=None, dynabase=None, dynainclude=None, 
    67 def newDynaForm(dynamodel=None, dynabase=None, dynainclude=None, 
    68                 dynaexclude=None, dynafields=None):
    68                 dynaexclude=None, dynaproperties=None):
    69   """Creates a new form DynaForm class.
    69   """Creates a new form DynaForm class.
    70 
    70 
    71   The returned class extends dynabase, but with the following additions:
    71   The returned class extends dynabase, but with the following additions:
    72   * It has a Meta class with the 'model', 'include', and 'exclude'
    72   * It has a Meta class with the 'model', 'include', and 'exclude'
    73   fields set as specified by newDynaForm's keyword arguments.
    73   fields set as specified by newDynaForm's keyword arguments.
    74   * It's __metaclass__ is set to DynaFormMetaclass (which inherits from
    74   * It's __metaclass__ is set to DynaFormMetaclass (which inherits from
    75   the default djangoforms.ModelFormMetaclass).
    75   the default djangoforms.ModelFormMetaclass).
    76   * The Meta class has an additional dynaconf field which is set to
    76   * The Meta class has an additional dynaconf field which is set to
    77   the dyanfields keyword argument passed to newDynaForm.
    77   the dyanfields keyword argument passed to newDynaForm.
    78 
    78 
    79   See DynaFormMetaclass for an explanation on how the dynafields
    79   See DynaFormMetaclass for an explanation on how the dynaproperties
    80   property is used to construct the DynaForm class.
    80   property is used to construct the DynaForm class.
    81   """
    81   """
    82 
    82 
    83   class DynaForm(dynabase):
    83   class DynaForm(dynabase):
    84     """The dynamically created Form class
    84     """The dynamically created Form class
    91       """
    91       """
    92 
    92 
    93       model = dynamodel
    93       model = dynamodel
    94       include = dynainclude
    94       include = dynainclude
    95       exclude = dynaexclude
    95       exclude = dynaexclude
    96       dynaconf = dynafields
    96       dynaconf = dynaproperties
    97 
    97 
    98   return DynaForm
    98   return DynaForm
    99 
    99 
   100 
   100 
   101 def extendDynaForm(dynaform, dynainclude=None, dynaexclude=None, 
   101 def extendDynaForm(dynaform, dynainclude=None, dynaexclude=None, 
   102                    dynafields=None, append=False):
   102                    dynaproperties=None, append=False):
   103   """Extends an existing dynaform.
   103   """Extends an existing dynaform.
   104 
   104 
   105   If any of dynainclude, dynaexclude or dynafields are not present,
   105   If any of dynainclude, dynaexclude or dynaproperties are not present,
   106   they are retrieved from dynaform (if present in it's Meta class).
   106   they are retrieved from dynaform (if present in it's Meta class).
   107 
   107 
   108   While it is rather useless to extend from a dynaform that does not have
   108   While it is rather useless to extend from a dynaform that does not have
   109   a Meta class, it is allowed, the resulting DynaForm is the same as if
   109   a Meta class, it is allowed, the resulting DynaForm is the same as if
   110   newDynaForm was called with all extendDynForm's keyword arguments.
   110   newDynaForm was called with all extendDynForm's keyword arguments.
   135       dynainclude += originclude
   135       dynainclude += originclude
   136       dynaexclude += origexclude
   136       dynaexclude += origexclude
   137 
   137 
   138     # The most interesting parameter, the 'extra fields' dictionary
   138     # The most interesting parameter, the 'extra fields' dictionary
   139     dynaconf = getattr(meta, 'dynaconf', {})
   139     dynaconf = getattr(meta, 'dynaconf', {})
   140     if not dynafields:
   140     if not dynaproperties:
   141       dynafields = dynaconf
   141       dynaproperties = dynaconf
   142     else:
   142     else:
   143       dicts.merge(dynafields, dynaconf)
   143       dicts.merge(dynaproperties, dynaconf)
   144 
   144 
   145   # Create a new DynaForm, using the properties we extracted
   145   # Create a new DynaForm, using the properties we extracted
   146   return newDynaForm(
   146   return newDynaForm(
   147       dynamodel=dynamodel,
   147       dynamodel=dynamodel,
   148       dynabase=dynaform,
   148       dynabase=dynaform,
   149       dynainclude=dynainclude,
   149       dynainclude=dynainclude,
   150       dynaexclude=dynaexclude,
   150       dynaexclude=dynaexclude,
   151       dynafields=dynafields)
   151       dynaproperties=dynaproperties)