Switch to unified view

a/Allura/allura/model/timeline.py b/Allura/allura/model/timeline.py
1
import bson
2
from ming.odm import Mapper
1
from activitystream import base
3
from activitystream import base
4
from allura.lib import security
5
2
6
3
class ActivityNode(base.NodeBase):
7
class ActivityNode(base.NodeBase):
4
    @property
8
    @property
5
    def node_id(self):
9
    def node_id(self):
6
        return "%s:%s" % (self.__mongometa__.name, self._id)
10
        return "%s:%s" % (self.__class__.__name__, self._id)
11
7
12
8
class ActivityObject(base.ActivityObjectBase):
13
class ActivityObject(base.ActivityObjectBase):
9
    @property
14
    @property
10
    def activity_name(self):
15
    def activity_name(self):
11
        """Override this for each Artifact type."""
16
        """Override this for each Artifact type."""
12
        return "%s %s" % (self.__mongometa__.name.capitalize(), self._id)
17
        return "%s %s" % (self.__mongometa__.name.capitalize(), self._id)
13
18
14
    @property
19
    @property
15
    def activity_url(self):
20
    def activity_url(self):
16
        return self.url()
21
        return self.url()
22
23
    @property
24
    def activity_extras(self):
25
        """Return a BSON-serializable dict of extra stuff to store on the
26
        activity.
27
        """
28
        return {"allura_id": self.allura_id}
29
30
    @property
31
    def allura_id(self):
32
        """Return a string which uniquely identifies this object and which can
33
        be used to retrieve the object from mongo.
34
        """
35
        return "%s:%s" % (self.__class__.__name__, self._id)
36
37
    def has_activity_access(self, perm, user):
38
        """Return True if user has perm access to this object, otherwise
39
        return False.
40
        """
41
        return security.has_access(self, perm, user, self.project)
42
43
44
def perm_check(user):
45
    def _perm_check(activity):
46
        """Return True if c.user has 'read' access to this activity,
47
        otherwise return False.
48
        """
49
        allura_id = activity['obj']['activity_extras'].get('allura_id')
50
        if not allura_id: return True
51
        classname, _id = allura_id.split(':')
52
        cls = Mapper.by_classname(classname).mapped_class
53
        try:
54
            _id = bson.ObjectId(_id)
55
        except bson.errors.InvalidId:
56
            pass
57
        obj = cls.query.get(_id=_id)
58
        return obj and obj.has_activity_access('read', user)
59
    return _perm_check