propose a more flexible implementation
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 05 Aug 2011 15:11:39 +0200
changeset 7 fe416ee3cf81
parent 6 76cb4680f2af
child 8 e7fcada2d97a
propose a more flexible implementation
entities.py
views.py
--- a/entities.py	Fri Aug 05 13:34:22 2011 +0200
+++ b/entities.py	Fri Aug 05 15:11:39 2011 +0200
@@ -13,30 +13,73 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
+"""cubicweb-astream adapters"""
 
-"""cubicweb-astream entity's classes"""
-from cubicweb.selectors import is_instance
+from cubicweb.selectors import is_instance, adaptable
 from cubicweb.view import EntityAdapter
+from cubicweb.appobject import AppObject
 
 class IActivityStreamAdapter(EntityAdapter):
     __regid__ = 'IActivityStream'
 
     @property
-    def rql(self):
-        raise NotImplementedError()
+    def astream_rql(self):
+        """return a RQL query to get the whole activity stream of the adapted
+        entity.
+
+        In this query, %(x)s will refer to the eid of the adapted entity. The
+        returned query is expected to select:
+
+        1. the eid of the activity
+        2. its date
+        3. the actor of the activity
+        4. some content.
+
+        and to be ordered on date (descending)
+        """
+        rqls = []
+        for part in self._cw.vreg['astream'].possible_objects(
+            self._cw, entity=self.entity):
+            rqls += part.rql_parts
+        assert rqls, 'no activity stream for %s' % self.entity
+        if len(rqls) > 1:
+            rql =  ' UNION '.join('(%s)' % rql for rql in rqls)
+        else:
+            rql = rqls[0]
+        return 'Any X,XD,XA,XC ORDERBY XD DESC WITH X,XD,XA,XC BEING (%s)' % rql
+
+
+class ActivityStreamPart(AppObject):
+    __registry__ = 'astream'
+
+    @property
+    def rql_parts(self):
+        """return a list of RQL queries to get some path of the activity stream
+        of the adapted entity.
+
+        In each query, %(x)s will refer to the eid of the adapted entity. The
+        returned query is expected to select:
+
+        1. the eid of the activity
+        2. its date
+        3. the actor of the activity
+        4. some content.
+
+        and should not be ordered.
+        """
+        raise NotImplementedError
+
+
+class StatefulAStreamPart(ActivityStreamPart):
+    __select__ = adaptable('IWorkflowable')
+    __regid__ = 'stateful'
+    rql_parts = (
+        'Any TI,TICD,U,TIC WHERE TI is TrInfo, TI wf_info_for X, X eid %(x)s,'
+        'TI creation_date TICD, TI created_by U?, TI comment TIC',)
+
 
 class IActivityStreamItemAdapter(EntityAdapter):
     __regid__ = 'IActivityStreamItem'
-
-    @property
-    def content(self):
-        raise NotImplementedError()
-
-    @property
-    def date(self):
-        raise NotImplementedError()
-
-class AnyAStreamItemAdapter(IActivityStreamItemAdapter):
     __select__ = is_instance('Any')
 
     @property
@@ -47,7 +90,12 @@
     def date(self):
         return self.entity.creation_date
 
-class TrinfoAStreamItemAdapter(AnyAStreamItemAdapter):
+    @property
+    def actor(self):
+        return self.entity.dc_creator()
+
+
+class TrinfoAStreamItemAdapter(IActivityStreamItemAdapter):
     __select__ = is_instance('TrInfo')
 
     @property
--- a/views.py	Fri Aug 05 13:34:22 2011 +0200
+++ b/views.py	Fri Aug 05 15:11:39 2011 +0200
@@ -4,14 +4,16 @@
 class AStreamView(EntityView):
     __regid__ = 'activitystream'
     __select__ = EntityView.__select__ & adaptable('IActivityStream')
+    title = _('activitystream')
 
     def cell_call(self, row, col):
         entity = self.cw_rset.get_entity(row, col)
-        rset = self._cw.execute(entity.cw_adapt_to('IActivityStream').rql,
+        rset = self._cw.execute(entity.cw_adapt_to('IActivityStream').astream_rql,
                                 dict(x=entity.eid))
         self.paginate(rset=rset)
         self.wview('activitystream_item', rset, 'null')
 
+
 class AStreamItemView(EntityView):
     __regid__ = 'activitystream_item'
     __select__ = EntityView.__select__ & adaptable('IActivityStreamItem')
@@ -19,13 +21,11 @@
     def cell_call(self, row, col):
         self._cw.add_css('cubes.astream.css')
         entity = self.cw_rset.get_entity(row, col)
-        ablock = entity.dc_creator()
         activity = entity.cw_adapt_to('IActivityStreamItem')
         self.w(u'<div class="activitystream">'
                u'<span class="author">%s</span>'
                u'<span class="msgtxt">%s</span>'
                u'<span class="meta"><a href="%s">%s</a></span>'
-               u'</div>' % (ablock, activity.content,
-                            entity.absolute_url(), activity.date))
-
-
+               u'</div>' % (activity.actor, activity.content,
+                            entity.absolute_url(),
+                            self._cw.format_date(activity.date, time=True)))