astreampart.diff ready for review
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 04 Aug 2011 17:13:25 +0200
changeset 3 9c6f2d721ae5
parent 2 ec728e052d43
child 4 4f4263d09b01
astreampart.diff ready for review
astreampart.diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/astreampart.diff	Thu Aug 04 17:13:25 2011 +0200
@@ -0,0 +1,148 @@
+# HG changeset patch
+# User Sylvain Thénault <sylvain.thenault@logilab.fr>
+# Parent ec728e052d43c4ef34ba62fbbdfa4e53e5c559b3
+propose a more flexible implementation
+
+diff --git a/entities.py b/entities.py
+--- a/entities.py
++++ b/entities.py
+@@ -11,45 +11,93 @@
+ # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ # details.
+ #
+ # 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.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.astream_rqls
++        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 astream_rqls(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'
++    astream_rqls = (
++        '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
+     def content(self):
+         return u'%s %s added' % (self.entity.e_schema, self.entity.dc_title())
+ 
+     @property
+     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
+     def content(self):
+         return (u'%s %s transition from state %s to state %s with comment %s'
+diff --git a/views.py b/views.py
+--- a/views.py
++++ b/views.py
+@@ -2,18 +2,20 @@ from cubicweb.selectors import is_instan
+ from cubicweb.view import EntityView
+ 
+ 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,
+                                 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')
+ 
+     def cell_call(self, row, col):
+@@ -25,7 +27,5 @@ class AStreamItemView(EntityView):
+                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))
+-
+-