~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright 2009-2012 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=E0611,W0212

__metaclass__ = type
__all__ = [
    'EmailAddress',
    'EmailAddressSet',
    'HasOwnerMixin',
    'UndeletableEmailAddress',
    ]


import hashlib
import operator

from sqlobject import (
    ForeignKey,
    StringCol,
    )
from zope.interface import implements

from lp.app.validators.email import valid_email
from lp.services.database.enumcol import EnumCol
from lp.services.database.sqlbase import (
    quote,
    SQLBase,
    sqlvalues,
    )
from lp.services.identity.interfaces.emailaddress import (
    EmailAddressAlreadyTaken,
    EmailAddressStatus,
    IEmailAddress,
    IEmailAddressSet,
    InvalidEmailAddress,
    )


class HasOwnerMixin:
    """A mixing providing an 'owner' property which returns self.person.

    This is to be used on content classes who want to provide IHasOwner but
    have the owner stored in an attribute named 'person' rather than 'owner'.
    """
    owner = property(operator.attrgetter('person'))


class EmailAddress(SQLBase, HasOwnerMixin):
    implements(IEmailAddress)

    _table = 'EmailAddress'
    _defaultOrder = ['email']

    email = StringCol(
            dbName='email', notNull=True, unique=True, alternateID=True)
    status = EnumCol(dbName='status', schema=EmailAddressStatus, notNull=True)
    person = ForeignKey(dbName='person', foreignKey='Person', notNull=False)
    account = ForeignKey(
            dbName='account', foreignKey='Account', notNull=False,
            default=None)

    def __repr__(self):
        return '<EmailAddress at 0x%x <%s> [%s]>' % (
            id(self), self.email, self.status)

    def destroySelf(self):
        """See `IEmailAddress`."""
        # Import this here to avoid circular references.
        from lp.registry.interfaces.mailinglist import MailingListStatus
        from lp.registry.model.mailinglist import (
            MailingListSubscription)

        if self.status == EmailAddressStatus.PREFERRED:
            raise UndeletableEmailAddress(
                "This is a person's preferred email, so it can't be deleted.")
        mailing_list = self.person and self.person.mailing_list
        if (mailing_list is not None
            and mailing_list.status != MailingListStatus.PURGED
            and mailing_list.address == self.email):
            raise UndeletableEmailAddress(
                "This is the email address of a team's mailing list, so it "
                "can't be deleted.")

        # XXX 2009-05-04 jamesh bug=371567: This function should not
        # be responsible for removing subscriptions, since the SSO
        # server can't write to that table.
        for subscription in MailingListSubscription.selectBy(
            email_address=self):
            subscription.destroySelf()
        super(EmailAddress, self).destroySelf()

    @property
    def rdf_sha1(self):
        """See `IEmailAddress`."""
        return hashlib.sha1('mailto:' + self.email).hexdigest().upper()


class EmailAddressSet:
    implements(IEmailAddressSet)

    def getByPerson(self, person):
        """See `IEmailAddressSet`."""
        return EmailAddress.selectBy(person=person, orderBy='email')

    def getPreferredEmailForPeople(self, people):
        """See `IEmailAddressSet`."""
        return EmailAddress.select("""
            EmailAddress.status = %s AND
            EmailAddress.person IN %s
            """ % sqlvalues(EmailAddressStatus.PREFERRED,
                            [person.id for person in people]))

    def getByEmail(self, email):
        """See `IEmailAddressSet`."""
        return EmailAddress.selectOne(
            "lower(email) = %s" % quote(email.strip().lower()))

    def new(self, email, person=None, status=EmailAddressStatus.NEW,
            account=None):
        """See IEmailAddressSet."""
        email = email.strip()

        if not valid_email(email):
            raise InvalidEmailAddress(
                "%s is not a valid email address." % email)

        if self.getByEmail(email) is not None:
            raise EmailAddressAlreadyTaken(
                "The email address '%s' is already registered." % email)
        assert status in EmailAddressStatus.items
        if person is None:
            personID = None
        else:
            if account is None:
                account = person.account
            personID = person.id
            accountID = account and account.id
            assert person.accountID == accountID, (
                "Email address '%s' must be linked to same account as "
                "person '%s'.  Expected %r (%s), got %r (%s)" % (
                    email, person.name, person.account, person.accountID,
                    account, accountID))
        # We use personID instead of just person, as in some cases the
        # Person record will not yet be replicated from the main
        # Store to the auth master Store.
        return EmailAddress(
            email=email,
            status=status,
            personID=personID,
            account=account)


class UndeletableEmailAddress(Exception):
    """User attempted to delete an email address which can't be deleted."""