[Zope3-checkins] CVS: Zope3/src/zope/app/utilities - definition.py:1.1.2.2 definition.zcml:1.1.2.2 instance.py:1.1.2.4 instance.zcml:1.1.2.3

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Aug 14 19:46:45 EDT 2003


Update of /cvs-repository/Zope3/src/zope/app/utilities
In directory cvs.zope.org:/tmp/cvs-serv24609/utilities

Modified Files:
      Tag: dreamcatcher-ttwschema-branch
	definition.py definition.zcml instance.py instance.zcml 
Log Message:
Everything works now. To demo it, do:

1. Create a Utility Service.

2. Create a Menu Service

3. Add a Browser Menu and register it as 'add_content'. This will be 
   automated.

4. Create a Persistent Schema Utility (this begs to be renamed). Call it
   'IDocument'. Add some fields.

5. Create a Content Component Defition. Call it 'Document'. 
   Find the IDocument schema and set it to the definitions schema. Make 
   security assertions as desired. Later I will also make all the menu 
   options adjustable.

6. Go to your Content space. There you should see a 'Document' object in 
   the list of available Content Objects. As usual, click on it to create 
   the object.

That's it.

Now it is time to clean up and write tests.


=== Zope3/src/zope/app/utilities/definition.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/app/utilities/definition.py:1.1.2.1	Wed Aug 13 15:53:46 2003
+++ Zope3/src/zope/app/utilities/definition.py	Thu Aug 14 18:46:38 2003
@@ -17,10 +17,13 @@
 """
 from persistence import Persistent
 from persistence.dict import PersistentDict
+from zope.app import zapi
 from zope.app.browser.form.editview import EditView
 from zope.app.browser.form.submit import Update
 from zope.app.component.interfacefield import InterfaceField
 from zope.app.form.utility import setUpWidget
+from zope.app.interfaces.container import IAdding 
+from zope.app.services.menu import LocalBrowserMenuItem
 from zope.app.security.permission import PermissionField
 from zope.app.services.utility import UtilityRegistration
 from zope.context import ContextMethod
@@ -58,11 +61,25 @@
 
 class ContentComponentRegistration(UtilityRegistration):
     """Content Component Registration"""
+
+    menuitem_id = None
+    menu = None
     
     def activated(self):
         """Once activated, we have to register the new Content Object with the
         appropriate menu.
         """
+        service = zapi.getService(self, "BrowserMenu")
+        # XXX: Should use queryInheritedMenu()
+        self.menu = service.queryLocalMenu('add_content')
+        # Creating the menu item
+        # XXX: Should be configurable
+        item = LocalBrowserMenuItem()
+        item.interface = IAdding
+        item.action = 'AddContentComponent/' + self.name
+        item.title = self.name
+        item.permission = 'zope.ManageContent'
+        self.menuitem_id = self.menu.setObject('something', item)
         component = self.getComponent()
         component.name = self.name
     activated = ContextMethod(activated)
@@ -70,6 +87,9 @@
     def deactivated(self):
         """Once activated, we have to unregister the new Content Object with
         the appropriate menu."""
+        self.menu.__delitem__(self.menuitem_id)
+        self.menu = None
+        self.menuitem_id = None 
         component = self.getComponent()
         component.name = '<component not activated>'
     deactivated = ContextMethod(deactivated)


=== Zope3/src/zope/app/utilities/definition.zcml 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/app/utilities/definition.zcml:1.1.2.1	Wed Aug 13 15:53:46 2003
+++ Zope3/src/zope/app/utilities/definition.zcml	Thu Aug 14 18:46:38 2003
@@ -68,6 +68,7 @@
       />
 
   <browser:editform
+      for=".definition.IContentComponent"
       schema=".definition.IContentComponent"
       name="edit.html"
       fields="schema"
@@ -75,11 +76,16 @@
       permission="zope.ManageContent"/>
 
   <browser:editform
+      for=".definition.IContentComponent"
       schema=".definition.IContentComponent"
       name="editpermissions.html"
       template="permission_edit.pt"
       class=".definition.ContentComponentPermissionEdit"
       menu="zmi_views" title="Schema Permissions"
       permission="zope.ManageContent"/>
+
+  <browser:defaultView
+      for=".definition.IContentComponent"
+      name="edit.html" />
 
 </configure>


=== Zope3/src/zope/app/utilities/instance.py 1.1.2.3 => 1.1.2.4 ===
--- Zope3/src/zope/app/utilities/instance.py:1.1.2.3	Wed Aug 13 15:53:46 2003
+++ Zope3/src/zope/app/utilities/instance.py	Thu Aug 14 18:46:38 2003
@@ -97,7 +97,7 @@
 
     def __repr__(self):
         return '<ContentComponentInstance called %s>' %self.__name__
-    
+
 
 def ContentComponentInstanceChecker(instance):
     """A function that can be registered as a Checker in defineChecker()"""
@@ -105,6 +105,62 @@
                    instance.__checker_setattr.get)
 
 defineChecker(ContentComponentInstance, ContentComponentInstanceChecker)
+
+
+from zope.app import zapi
+from zope.app.browser.form.add import AddView
+from zope.publisher.interfaces import IPublishTraverse
+from zope.app.services.servicenames import Utilities
+from zope.app.utilities.definition import IContentComponent
+from zope.component.exceptions import ComponentLookupError
+
+class AddContentComponentView(AddView):
+
+    implements(IPublishTraverse)
+
+    def __init__(self, context, request):
+        self.context = context
+        self.request = request
+
+    def publishTraverse(self, request, name):
+        """See zope.app.interfaces.container.IAdding"""
+        if '=' in name:
+            type_name, content_name = name.split("=", 1)
+            self.context.contentName = content_name
+
+        utilities = zapi.getService(self.context, Utilities)
+        matching = utilities.getRegisteredMatching(IContentComponent,
+                                                   type_name)
+        if not (matching and matching[0][2].active()):
+            raise ComponentLookupError, \
+                  "No Content Component Definition named '%s' found" %type_name
+
+        self.definition = matching[0][2].active().getComponent()
+        self.schema = self.definition.schema
+        self.label = 'Add %s' %self.definition.name
+        super(AddContentComponentView, self).__init__(self.context, request)
+
+        return self.generated_form
+
+    def createAndAdd(self, data):
+        """Create a Content Component Instance and add it to the container."""
+        content = ContentComponentInstance(self.definition.name,
+                                           self.definition.schema,
+                                           self.definition.permissions)
+        errors = []
+        for name, value in data.items():
+            field = self.schema[name]
+            try:
+                field.set(content, data[name])
+            except ValidationError:
+                errors.append(sys.exc_info()[1])
+
+        content = self.add(content)
+
+        if errors:
+            raise WidgetsError(*errors)
+
+        return content        
 
 
 from zope.app.browser.form.editview import EditView


=== Zope3/src/zope/app/utilities/instance.zcml 1.1.2.2 => 1.1.2.3 ===
--- Zope3/src/zope/app/utilities/instance.zcml:1.1.2.2	Wed Aug 13 15:53:46 2003
+++ Zope3/src/zope/app/utilities/instance.zcml	Thu Aug 14 18:46:38 2003
@@ -10,6 +10,13 @@
   </content>
 
   <browser:page
+      name="AddContentComponent"
+      for="zope.app.interfaces.container.IAdding"
+      permission="zope.ManageContent"
+      class=".instance.AddContentComponentView"
+      attribute="generated_form" />
+
+  <browser:page
       name="edit.html"
       menu="zmi_views" title="Edit"
       for=".instance.IContentComponentInstance"
@@ -20,7 +27,7 @@
   <!-- Directives to test the Content Component Instance -->
   <browser:addform
       label="New ContentComponent"
-      name="AddContentComponent"
+      name="AddDirectContentComponent"
       schema=".instance.IContentComponentInstance"
       permission="zope.ManageContent"
       content_factory=".instance.ContentComponentInstance"




More information about the Zope3-Checkins mailing list