~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/registry/model/pillaraffiliation.py

  • Committer: Graham Binns
  • Date: 2011-09-07 15:59:13 UTC
  • mto: This revision was merged to the branch mainline in revision 13914.
  • Revision ID: graham@canonical.com-20110907155913-p97tx2e34ysbcgp3
Added an XXX.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
from lp.registry.interfaces.distributionsourcepackage import (
35
35
    IDistributionSourcePackage,
36
36
    )
 
37
from lp.registry.model.teammembership import find_team_participations
37
38
 
38
39
 
39
40
class IHasAffiliation(Interface):
61
62
 
62
63
    implements(IHasAffiliation)
63
64
 
 
65
    # We rank the affiliations from most important to least important.
 
66
    # Unlisted roles are given a rank of 10.
 
67
    affiliation_priorities = {
 
68
        'maintainer': 1,
 
69
        'driver': 2,
 
70
        'bug supervisor': 3,
 
71
        'security contact': 4,
 
72
    }
 
73
 
64
74
    def __init__(self, context):
65
75
        self.context = context
66
76
 
67
77
    def getPillar(self):
68
78
        return self.context
69
79
 
70
 
    def _getAffiliationDetails(self, person, pillar):
 
80
    def _getAffiliation(self, person, pillar):
71
81
        """ Return the affiliation information for a person, if any.
72
82
 
 
83
        Subclasses will override this method to perform specific affiliation
 
84
        checks.
 
85
        The return result is a list of tuples (pillar displayanme, role).
 
86
        """
 
87
        return []
 
88
 
 
89
    def _getAffiliationTeamRoles(self, pillar):
 
90
        """ Return teams for which a person needs to belong, if affiliated.
 
91
 
73
92
        A person is affiliated with a pillar if they are in the list of
74
93
        drivers or are the maintainer.
75
 
        The return result is a list of tuples (pillar displayanme, role).
76
94
        """
77
 
        result = []
78
 
        if person.inTeam(pillar.owner):
79
 
            result.append((pillar.displayname, 'maintainer'))
80
 
        for driver in pillar.drivers:
81
 
            if person.inTeam(driver):
82
 
                result.append((pillar.displayname, 'driver'))
83
 
                break
 
95
        result = {
 
96
            (pillar.displayname, 'maintainer'): [pillar.owner],
 
97
            (pillar.displayname, 'driver'): pillar.drivers}
84
98
        return result
85
99
 
86
100
    def getAffiliationBadges(self, persons):
87
101
        """ Return the affiliation badge details for people given a context.
 
102
 
 
103
        There are 2 ways we check for affiliation:
 
104
        1. Generic membership checks of particular teams as returned by
 
105
           _getAffiliationTeamRoles
 
106
        2. Specific affiliation checks as performed by _getAffiliation
88
107
        """
89
108
        pillar = self.getPillar()
90
109
        result = []
 
110
 
 
111
        # We find the teams to check for participation..
 
112
        affiliation_team_details = self._getAffiliationTeamRoles(pillar)
 
113
        teams_to_check = set()
 
114
        for teams in affiliation_team_details.values():
 
115
            teams_to_check.update(teams)
 
116
        # We gather the participation for the persons.
 
117
        people_teams = find_team_participations(persons, teams_to_check)
 
118
 
91
119
        for person in persons:
92
 
            affiliation_details = self._getAffiliationDetails(person, pillar)
93
 
            if not affiliation_details:
 
120
            # Specific affiliations
 
121
            affiliations = self._getAffiliation(person, pillar)
 
122
            # Generic, team based affiliations
 
123
            affiliated_teams = people_teams.get(person, [])
 
124
            for affiliated_team in affiliated_teams:
 
125
                for affiliation, teams in affiliation_team_details.items():
 
126
                    if affiliated_team in teams:
 
127
                        affiliations.append(affiliation)
 
128
 
 
129
            if not affiliations:
94
130
                result.append([])
95
131
                continue
96
132
 
108
144
            else:
109
145
                default_icon_url = "/@@/product-badge"
110
146
            icon_url = getIconUrl(self.context, pillar, default_icon_url)
 
147
 
 
148
            # Sort the affiliation list according the the importance of each
 
149
            # affiliation role.
 
150
            affiliations.sort(
 
151
                key=lambda affiliation_rec:
 
152
                    self.affiliation_priorities.get(affiliation_rec[1], 10))
111
153
            badges = []
112
 
            for affiliation in affiliation_details:
 
154
            for affiliation in affiliations:
113
155
                alt_text = "%s %s" % affiliation
114
156
                badges.append(BadgeDetails(icon_url, alt_text))
115
157
            result.append(badges)
121
163
    def getPillar(self):
122
164
        return self.context.pillar
123
165
 
124
 
    def _getAffiliationDetails(self, person, pillar):
 
166
    def _getAffiliationTeamRoles(self, pillar):
125
167
        """ A person is affiliated with a bugtask based on (in order):
126
168
        - owner of bugtask pillar
127
169
        - driver of bugtask pillar
128
170
        - bug supervisor of bugtask pillar
129
171
        - security contact of bugtask pillar
130
172
        """
131
 
        result = super(BugTaskPillarAffiliation, self)._getAffiliationDetails(
132
 
            person, pillar)
133
 
        if person.inTeam(pillar.bug_supervisor):
134
 
            result.append((pillar.displayname, 'bug supervisor'))
135
 
        if person.inTeam(pillar.security_contact):
136
 
            result.append((pillar.displayname, 'security contact'))
 
173
        super_instance = super(BugTaskPillarAffiliation, self)
 
174
        result = super_instance._getAffiliationTeamRoles(pillar)
 
175
        result[(pillar.displayname, 'bug supervisor')] = (
 
176
            [pillar.bug_supervisor])
 
177
        result[(pillar.displayname, 'security contact')] = (
 
178
            [pillar.security_contact])
137
179
        return result
138
180
 
139
181
 
140
182
class BranchPillarAffiliation(BugTaskPillarAffiliation):
141
183
    """An affiliation adapter for branches."""
 
184
 
142
185
    def getPillar(self):
143
186
        return self.context.product or self.context.distribution
144
187
 
 
188
    def getBranch(self):
 
189
        return self.context
 
190
 
 
191
    def _getAffiliation(self, person, pillar):
 
192
        super_instance = super(BranchPillarAffiliation, self)
 
193
        result = super_instance._getAffiliation(person, pillar)
 
194
        if self.getBranch().isPersonTrustedReviewer(person):
 
195
            result.append((pillar.displayname, 'trusted reviewer'))
 
196
        return result
 
197
 
 
198
 
 
199
class CodeReviewVotePillarAffiliation(BranchPillarAffiliation):
 
200
    """An affiliation adapter for CodeReviewVotes."""
 
201
 
 
202
    def getPillar(self):
 
203
        """Return the target branch'pillar."""
 
204
        branch = self.getBranch()
 
205
        return branch.product or branch.distribution
 
206
 
 
207
    def getBranch(self):
 
208
        return self.context.branch_merge_proposal.target_branch
 
209
 
145
210
 
146
211
class DistroSeriesPillarAffiliation(PillarAffiliation):
147
212
    """An affiliation adapter for distroseries."""
162
227
 
163
228
 
164
229
class QuestionPillarAffiliation(PillarAffiliation):
165
 
    """An affiliation adapter for questions."""
 
230
    """An affiliation adapter for questions.
 
231
 
 
232
    A person is affiliated with a question based on (in order):
 
233
    - answer contact for question target
 
234
    - owner of question target
 
235
    - driver of question target
 
236
    """
 
237
 
166
238
    def getPillar(self):
167
239
        return self.context.product or self.context.distribution
168
240
 
169
 
    def _getAffiliationDetails(self, person, pillar):
170
 
        """ A person is affiliated with a question based on (in order):
171
 
        - answer contact for question target
172
 
        - owner of question target
173
 
        - driver of question target
174
 
        """
 
241
    def _getAffiliation(self, person, pillar):
175
242
        result = (super(QuestionPillarAffiliation, self)
176
 
                                ._getAffiliationDetails(person, pillar))
 
243
                                ._getAffiliation(person, pillar))
177
244
        target = self.context.target
178
245
        if IDistributionSourcePackage.providedBy(target):
179
246
            question_targets = (target, target.distribution)