[Zope-CVS] CVS: Products/CompositePage - composite.py:1.22 element.py:1.5 interfaces.py:1.12 slot.py:1.20

Sidnei da Silva sidnei at awkly.org
Wed Apr 14 12:15:30 EDT 2004


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

Modified Files:
	composite.py element.py interfaces.py slot.py 
Log Message:
Split interesting parts of Composite into a base mixin class. Refactor parts of the SlotGenerator class.


=== Products/CompositePage/composite.py 1.21 => 1.22 ===
--- Products/CompositePage/composite.py:1.21	Tue Apr 13 15:12:01 2004
+++ Products/CompositePage/composite.py	Wed Apr 14 12:15:29 2004
@@ -34,14 +34,39 @@
 
 _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 SlotContainer(Acquisition.Implicit):
+    """ A dummy slots container for the base implementation of
+    SlotGenerator.
+    """
+
+    def get(self, name, default=None):
+        raise KeyError, name
+
+    __getitem__ = get
 
-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 newSlot(self, name):
+        return self._slot_class(name)
+
+    def getSlots(self, context=None):
+        if context is None:
+            context = aq_parent(aq_inner(self))
+        return SlotContainer().__of__(context)
+
     def get(self, name, class_name=None, title=None):
         """Returns a slot by name.
 
@@ -50,7 +75,6 @@
         slot at the same time.
         """
         composite = aq_parent(aq_inner(self))
-        slots = composite.filled_slots
         if composite._v_slot_specs is not None:
             # Send the slot specs to the composite.
             composite._v_slot_specs.append({
@@ -58,35 +82,49 @@
                 'class_name': class_name,
                 'title': title,
                 })
+        slots = self.getSlots(composite)
         try:
             return slots[name]
         except (KeyError, AttributeError):
-            # 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)
+            return self.newSlot(name).__of__(slots)
 
     __getitem__ = get
 
+class FolderSlotGenerator(SlotGenerator):
 
+    def getSlots(self, context=None):
+        if context is None:
+            context = self._getComposite()
+        return context.filled_slots
+
+    def _getComposite(self):
+        return aq_parent(aq_inner(self))
+
+    def newSlot(self, name):
+        # Generate a new slot.
+        s = SlotGenerator.newSlot(self, name)
+        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
 
-class Composite(Folder):
-    """An HTML fragment composed from a template and fragments.
+class CompositeMixin:
+    """ An HTML fragment composed from a template and fragments.
     """
+
     meta_type = "Composite"
     __implements__ = IComposite
 
     security = ClassSecurityInfo()
 
-    manage_options = (
-        Folder.manage_options[:1]
-        + ({"label": "Design", "action": "manage_designForm",},
-           {"label": "View", "action": "view",},)
-        + Folder.manage_options[2:]
-        )
+    manage_options = ({"label": "Design",
+                       "action": "manage_designForm",},
+                      {"label": "View",
+                       "action": "view",},
+                      )
 
     default_ui = "common"
     template_path = "template"
@@ -96,18 +134,13 @@
     _v_slot_specs = None  # [{'name', 'class', 'title'}]
 
     security.declarePublic("slots")
-    slots = SlotGenerator()
+    slots = FolderSlotGenerator()
 
-    _properties = Folder._properties + (
+    _properties = (
         {"id": "template_path", "mode": "w", "type": "string",
          "label": "Path to template"},
         )
 
-    def __init__(self):
-        f = SlotCollection()
-        f._setId("filled_slots")
-        self._setObject(f.getId(), f)
-
     security.declareProtected(perm_names.view, "hasTemplate")
     def hasTemplate(self):
         if self.template_path:
@@ -287,18 +320,29 @@
         """
         return self._v_editing
 
-Globals.InitializeClass(Composite)
+Globals.InitializeClass(CompositeMixin)
 
 
+class Composite(CompositeMixin, Folder):
+    """An HTML fragment composed from a template and fragments.
 
-class SlotCollection(Folder):
-    """Collection of composite slots.
+    Fragments are stored on a container called 'filled_slots'.
     """
-    meta_type = "Slot Collection"
 
-    def all_meta_types(self):
-        return Folder.all_meta_types(self, interfaces=(ISlot,))
+    manage_options = (
+        Folder.manage_options[:1]
+        + CompositeMixin.manage_options
+        + Folder.manage_options[2:]
+        )
+
+    _properties = Folder._properties + CompositeMixin._properties
+
+    def __init__(self):
+        f = SlotCollection()
+        f._setId("filled_slots")
+        self._setObject(f.getId(), f)
 
+Globals.InitializeClass(Composite)
 
 
 class FailedElement(SimpleItem):


=== Products/CompositePage/element.py 1.4 => 1.5 ===
--- Products/CompositePage/element.py:1.4	Thu Apr  1 18:20:37 2004
+++ Products/CompositePage/element.py	Wed Apr 14 12:15:29 2004
@@ -82,7 +82,7 @@
     def setInlineTemplate(self, template):
         """Sets the inline template for this object.
         """
-        self.template_name = str(template_name)
+        self.template_name = str(template)
 
     def listAllowableInlineTemplates(self, slot_class_name=None):
         """Returns a list of inline template names allowable for this object.


=== Products/CompositePage/interfaces.py 1.11 => 1.12 ===
--- Products/CompositePage/interfaces.py:1.11	Thu Apr  1 18:20:37 2004
+++ Products/CompositePage/interfaces.py	Wed Apr 14 12:15:29 2004
@@ -47,6 +47,16 @@
         """Returns a slot, creating it if it does not yet exist.
         """
 
+    def getSlots(context=None):
+        """Returns the slots container, which should have at
+        least __getitem__ and raise a KeyError if the slot
+        doesn't exists.
+        """
+
+    def newSlot(name):
+        """Create a new slot, and optionally add it to the
+        slots container.
+        """
 
 class ISlot(Interface):
     """A slot in a composite.
@@ -105,7 +115,7 @@
         """Returns the name of the inline template this object uses.
 
         Returns None if none has been chosen and there is no default.
-        
+
         The slot_class_name may be provided as an optimization.
         """
 
@@ -117,7 +127,7 @@
         """Returns a list of templates allowable for this object.
 
         Returns a list of (template_name, template_object).
-        
+
         The slot_class_name may be provided as an optimization.
         """
 


=== Products/CompositePage/slot.py 1.19 => 1.20 ===
--- Products/CompositePage/slot.py:1.19	Tue Apr 13 15:12:01 2004
+++ Products/CompositePage/slot.py	Wed Apr 14 12:15:29 2004
@@ -71,8 +71,7 @@
     def __init__(self, id):
         self.id = id
 
-
-class Slot (OrderedFolder):
+class Slot(OrderedFolder):
     """A slot in a composite.
     """
     meta_type = "Composite Slot"




More information about the Zope-CVS mailing list