Commit 33cb55af authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

propose a more flexible implementation

parent 76cb4680f2af
......@@ -13,30 +13,73 @@
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <>.
"""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'
def rql(self):
raise NotImplementedError()
def astream_rql(self):
"""return a RQL query to get the whole activity stream of the adapted
class IActivityStreamItemAdapter(EntityAdapter):
__regid__ = 'IActivityStreamItem'
In this query, %(x)s will refer to the eid of the adapted entity. The
returned query is expected to select:
def content(self):
raise NotImplementedError()
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)
rql = rqls[0]
class ActivityStreamPart(AppObject):
__registry__ = 'astream'
def date(self):
raise NotImplementedError()
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.
class AnyAStreamItemAdapter(IActivityStreamItemAdapter):
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'
__select__ = is_instance('Any')
......@@ -47,7 +90,12 @@ class AnyAStreamItemAdapter(IActivityStreamItemAdapter):
def date(self):
return self.entity.creation_date
class TrinfoAStreamItemAdapter(AnyAStreamItemAdapter):
def actor(self):
return self.entity.dc_creator()
class TrinfoAStreamItemAdapter(IActivityStreamItemAdapter):
__select__ = is_instance('TrInfo')
......@@ -4,14 +4,16 @@ 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,
rset = self._cw.execute(entity.cw_adapt_to('IActivityStream').astream_rql,
self.wview('activitystream_item', rset, 'null')
class AStreamItemView(EntityView):
__regid__ = 'activitystream_item'
__select__ = EntityView.__select__ & adaptable('IActivityStreamItem')
......@@ -19,13 +21,11 @@ class AStreamItemView(EntityView):
def cell_call(self, row, col):
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,
u'</div>' % (, activity.content,
self._cw.format_date(, time=True)))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment