Make forms generic
authorSverre Rabbelier <srabbelier@gmail.com>
Sat, 29 Nov 2008 00:12:47 +0000
changeset 606 65d35584ee31
parent 605 4a384d412640
child 607 7e560d8cc035
Make forms generic Currently only Sponsor uses the new generic forms, as such this makes the form code more complex. The other forms, however, can be converted in a similar way, which will result in a lot of code reduction. Patch by: Sverre Rabbelier
app/soc/logic/models/base.py
app/soc/views/models/base.py
app/soc/views/models/sponsor.py
--- a/app/soc/logic/models/base.py	Sat Nov 29 00:12:16 2008 +0000
+++ b/app/soc/logic/models/base.py	Sat Nov 29 00:12:47 2008 +0000
@@ -63,6 +63,12 @@
     else:
       self._skip_properties = []
 
+  def getModel(self):
+    """Returns the model this logic class uses
+    """
+
+    return self._model
+
   def _updateField(self, model, name, value):
     """Hook called when a field is updated.
 
--- a/app/soc/views/models/base.py	Sat Nov 29 00:12:16 2008 +0000
+++ b/app/soc/views/models/base.py	Sat Nov 29 00:12:47 2008 +0000
@@ -25,6 +25,7 @@
 
 
 from django import http
+from django import forms
 from django.conf.urls import defaults
 from django.utils.translation import ugettext_lazy
 
@@ -33,12 +34,14 @@
 import soc.views.helper.lists
 import soc.views.helper.responses
 
+from soc.logic import cleaning
 from soc.logic import dicts
 from soc.logic import models
 from soc.models import linkable
 from soc.views import helper
 from soc.views import out_of_band
 from soc.views.helper import access
+from soc.views.helper import dynaform
 
 
 class View(object):
@@ -163,9 +166,48 @@
         self.DEF_SUBMIT_MSG_PARAM_NAME: self.DEF_SUBMIT_MSG_PROFILE_SAVED,
         }
 
+    new_params['dynabase'] = helper.forms.BaseForm
+
+    new_params['create_dynainclude'] = [] + params.get('extra_dynainclude', [])
+    new_params['create_dynaexclude'] = ['scope', 'scope_path'] + \
+        params.get('extra_dynaexclude', [])
+    new_params['create_dynafields'] = {
+        'clean_link_id': cleaning.clean_new_link_id(params['logic']),
+        'clean_feed_url': cleaning.clean_feed_url,
+        }
+
+    dynafields = {
+        'clean_link_id': cleaning.clean_link_id,
+        'link_id': forms.CharField(widget=helper.widgets.ReadOnlyInput()),
+        }
+    dynafields.update(params.get('extra_dynafields', {}))
+
+    new_params['edit_dynainclude'] = None
+    new_params['edit_dynaexclude'] = None
+    new_params['edit_dynafields'] = dynafields
+
     self._params = dicts.merge(params, new_params)
     self._logic = self._params['logic']
 
+    # These need to be constructed seperately, because they require
+    # parameters that can be defined either in params, or new_params.
+    if 'create_form' not in self._params:
+      self._params['create_form'] = dynaform.newDynaForm(
+        dynabase = self._params['dynabase'],
+        dynamodel = self._logic.getModel(),
+        dynainclude = self._params['create_dynainclude'],
+        dynaexclude = self._params['create_dynaexclude'],
+        dynafields = self._params['create_dynafields'],
+        )
+
+    if 'edit_form' not in self._params:
+      self._params['edit_form'] = dynaform.extendDynaForm(
+        dynaform = self._params['create_form'],
+        dynainclude = self._params['edit_dynainclude'],
+        dynaexclude = self._params['edit_dynaexclude'],
+        dynafields = self._params['edit_dynafields'],
+        )
+
   def public(self, request, page_name=None, params=None, **kwargs):
     """Displays the public page for the entity specified by **kwargs.
 
--- a/app/soc/views/models/sponsor.py	Sat Nov 29 00:12:16 2008 +0000
+++ b/app/soc/views/models/sponsor.py	Sat Nov 29 00:12:47 2008 +0000
@@ -30,10 +30,8 @@
 from django.utils.translation import ugettext_lazy
 
 from soc.logic import dicts
-from soc.logic import cleaning
 from soc.logic import models
 from soc.views import helper
-from soc.views.helper import dynaform
 from soc.views.helper import widgets
 from soc.views.models import base
 
@@ -44,28 +42,6 @@
 import soc.views.helper.widgets
 
 
-CreateForm = dynaform.newDynaForm(
-    dynabase = helper.forms.BaseForm,
-    dynamodel = soc.models.sponsor.Sponsor,
-    dynaexclude = ['scope', 'scope_path', 'founder', 'home'],
-    dynafields = {
-        'clean_link_id': cleaning.clean_new_link_id(models.sponsor.logic),
-        'clean_feed_url': cleaning.clean_feed_url,
-        },
-    )
-
-
-EditForm = dynaform.extendDynaForm(
-    dynaform = CreateForm,
-    dynafields = {
-         'clean_link_id': cleaning.clean_link_id,
-        'link_id': forms.CharField(widget=helper.widgets.ReadOnlyInput()),
-        'founded_by': forms.CharField(widget=helper.widgets.ReadOnlyInput(),
-                               required=False),
-        },
-    )
-
-
 class View(base.View):
   """View methods for the Sponsor model.
   """
@@ -89,9 +65,12 @@
     # solution that will be implemented in base View.
     params['url_name'] = "sponsor"
     params['module_name'] = "sponsor"
-       
-    params['edit_form'] = EditForm
-    params['create_form'] = CreateForm
+
+    params['extra_dynaexclude'] = ['founder', 'home']
+    params['extra_dynafields'] = {
+        'founded_by': forms.CharField(widget=helper.widgets.ReadOnlyInput(),
+                                   required=False),
+        }
 
     # TODO(tlarsen): Add support for Django style template lookup
     params['edit_template'] = 'soc/sponsor/edit.html'