~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/soyuz/model/archivesubscriber.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-15 13:35:36 UTC
  • mfrom: (13677.4.4 bug-690568)
  • Revision ID: launchpad@pqm.canonical.com-20110815133536-7oj1019k90cedwqs
[r=henninge][bug=690568] Fetch email addresses for potential PPA
        subscribers all in one query.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009 Canonical Ltd.  This software is licensed under the
 
1
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
"""Database class for table ArchiveSubscriber."""
13
13
from storm.expr import (
14
14
    And,
15
15
    Desc,
 
16
    Join,
16
17
    LeftJoin,
17
 
    Join,
18
18
    )
19
19
from storm.locals import (
20
20
    DateTime,
30
30
 
31
31
from canonical.database.constants import UTC_NOW
32
32
from canonical.database.enumcol import DBEnum
 
33
from canonical.launchpad.database.emailaddress import EmailAddress
 
34
from canonical.launchpad.interfaces.emailaddress import EmailAddressStatus
33
35
from lp.registry.interfaces.person import validate_person
34
36
from lp.registry.model.teammembership import TeamParticipation
35
37
from lp.soyuz.interfaces.archiveauthtoken import IArchiveAuthTokenSet
96
98
        store = Store.of(self)
97
99
        if self.subscriber.is_team:
98
100
 
 
101
            # We get all the people who already have active tokens for
 
102
            # this archive (for example, through separate subscriptions).
 
103
            auth_token = LeftJoin(
 
104
                ArchiveAuthToken,
 
105
                And(ArchiveAuthToken.person_id == Person.id,
 
106
                    ArchiveAuthToken.archive_id == self.archive_id,
 
107
                    ArchiveAuthToken.date_deactivated == None))
 
108
 
 
109
            team_participation = Join(
 
110
                TeamParticipation,
 
111
                TeamParticipation.personID == Person.id)
 
112
 
 
113
            # Only return people with preferred email address set.
 
114
            preferred_email = Join(
 
115
                EmailAddress, EmailAddress.personID == Person.id)
 
116
 
99
117
            # We want to get all participants who are themselves
100
118
            # individuals, not teams:
101
 
            all_subscribers = store.find(
102
 
                Person,
 
119
            non_active_subscribers = store.using(
 
120
                Person, team_participation, preferred_email, auth_token).find(
 
121
                (Person, EmailAddress),
 
122
                EmailAddress.status == EmailAddressStatus.PREFERRED,
103
123
                TeamParticipation.teamID == self.subscriber_id,
104
 
                TeamParticipation.personID == Person.id,
105
 
                Person.teamowner == None)
106
 
 
107
 
            # Then we get all the people who already have active
108
 
            # tokens for this archive (for example, through separate
109
 
            # subscriptions).
110
 
            active_subscribers = store.find(
111
 
                Person,
112
 
                Person.id == ArchiveAuthToken.person_id,
113
 
                ArchiveAuthToken.archive_id == self.archive_id,
114
 
                ArchiveAuthToken.date_deactivated == None)
115
 
 
116
 
            # And return just the non active subscribers:
117
 
            non_active_subscribers = all_subscribers.difference(
118
 
                active_subscribers)
 
124
                Person.teamowner == None,
 
125
                # There is no existing archive auth token.
 
126
                ArchiveAuthToken.person_id == None)
119
127
            non_active_subscribers.order_by(Person.name)
120
128
            return non_active_subscribers
121
129
        else:
128
136
                return EmptyResultSet()
129
137
 
130
138
            # Otherwise return a result set containing only the
131
 
            # subscriber.
132
 
            return store.find(Person, Person.id == self.subscriber_id)
 
139
            # subscriber and their preferred email address.
 
140
            return store.find(
 
141
                (Person, EmailAddress),
 
142
                Person.id == self.subscriber_id,
 
143
                EmailAddress.personID == Person.id,
 
144
                EmailAddress.status == EmailAddressStatus.PREFERRED)
133
145
 
134
146
 
135
147
class ArchiveSubscriberSet: