entities.py 3.59 KB
Newer Older
Nicolas Chauvat's avatar
Nicolas Chauvat committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# copyright 2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# 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/>.
16
"""cubicweb-activitystream adapters"""
Nicolas Chauvat's avatar
Nicolas Chauvat committed
17

18
from cubicweb.predicates import is_instance, adaptable
19
from cubicweb.view import EntityAdapter
20
from cubicweb.appobject import AppObject
21
22
23

class IActivityStreamAdapter(EntityAdapter):
    __regid__ = 'IActivityStream'
24
    __abstract__ = True
25

26
    @property
27
28
29
    def astream_rql(self):
        """return a RQL query to get the whole activity stream of the adapted
        entity.
30

31
32
        In this query, %(x)s will refer to the eid of the adapted entity. The
        returned query is expected to select:
33

34
35
36
37
38
39
40
41
42
        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(
43
                self._cw, entity=self.entity):
44
45
46
47
48
49
50
51
52
53
54
            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'
55
56

    @property
57
58
59
60
61
62
63
64
65
66
67
    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.
Nicolas Chauvat's avatar
Nicolas Chauvat committed
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        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'
Nicolas Chauvat's avatar
Nicolas Chauvat committed
84
85
86
87
88
89
90
91
92
    __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
93

94
95
96
97
98
99
    @property
    def actor(self):
        return self.entity.dc_creator()


class TrinfoAStreamItemAdapter(IActivityStreamItemAdapter):
100
101
102
103
104
105
106
107
108
109
110
    __select__ = is_instance('TrInfo')

    @property
    def content(self):
        return (u'%s %s transition from state %s to state %s with comment %s'
                % (self.entity.wf_info_for[0].e_schema,
                   self.entity.wf_info_for[0].dc_title(),
                   self.entity.from_state[0].name,
                   self.entity.to_state[0].name,
                   self.entity.printable_value('comment'),
                   ))