~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Ian Booth
  • Date: 2011-08-05 06:10:59 UTC
  • mto: This revision was merged to the branch mainline in revision 13636.
  • Revision ID: ian.booth@canonical.com-20110805061059-yrkq3apnzzzsisbq
Copy changes from previous branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    )
30
30
 
31
31
from canonical.launchpad.interfaces.launchpad import IHasIcon
 
32
from lp.answers.interfaces.question import IQuestion
 
33
from lp.blueprints.interfaces.specification import ISpecification
32
34
from lp.bugs.interfaces.bugtask import IBugTask
33
35
from lp.registry.interfaces.distribution import IDistribution
 
36
from lp.registry.interfaces.distroseries import IDistroSeries
 
37
from lp.registry.interfaces.productseries import IProductSeries
34
38
 
35
39
 
36
40
class IHasAffiliation(Interface):
51
55
class PillarAffiliation(object):
52
56
    """Default affiliation adapter.
53
57
 
54
 
    No affiliation is returned.
 
58
    Subclasses may need to override getPillar() in order to provide the pillar
 
59
    entity for which affiliation is to be determined. The default is just to
 
60
    use the context object directly.
55
61
    """
56
62
 
57
63
    implements(IHasAffiliation)
59
65
    def __init__(self, context):
60
66
        self.context = context
61
67
 
62
 
    def getAffiliationBadge(self, person):
63
 
        return None
64
 
 
65
 
 
66
 
# XXX: wallyworld 2011-05-24 bug=81692: TODO Work is required to determine
67
 
# exactly what is required in terms of figuring out affiliation..
68
 
 
69
 
@adapter(IBugTask)
70
 
class BugTaskPillarAffiliation(PillarAffiliation):
71
 
    """An affiliation adapter for bug tasks."""
72
 
 
73
 
    def getAffiliationBadge(self, person):
74
 
        pillar = self.context.pillar
75
 
        affiliated = person.inTeam(pillar.owner)
76
 
        if not affiliated:
 
68
    def getPillar(self):
 
69
        return self.context
 
70
 
 
71
    def _getAffiliationDetails(self, person, pillar):
 
72
        """ Return the affiliation information for a person, if any.
 
73
 
 
74
        A person is affiliated with a pillar if they are in the list of
 
75
        drivers or are the maintainer.
 
76
        """
 
77
        if person.inTeam(pillar.owner):
 
78
            return pillar.displayname, 'maintainer'
 
79
        for driver in pillar.drivers:
 
80
            if person.inTeam(driver):
 
81
                return pillar.displayname, 'driver'
 
82
        return  None
 
83
 
 
84
    def getAffiliationBadge(self, person):
 
85
        """ Return the affiliation badge details for a person given a context.
 
86
        """
 
87
        pillar = self.getPillar()
 
88
        affiliation_details = self._getAffiliationDetails(person, pillar)
 
89
        if not affiliation_details:
77
90
            return None
78
91
 
79
 
        def getIconUrl(context, default_url):
 
92
        def getIconUrl(context, pillar, default_url):
80
93
            if IHasIcon.providedBy(context) and context.icon is not None:
81
94
                icon_url = context.icon.getURL()
82
95
                return icon_url
 
96
            if IHasIcon.providedBy(pillar) and pillar.icon is not None:
 
97
                icon_url = context.icon.getURL()
 
98
                return icon_url
83
99
            return default_url
84
100
 
85
 
        alt_text = "Affiliated with %s" % pillar.displayname
 
101
        alt_text = "%s %s" % affiliation_details
86
102
        if IDistribution.providedBy(pillar):
87
 
            icon_url = getIconUrl(pillar, "/@@/distribution-badge")
 
103
            default_icon_url = "/@@/distribution-badge"
88
104
        else:
89
 
            icon_url = getIconUrl(pillar, "/@@/product-badge")
 
105
            default_icon_url = "/@@/product-badge"
 
106
        icon_url = getIconUrl(self.context, pillar, default_icon_url)
90
107
        return BadgeDetails(icon_url, alt_text)
 
108
 
 
109
 
 
110
@adapter(IBugTask)
 
111
class BugTaskPillarAffiliation(PillarAffiliation):
 
112
    """An affiliation adapter for bug tasks."""
 
113
    def getPillar(self):
 
114
        return self.context.pillar
 
115
 
 
116
    def _getAffiliationDetails(self, person, pillar):
 
117
        """ A person is affiliated with a bugtask based on (in order):
 
118
        - owner of bugtask pillar
 
119
        - driver of bugtask pillar
 
120
        - bug supervisor of bugtask pillar
 
121
        - security contact of bugtask pillar
 
122
        """
 
123
        result = super(BugTaskPillarAffiliation, self)._getAffiliationDetails(
 
124
            person, pillar)
 
125
        if result is not None:
 
126
            return result
 
127
        if person.inTeam(pillar.bug_supervisor):
 
128
            return pillar.displayname, 'bug supervisor'
 
129
        if person.inTeam(pillar.security_contact):
 
130
            return pillar.displayname, 'security contact'
 
131
 
 
132
 
 
133
@adapter(IDistroSeries)
 
134
class DistroSeriesPillarAffiliation(PillarAffiliation):
 
135
    """An affiliation adapter for distroseries."""
 
136
    def getPillar(self):
 
137
        return self.context.distribution
 
138
 
 
139
 
 
140
@adapter(IProductSeries)
 
141
class ProductSeriesPillarAffiliation(PillarAffiliation):
 
142
    """An affiliation adapter for productseries."""
 
143
    def getPillar(self):
 
144
        return self.context.product
 
145
 
 
146
 
 
147
@adapter(ISpecification)
 
148
class SpecificationPillarAffiliation(PillarAffiliation):
 
149
    """An affiliation adapter for blueprints."""
 
150
    def getPillar(self):
 
151
        return (self.context.target)
 
152
 
 
153
 
 
154
@adapter(IQuestion)
 
155
class QuestionPillarAffiliation(PillarAffiliation):
 
156
    """An affiliation adapter for questions."""
 
157
    def getPillar(self):
 
158
        return self.context.product or self.context.distribution
 
159
 
 
160
    def _getAffiliationDetails(self, person, pillar):
 
161
        """ A person is affiliated with a question based on (in order):
 
162
        - answer contact for question target
 
163
        - owner of question target
 
164
        - driver of question target
 
165
        """
 
166
        target = self.context.target
 
167
        answer_contacts = target.answer_contacts
 
168
        for answer_contact in answer_contacts:
 
169
            if person.inTeam(answer_contact):
 
170
                return target.displayname, 'answer contact'
 
171
        return super(QuestionPillarAffiliation, self)._getAffiliationDetails(
 
172
            person, pillar)