[Zope-CVS] CVS: Products/CompositePage - composite.py:1.24 interfaces.py:1.14

Shane Hathaway shane at zope.com
Thu Apr 15 18:13:45 EDT 2004


Update of /cvs-repository/Products/CompositePage
In directory cvs.zope.org:/tmp/cvs-serv8050

Modified Files:
	composite.py interfaces.py 
Log Message:
Removed getSlots and newSlot.  Added _usingSlot().

SlotGenerator can't correctly fulfill the contract implied by
getSlots().  Even though slots are persisted, only the template knows
exactly what slots are in use.

To split up SlotGenerator.get(), created _usingSlot().  This puts the
responsibility for tracking what slots are in use back into Composite.
It also makes it more apparent that it is better to replace
SlotGenerator than to subclass it.



=== Products/CompositePage/composite.py 1.23 => 1.24 ===
--- Products/CompositePage/composite.py:1.23	Thu Apr 15 13:41:33 2004
+++ Products/CompositePage/composite.py	Thu Apr 15 18:13:44 2004
@@ -34,41 +34,14 @@
 
 _www = os.path.join(os.path.dirname(__file__), "www")
 
-class SlotCollection(Folder):
-    """Collection of composite slots.
-    """
-    meta_type = "Slot Collection"
-
-    def all_meta_types(self):
-        return Folder.all_meta_types(self, interfaces=(ISlot,))
 
-class SlotGenerator(Acquisition.Explicit):
+class SlotGenerator (Acquisition.Explicit):
     """Automatically makes slots available to the template.
 
     Note: instances of this class are shared across threads.
     """
     _slot_class = Slot
 
-    def getSlots(self, composite=None):
-        if composite is None:
-            composite = self._getComposite()
-        return composite.filled_slots
-
-    def _getComposite(self):
-        return aq_parent(aq_inner(self))
-
-    def newSlot(self, name, composite=None):
-        # Generate a new slot.
-        s = self._slot_class(name)
-        if composite is None:
-            composite = self._getComposite()
-        if composite.isEditing():
-            # Persist the slot.
-            slots = self.getSlots(composite)
-            slots._setObject(s.getId(), s)
-        # else don't persist the slot.
-        return s
-
     def get(self, name, class_name=None, title=None):
         """Returns a slot by name.
 
@@ -77,35 +50,38 @@
         slot at the same time.
         """
         composite = aq_parent(aq_inner(self))
-        if composite._v_slot_specs is not None:
-            # Send the slot specs to the composite.
-            composite._v_slot_specs.append({
-                'name': name,
-                'class_name': class_name,
-                'title': title,
-                })
-        slots = self.getSlots(composite)
+        composite._usingSlot(name, class_name, title)
+        slots = composite.filled_slots
         try:
             return slots[name]
         except (KeyError, AttributeError):
-            return self.newSlot(name, composite).__of__(slots)
+            # Generate a new slot.
+            s = self._slot_class(name)
+            if composite.isEditing():
+                # Persist the slot.
+                slots._setObject(s.getId(), s)
+            # else don't persist the slot.
+            return s.__of__(slots)
 
     __getitem__ = get
 
+
 class CompositeMixin:
-    """ An HTML fragment composed from a template and fragments.
-    """
+    """Base class for editable composite pages.
 
+    Composite pages are assemblies of templates and composite
+    elements.  This base class provides the nuts and bolts of a
+    composite editing interface.
+    """
     meta_type = "Composite"
     __implements__ = IComposite
 
     security = ClassSecurityInfo()
 
-    manage_options = ({"label": "Design",
-                       "action": "manage_designForm",},
-                      {"label": "View",
-                       "action": "view",},
-                      )
+    manage_options = (
+        {"label": "Design", "action": "manage_designForm",},
+        {"label": "View", "action": "view",},
+        )
 
     default_ui = "common"
     template_path = "template"
@@ -194,6 +170,22 @@
             raise CompositeError("No composite_tool found")
         return guarded_getattr(tool.uis, ui)
 
+    def _usingSlot(self, name, class_name, title):
+        """Receives notification that the template is using a slot.
+
+        Even though slots are persisted, only the template knows
+        exactly what slots are in use.  This callback gives the
+        composite a chance to learn about the slots in use.  This is
+        designed for editing purposes, not rendering.
+        """
+        if self._v_slot_specs is not None:
+            # Record the slot spec.
+            self._v_slot_specs.append({
+                'name': name,
+                'class_name': class_name,
+                'title': title,
+                })
+
     security.declareProtected(perm_names.change_composites, "getSlotSpecs")
     def getSlotSpecs(self):
         """Returns the slot specs within the template.
@@ -302,6 +294,15 @@
         return self._v_editing
 
 Globals.InitializeClass(CompositeMixin)
+
+
+class SlotCollection(Folder):
+    """Stored collection of composite slots.
+    """
+    meta_type = "Slot Collection"
+
+    def all_meta_types(self):
+        return Folder.all_meta_types(self, interfaces=(ISlot,))
 
 
 class Composite(CompositeMixin, Folder):


=== Products/CompositePage/interfaces.py 1.13 => 1.14 ===
--- Products/CompositePage/interfaces.py:1.13	Thu Apr 15 13:41:33 2004
+++ Products/CompositePage/interfaces.py	Thu Apr 15 18:13:44 2004
@@ -47,16 +47,6 @@
         """Returns a slot, creating it if it does not yet exist.
         """
 
-    def getSlots(composite=None):
-        """Returns the slots container, which should have at
-        least __getitem__ and raise a KeyError if the slot
-        doesn't exists.
-        """
-
-    def newSlot(name, composite=None):
-        """Create a new slot, and optionally add it to the
-        slots container.
-        """
 
 class ISlot(Interface):
     """A slot in a composite.




More information about the Zope-CVS mailing list