[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/browser/ Add content_factory_id attribute to the addform

Dominik Huber dominik.huber at projekt01.ch
Mon May 23 05:46:24 EDT 2005


Log message for revision 30479:
  Add content_factory_id attribute to the addform
  and addwizard directive so that named factories
  can be invoked too.
  

Changed:
  U   Zope3/trunk/src/zope/app/form/browser/add.py
  U   Zope3/trunk/src/zope/app/form/browser/addwizard.py
  U   Zope3/trunk/src/zope/app/form/browser/metaconfigure.py
  U   Zope3/trunk/src/zope/app/form/browser/metadirectives.py
  U   Zope3/trunk/src/zope/app/form/browser/tests/test_add.py

-=-
Modified: Zope3/trunk/src/zope/app/form/browser/add.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/add.py	2005-05-23 06:26:01 UTC (rev 30478)
+++ Zope3/trunk/src/zope/app/form/browser/add.py	2005-05-23 09:46:14 UTC (rev 30479)
@@ -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

Modified: Zope3/trunk/src/zope/app/form/browser/addwizard.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/addwizard.py	2005-05-23 06:26:01 UTC (rev 30478)
+++ Zope3/trunk/src/zope/app/form/browser/addwizard.py	2005-05-23 09:46:14 UTC (rev 30479)
@@ -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 []

Modified: Zope3/trunk/src/zope/app/form/browser/metaconfigure.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/metaconfigure.py	2005-05-23 06:26:01 UTC (rev 30478)
+++ Zope3/trunk/src/zope/app/form/browser/metaconfigure.py	2005-05-23 09:46:14 UTC (rev 30479)
@@ -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 = []

Modified: Zope3/trunk/src/zope/app/form/browser/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/metadirectives.py	2005-05-23 06:26:01 UTC (rev 30478)
+++ Zope3/trunk/src/zope/app/form/browser/metadirectives.py	2005-05-23 09:46:14 UTC (rev 30479)
@@ -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"""

Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_add.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_add.py	2005-05-23 06:26:01 UTC (rev 30478)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_add.py	2005-05-23 09:46:14 UTC (rev 30479)
@@ -17,23 +17,27 @@
 """
 import unittest
 
-from zope.app import zapi
-from zope.app.testing import ztapi
+from zope.component.interfaces import IFactory
+from zope.component.interfaces import ISiteManager
+from zope.component.factory import Factory
 from zope.interface import Interface, implements
+from zope.publisher.browser import TestRequest
 from zope.publisher.interfaces.browser import IBrowserRequest
 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
-from zope.publisher.browser import TestRequest
 from zope.schema import TextLine, accessors
 from zope.security.checker import CheckerPublic
 
+from zope.app import zapi
+from zope.app.component.site import SiteManagerAdapter
+from zope.app.container.interfaces import IAdding
 from zope.app.event.tests.placelesssetup import getEvents
 from zope.app.event.interfaces import IObjectCreatedEvent, IObjectModifiedEvent
+from zope.app.form import CustomWidgetFactory
+from zope.app.form.browser import TextWidget as Text
 from zope.app.form.browser.add import AddViewFactory, AddView
 from zope.app.form.browser.metaconfigure import AddFormDirective
-from zope.app.container.interfaces import IAdding
-from zope.app.form import CustomWidgetFactory
-from zope.app.form.browser import TextWidget as Text
 from zope.app.form.browser.submit import Update
+from zope.app.testing import ztapi
 from zope.app.testing.placelesssetup import PlacelessSetup
 
 # Foo needs to be imported as globals() are checked
@@ -190,6 +194,44 @@
 
         return args
 
+    def test_add_content_factory_id(self, args=None):
+        self._invoke_add(content_factory='C')
+        (descriminator, callable, args, kw) = self._context.last_action
+
+        self.assertEqual(descriminator,
+                         ('view', IAdding, 'addthis', IBrowserRequest, 
+                         IDefaultBrowserLayer))
+
+        self.assertEqual(callable, AddViewFactory)
+
+        (name, schema, label, permission, layer, template,
+         default_template, bases, for_, fields, content_factory,
+         arguments, keyword_arguments, set_before_add,
+         set_after_add)  = args
+
+        self.assertEqual(name, 'addthis')
+        self.assertEqual(schema, I)
+        self.assertEqual(label, 'Add this')
+        self.assertEqual(permission, CheckerPublic) # 'zope.Public' translated
+        self.assertEqual(layer, IDefaultBrowserLayer)
+        self.assertEqual(template, 'add.pt')
+        self.assertEqual(default_template, 'add.pt')
+        self.assertEqual(bases, (V, AddView, ))
+        self.assertEqual(for_, IAdding)
+        self.assertEqual(" ".join(fields),
+                         "name first last email address getfoo extra1 extra2")
+        self.assertEqual(content_factory, 'C')
+        self.assertEqual(" ".join(arguments),
+                         "first last")
+        self.assertEqual(" ".join(keyword_arguments),
+                         "email")
+        self.assertEqual(" ".join(set_before_add),
+                         "getfoo")
+        self.assertEqual(" ".join(set_after_add),
+                         "extra1 name address extra2")
+
+        return args
+
     def test_create(self):
 
         class Adding(object):
@@ -223,6 +265,43 @@
         self.assertEqual(content.args, ('a', 0))
         self.assertEqual(content.kw, {'abc':'def'})
 
+    def test_create_content_factory_id(self):
+
+        class Adding(object):
+
+            implements(IAdding)
+
+            def __init__(self, test):
+                self.test = test
+
+            def add(self, ob):
+                self.ob = ob
+                self.test.assertEqual(
+                    ob.__dict__,
+                    {'args': ("bar", "baz"),
+                     'kw': {'email': 'baz at dot.com'},
+                     '_foo': 'foo',
+                    })
+                return ob
+            def nextURL(self):
+                return "."
+
+        # register content factory for content factory id lookup
+        ztapi.provideAdapter(None, ISiteManager, SiteManagerAdapter)
+        ztapi.provideUtility(IFactory, Factory(C), name='C')
+        
+        adding = Adding(self)
+        self._invoke_add(content_factory='C')
+        (descriminator, callable, args, kw) = self._context.last_action
+        factory = AddViewFactory(*args)
+        request = TestRequest()
+        view = zapi.getMultiAdapter((adding, request), name='addthis')
+        content = view.create('a',0,abc='def')
+
+        self.failUnless(isinstance(content, C))
+        self.assertEqual(content.args, ('a', 0))
+        self.assertEqual(content.kw, {'abc':'def'})
+
     def test_createAndAdd(self):
 
         class Adding(object):



More information about the Zope3-Checkins mailing list