[ctxcomponents] RQLCtxComponent: refactor init_rendering/render_body to ease overriding stable
authorJulien Jehannet <julien.jehannet@logilab.fr>
Thu, 23 Dec 2010 15:49:29 +0100
branchstable
changeset 6777 1d423b7aa6c9
parent 6776 eebe86b89285
child 6778 7dd4835d5198
[ctxcomponents] RQLCtxComponent: refactor init_rendering/render_body to ease overriding init_rendering now simply execute the query to get result set to be displayed, and store it in .cw_rset attribute. Links to be displayed by default are now computed in render_body. So you may now simply customise what to be displayed by overriding render_body.
web/component.py
--- a/web/component.py	Wed Jan 05 11:09:51 2011 +0100
+++ b/web/component.py	Thu Dec 23 15:49:29 2010 +0100
@@ -334,30 +334,39 @@
 # high level abstract classes ##################################################
 
 class RQLCtxComponent(CtxComponent):
-    """abstract box for boxes displaying the content of a rql query not
-    related to the current result set.
+    """abstract box for boxes displaying the content of a rql query not related
+    to the current result set.
+
+    Notice that this class's init_rendering implemention is overwriting context
+    result set (eg `cw_rset`) with the result set returned by execution of
+    `to_display_rql()`.
     """
-    rql  = None
+    rql = None
 
     def to_display_rql(self):
+        """return arguments to give to self._cw.execute, as a tuple, to build
+        the result set to be displayed by this box.
+        """
         assert self.rql is not None, self.__regid__
         return (self.rql,)
 
     def init_rendering(self):
-        rset = self._cw.execute(*self.to_display_rql())
-        if not rset:
+        super(RQLCtxComponent, self).init_rendering()
+        self.cw_rset = self._cw.execute(*self.to_display_rql())
+        if not self.cw_rset:
             raise EmptyComponent()
+
+    def render_body(self, w):
+        rset = self.cw_rset
         if len(rset[0]) == 2:
-            self.items = []
+            items = []
             for i, (eid, label) in enumerate(rset):
                 entity = rset.get_entity(i, 0)
-                self.items.append(self.build_link(label, entity.absolute_url()))
+                items.append(self.build_link(label, entity.absolute_url()))
         else:
-            self.items = [self.build_link(e.dc_title(), e.absolute_url())
-                          for e in rset.entities()]
-
-    def render_body(self, w):
-        self.render_items(w)
+            items = [self.build_link(e.dc_title(), e.absolute_url())
+                     for e in rset.entities()]
+        self.render_items(w, items)
 
 
 class EditRelationMixIn(ReloadableMixIn):