~launchpad-pqm/launchpad/devel

10121.1.4 by Henning Eggers
Added PersonRoles class, adapter and test.
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Class that implements the IPersonRoles interface."""
5
6
__metaclass__ = type
7
__all__ = ['PersonRoles']
8
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
9
from zope.component import (
10
    adapts,
11
    getUtility,
12
    )
10121.1.4 by Henning Eggers
Added PersonRoles class, adapter and test.
13
from zope.interface import implements
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
14
13130.1.6 by Curtis Hovey
Move ILaunchpadCelebrity to lp.app.
15
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
14302.4.1 by Ian Booth
Allow users in project roles and comment owners to hide comments
16
from lp.bugs.interfaces.bugsupervisor import IHasBugSupervisor
17
from lp.bugs.interfaces.securitycontact import IHasSecurityContact
13130.1.6 by Curtis Hovey
Move ILaunchpadCelebrity to lp.app.
18
from lp.registry.interfaces.person import IPerson
19
from lp.registry.interfaces.role import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
20
    IHasDrivers,
21
    IPersonRoles,
22
    )
10121.1.4 by Henning Eggers
Added PersonRoles class, adapter and test.
23
24
10121.1.5 by Henning Eggers
Satisfy interface.
25
class PersonRoles:
10121.1.4 by Henning Eggers
Added PersonRoles class, adapter and test.
26
    implements(IPersonRoles)
27
    adapts(IPerson)
28
29
    def __init__(self, person):
30
        self.person = person
10121.1.7 by Henning Eggers
Implemented is_admin.
31
        self._celebrities = getUtility(ILaunchpadCelebrities)
10121.1.13 by Henning Eggers
Implemented inTeam.
32
        self.inTeam = self.person.inTeam
10121.1.7 by Henning Eggers
Implemented is_admin.
33
10121.1.23 by Henning Eggers
Implemented __getattr__.
34
    def __getattr__(self, name):
35
        """Handle all in_* attributes."""
36
        prefix = 'in_'
10121.2.14 by Henning Eggers
Documentation and code improvements as per review.
37
        errortext = "'PersonRoles' object has no attribute '%s'" % name
10121.1.23 by Henning Eggers
Implemented __getattr__.
38
        if not name.startswith(prefix):
10121.2.13 by Henning Eggers
PersonRoles.__getattr__ raising more informative AttributeError.
39
            raise AttributeError(errortext)
10121.1.23 by Henning Eggers
Implemented __getattr__.
40
        attribute = name[len(prefix):]
10121.2.13 by Henning Eggers
PersonRoles.__getattr__ raising more informative AttributeError.
41
        try:
42
            return self.person.inTeam(getattr(self._celebrities, attribute))
43
        except AttributeError:
44
            raise AttributeError(errortext)
10121.1.5 by Henning Eggers
Satisfy interface.
45
11307.2.25 by Robert Collins
Put a ratchet in place for the number of queries on the milestone index page, and check that its constant when more private bugs are added.
46
    @property
47
    def id(self):
48
        return self.person.id
49
10121.1.5 by Henning Eggers
Satisfy interface.
50
    def isOwner(self, obj):
51
        """See IPersonRoles."""
10121.1.16 by Henning Eggers
Implemented isOwner and isDriver.
52
        return self.person.inTeam(obj.owner)
10121.1.5 by Henning Eggers
Satisfy interface.
53
14302.4.1 by Ian Booth
Allow users in project roles and comment owners to hide comments
54
    def isBugSupervisor(self, obj):
55
        """See IPersonRoles."""
56
        return (IHasBugSupervisor.providedBy(obj)
57
                and self.person.inTeam(obj.bug_supervisor))
58
59
    def isSecurityContact(self, obj):
60
        """See IPersonRoles."""
61
        return (IHasSecurityContact.providedBy(obj)
62
                and self.person.inTeam(obj.security_contact))
63
10121.1.5 by Henning Eggers
Satisfy interface.
64
    def isDriver(self, obj):
65
        """See IPersonRoles."""
10121.2.7 by Henning Eggers
Fixed permission checkers and tests using them.
66
        return self.person.inTeam(obj.driver)
67
68
    def isOneOfDrivers(self, obj):
69
        """See IPersonRoles."""
70
        if not IHasDrivers.providedBy(obj):
71
            return self.isDriver(obj)
72
        for driver in obj.drivers:
10121.1.18 by Henning Eggers
Implemented check for multiple drivers.
73
            if self.person.inTeam(driver):
74
                return True
75
        return False
10121.1.5 by Henning Eggers
Satisfy interface.
76
10121.1.20 by Henning Eggers
Implemented isOneof.
77
    def isOneOf(self, obj, attributes):
10121.1.5 by Henning Eggers
Satisfy interface.
78
        """See IPersonRoles."""
10121.1.20 by Henning Eggers
Implemented isOneof.
79
        for attr in attributes:
80
            role = getattr(obj, attr)
81
            if self.person.inTeam(role):
82
                return True
83
        return False