app/soc/views/helper/dynaform.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 06 Mar 2009 15:52:12 +0000
changeset 1687 8203c805edc7
parent 1431 7e54ef90c210
child 2077 fd2e83a297c7
permissions -rw-r--r--
Removed commented property from comment model. scope_path is set to the commented Work anyway and back references don't work. This means we can also comment on any other linkable. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
629
492df53e4a0f Add missing dots and fix too long line in soc.views.helper.dynaform module. Remove unused imports in soc.views.models.document module. Fix too long lines. Add missing docstring for Meta classes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 602
diff changeset
    17
"""This module defines classes and functions for Dynamic Forms.
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
    ]
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
from google.appengine.ext.db import djangoforms
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
from soc.logic import dicts
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
class DynaFormMetaclass(djangoforms.ModelFormMetaclass):
629
492df53e4a0f Add missing dots and fix too long line in soc.views.helper.dynaform module. Remove unused imports in soc.views.models.document module. Fix too long lines. Add missing docstring for Meta classes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 602
diff changeset
    31
  """The DynaForm Meta class, adding support for dynamically defined fields.
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
  The new DynaForm class that is created by class function is very
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
  similar to one created by the regular djangoforms.ModelFormMetaclass.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
  The only difference is that is the form class has a Meta property,
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
  it's 'dynaconf' field will be used to define additional properties
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
  in the form class.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
  The 'dynaconf' field (if present), is expected to be iterable as a
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
  dictionary (with iteritems). The keys are used as the property names,
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
  and the values are used as the property value.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
  """
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
  def __new__(cls, class_name, bases, attrs):
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
    """See djangoforms.ModelFormMetaclass on how the __new__ method
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
    is used, for an explanation on how this class modifies the default
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
    behavior, see the DynaFormMetaclass's docstring.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
    """
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
    # Retrieve the Meta class, if present
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
    meta = attrs.get('Meta', None)
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
    conf = None
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
    # Try to retrieve the dynaconf property
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
    if meta:
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
      conf = getattr(meta, 'dynaconf', None)
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
    # When found, extend the form's attribute's with the specified ones
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
    if conf:
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
      for key, value in conf.iteritems():
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
        attrs[key] = value
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
    # Leave the rest to djangoforms.ModelFormMetaclass.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
    return super(DynaFormMetaclass, cls).__new__(cls, class_name, bases, attrs)
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
def newDynaForm(dynamodel=None, dynabase=None, dynainclude=None, 
1430
ff8cc6b15e6a Rename dynafields to dynaproperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    68
                dynaexclude=None, dynaproperties=None):
629
492df53e4a0f Add missing dots and fix too long line in soc.views.helper.dynaform module. Remove unused imports in soc.views.models.document module. Fix too long lines. Add missing docstring for Meta classes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 602
diff changeset
    69
  """Creates a new form DynaForm class.
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
  The returned class extends dynabase, but with the following additions:
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
  * It has a Meta class with the 'model', 'include', and 'exclude'
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
  fields set as specified by newDynaForm's keyword arguments.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
  * It's __metaclass__ is set to DynaFormMetaclass (which inherits from
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
  the default djangoforms.ModelFormMetaclass).
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
  * The Meta class has an additional dynaconf field which is set to
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
  the dyanfields keyword argument passed to newDynaForm.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
1430
ff8cc6b15e6a Rename dynafields to dynaproperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    79
  See DynaFormMetaclass for an explanation on how the dynaproperties
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
  property is used to construct the DynaForm class.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
  """
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
  class DynaForm(dynabase):
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
    """The dynamically created Form class
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
    """
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
    __metaclass__ = DynaFormMetaclass
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
    class Meta:
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
      """Inner Meta class that defines some behavior for the form.
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
      """
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
      model = dynamodel
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
      include = dynainclude
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
      exclude = dynaexclude
1430
ff8cc6b15e6a Rename dynafields to dynaproperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    96
      dynaconf = dynaproperties
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
  return DynaForm
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
629
492df53e4a0f Add missing dots and fix too long line in soc.views.helper.dynaform module. Remove unused imports in soc.views.models.document module. Fix too long lines. Add missing docstring for Meta classes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 602
diff changeset
   101
def extendDynaForm(dynaform, dynainclude=None, dynaexclude=None, 
1430
ff8cc6b15e6a Rename dynafields to dynaproperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   102
                   dynaproperties=None, append=False):
629
492df53e4a0f Add missing dots and fix too long line in soc.views.helper.dynaform module. Remove unused imports in soc.views.models.document module. Fix too long lines. Add missing docstring for Meta classes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 602
diff changeset
   103
  """Extends an existing dynaform.
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
1430
ff8cc6b15e6a Rename dynafields to dynaproperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   105
  If any of dynainclude, dynaexclude or dynaproperties are not present,
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
  they are retrieved from dynaform (if present in it's Meta class).
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
1026
e93271e9c70e Fixed typo's in soc/views/helper/dynaform.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1023
diff changeset
   108
  While it is rather useless to extend from a dynaform that does not have
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
  a Meta class, it is allowed, the resulting DynaForm is the same as if
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
  newDynaForm was called with all extendDynForm's keyword arguments.
1021
969566aa110d Allow appending to dynainclude/-exclude
Sverre Rabbelier <srabbelier@gmail.com>
parents: 629
diff changeset
   111
969566aa110d Allow appending to dynainclude/-exclude
Sverre Rabbelier <srabbelier@gmail.com>
parents: 629
diff changeset
   112
  If append is True, the form's original values for include and
969566aa110d Allow appending to dynainclude/-exclude
Sverre Rabbelier <srabbelier@gmail.com>
parents: 629
diff changeset
   113
  exclude will be appended to the supplied dynainclude and
969566aa110d Allow appending to dynainclude/-exclude
Sverre Rabbelier <srabbelier@gmail.com>
parents: 629
diff changeset
   114
  dynaexclude, which both are still allowed to be None.
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   115
  """
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
  # Try to retrieve the Meta class from the existing dynaform
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
  meta = getattr(dynaform, 'Meta', None)
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
  # If we find one, we can use it to 'extend' from
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
  if meta:
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
    dynamodel = getattr(meta, 'model', None)
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
1023
d849b47645f9 Bugfixes after recent refactoring
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1021
diff changeset
   124
    originclude = getattr(meta, 'include', [])
d849b47645f9 Bugfixes after recent refactoring
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1021
diff changeset
   125
    origexclude = getattr(meta, 'exclude', [])
d849b47645f9 Bugfixes after recent refactoring
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1021
diff changeset
   126
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
    if not dynainclude:
1023
d849b47645f9 Bugfixes after recent refactoring
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1021
diff changeset
   128
      dynainclude = originclude
d849b47645f9 Bugfixes after recent refactoring
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1021
diff changeset
   129
      originclude = []
1021
969566aa110d Allow appending to dynainclude/-exclude
Sverre Rabbelier <srabbelier@gmail.com>
parents: 629
diff changeset
   130
    if not dynaexclude:
1023
d849b47645f9 Bugfixes after recent refactoring
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1021
diff changeset
   131
      dynaexclude = origexclude
d849b47645f9 Bugfixes after recent refactoring
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1021
diff changeset
   132
      origexclude = []
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   133
1021
969566aa110d Allow appending to dynainclude/-exclude
Sverre Rabbelier <srabbelier@gmail.com>
parents: 629
diff changeset
   134
    if append:
1023
d849b47645f9 Bugfixes after recent refactoring
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1021
diff changeset
   135
      dynainclude += originclude
d849b47645f9 Bugfixes after recent refactoring
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1021
diff changeset
   136
      dynaexclude += origexclude
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
1026
e93271e9c70e Fixed typo's in soc/views/helper/dynaform.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1023
diff changeset
   138
    # The most interesting parameter, the 'extra fields' dictionary
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
    dynaconf = getattr(meta, 'dynaconf', {})
1430
ff8cc6b15e6a Rename dynafields to dynaproperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   140
    if not dynaproperties:
ff8cc6b15e6a Rename dynafields to dynaproperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   141
      dynaproperties = dynaconf
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   142
    else:
1430
ff8cc6b15e6a Rename dynafields to dynaproperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   143
      dicts.merge(dynaproperties, dynaconf)
602
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
  # Create a new DynaForm, using the properties we extracted
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
  return newDynaForm(
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
      dynamodel=dynamodel,
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   148
      dynabase=dynaform,
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   149
      dynainclude=dynainclude,
1caee6675fa7 Added a DynaForm module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
      dynaexclude=dynaexclude,
1430
ff8cc6b15e6a Rename dynafields to dynaproperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   151
      dynaproperties=dynaproperties)
1431
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   152
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   153
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   154
class DynaFieldMetaclass(type):
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   155
  """The DynaField Meta class, adding support for dynamic properties.
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   156
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   157
  The new DynaField class that is created by class function is only
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   158
  modified slightly. The only difference is that if the field class has
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   159
  a dynaproperties property, it will be used to define additional properties
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   160
  for the field.
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   161
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   162
  The 'dynaproperties' property (if present), is expected to be iterable as a
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   163
  dictionary (with iteritems). The keys are used as the property names,
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   164
  and the values are used as the property value.
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   165
  """
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   166
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   167
  def __new__(cls, name, bases, attrs):
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   168
    """See djangoforms.ModelFormMetaclass on how the __new__ method
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   169
    is used, for an explanation on how this class modifies the default
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   170
    behavior, see the DynaFormMetaclass's docstring.
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   171
    """
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   172
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   173
    # Retrieve the Meta class, if present
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   174
    dynaproperties = attrs.get('dynaproperties', {})
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   175
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   176
    for key, value in dynaproperties.iteritems():
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   177
      attrs[key] = value
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   178
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   179
    # Leave the rest to type
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   180
    return super(DynaFieldMetaclass, cls).__new__(cls, name, bases, attrs)
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   181
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   182
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   183
def newDynaField(field, base, passthrough):
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   184
  """Creates a new form DynaField class.
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   185
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   186
  The returned class extends base, but with the following additions:
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   187
  * It has a dynaproperties attribute as extracted from field.
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   188
  * It's __metaclass__ is set to DynaFieldMetaclass (which inherits from
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   189
  the default type class).
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   190
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   191
  See DynaFieldMetaclass for an explanation on how the dynaproperties
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   192
  property is used to construct the DynaForm class.
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   193
  """
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   194
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   195
  # pass only known accepted arguments to super
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   196
  init_args = dicts.filter(field, passthrough)
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   197
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   198
  properties = field.copy()
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   199
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   200
  # all pass through arguments are handled by super
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   201
  for key in passthrough:
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   202
    if key in properties:
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   203
      del properties[key]
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   204
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   205
  class DynaField(base):
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   206
    """The dynamically created Field class.
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   207
    """
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   208
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   209
    __metaclass__ = DynaFieldMetaclass
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   210
    dynaproperties = properties
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   211
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   212
    def __init__(self):
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   213
      """Pass through the init args to super.
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   214
      """
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   215
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   216
      super(DynaField, self).__init__(**init_args)
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   217
7e54ef90c210 Added DynaFields to the dynaform module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1430
diff changeset
   218
  return DynaField