[Zope3-dev] Extend addform and addwizard directive

Dominik Huber dominik.huber at projekt01.ch
Thu May 19 09:22:23 EDT 2005


I would like to add an additional 'content_factory_id' attribute to the 
addform- addwizard directive in order that utility based factories can 
be invoked within the adding view. See diff for details.

Any objections?

Regards,
Dominik Huber


-------------- next part --------------
Index: metadirectives.py
===================================================================
--- metadirectives.py	(revision 30417)
+++ metadirectives.py	(working copy)
@@ -179,6 +179,12 @@
         required=False
         )
 
+    content_factory_id = Id(
+        title=u"Content factory id",
+        description=u"A factory id to create new content objects",
+        required = False,
+        )
+
     arguments = Tokens(
         title=u"Arguments",
         description=u"""
Index: metaconfigure.py
===================================================================
--- metaconfigure.py	(revision 30417)
+++ metaconfigure.py	(working copy)
@@ -177,6 +177,7 @@
 
     # default add form information
     description = None
+    content_factory_id = None
     content_factory = None
     arguments = None
     keyword_arguments = None
@@ -252,9 +253,14 @@
         else:
             self.set_after_add = leftover
 
+    def _handle_content_factory(self):
+        if self.content_factory is None:
+            self.content_factory = self.content_factory_id
+
     def __call__(self):
         self._processWidgets()
         self._handle_menu()
+        self._handle_content_factory()
         self._handle_arguments()
 
         self._context.action(
@@ -338,6 +344,7 @@
 
     def __call__(self):
         self._handle_menu()
+        self._handle_content_factory()
 
         all_fields = self.fields
         leftover = []
Index: add.py
===================================================================
--- add.py	(revision 30417)
+++ add.py	(working copy)
@@ -20,6 +20,7 @@
 import sys
 
 from zope.app import zapi
+from zope.component.interfaces import IFactory
 from zope.event import notify
 from zope.interface import Interface
 
@@ -133,7 +134,20 @@
     def nextURL(self):
         return self.context.nextURL()
 
+# helper for factory resp. content_factory handling
+def _getFactory(self):
+    # get factory or factory id
+    factory = self.__dict__.get('_factory_or_id', self._factory_or_id)
 
+    if type(factory) is str: # factory id
+        return zapi.getUtility(IFactory, factory, self.context)
+    else:
+        return factory
+
+def _setFactory(self, value):
+    self.__dict__['_factory_or_id'] = value
+
+
 def AddViewFactory(name, schema, label, permission, layer,
                    template, default_template, bases, for_,
                    fields, content_factory, arguments,
@@ -146,7 +160,8 @@
     class_.schema = schema
     class_.label = label
     class_.fieldNames = fields
-    class_._factory = content_factory
+    class_._factory_or_id = content_factory
+    class_._factory = property(_getFactory, _setFactory)
     class_._arguments = arguments
     class_._keyword_arguments = keyword_arguments
     class_._set_before_add = set_before_add
Index: addwizard.py
===================================================================
--- addwizard.py	(revision 30417)
+++ addwizard.py	(working copy)
@@ -20,7 +20,10 @@
 import sys
 
 from zope.app import zapi
+from zope.component.interfaces import IFactory
 from zope.event import notify
+from zope.interface import Interface
+
 from zope.app.event.objectevent import ObjectCreatedEvent
 from zope.app.form.utility import setUpWidgets
 from zope.app.form.interfaces import WidgetsError, IInputWidget
@@ -108,6 +111,21 @@
         self.request.response.redirect(self.context.nextURL())
         return False
 
+
+# helper for factory resp. content_factory handling
+def _getFactory(self):
+    # get factory or factory id
+    factory = self.__dict__.get('_factory_or_id', self._factory_or_id)
+
+    if type(factory) is str: # factory id
+        return zapi.getUtility(IFactory, factory, self.context)
+    else:
+        return factory
+
+def _setFactory(self, value):
+    self.__dict__['_factory_or_id'] = value
+
+
 # XXX: Needs unittest
 def AddWizardViewFactory(
     name, schema, permission, layer, panes, fields,
@@ -120,7 +138,8 @@
     class_.schema = schema
     class_.panes = panes
     class_.fieldNames = fields
-    class_._factory = content_factory
+    class_._factory_or_id = content_factory
+    class_._factory = property(_getFactory, _setFactory)
     class_._arguments = arguments or []
     class_._keyword_arguments = keyword_arguments or []
     class_._set_before_add = set_before_add or []


More information about the Zope3-dev mailing list