~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/model/address.py

  • Committer: klm
  • Date: 1998-01-07 21:21:35 UTC
  • Revision ID: vcs-imports@canonical.com-19980107212135-sv0y521ps0xye37r
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012 by the Free Software Foundation, Inc.
2
 
#
3
 
# This file is part of GNU Mailman.
4
 
#
5
 
# GNU Mailman is free software: you can redistribute it and/or modify it under
6
 
# the terms of the GNU General Public License as published by the Free
7
 
# Software Foundation, either version 3 of the License, or (at your option)
8
 
# any later version.
9
 
#
10
 
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11
 
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
 
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13
 
# more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License along with
16
 
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.
17
 
 
18
 
"""Model for addresses."""
19
 
 
20
 
from __future__ import absolute_import, print_function, unicode_literals
21
 
 
22
 
__metaclass__ = type
23
 
__all__ = [
24
 
    'Address',
25
 
    ]
26
 
 
27
 
 
28
 
from email.utils import formataddr
29
 
from storm.locals import DateTime, Int, Reference, Unicode
30
 
from zope.event import notify
31
 
from zope.interface import implementer
32
 
 
33
 
from mailman.database.model import Model
34
 
from mailman.interfaces.address import AddressVerificationEvent, IAddress
35
 
from mailman.utilities.datetime import now
36
 
 
37
 
 
38
 
 
39
 
@implementer(IAddress)
40
 
class Address(Model):
41
 
    """See `IAddress`."""
42
 
 
43
 
    id = Int(primary=True)
44
 
    email = Unicode()
45
 
    _original = Unicode()
46
 
    display_name = Unicode()
47
 
    _verified_on = DateTime(name='verified_on')
48
 
    registered_on = DateTime()
49
 
 
50
 
    user_id = Int()
51
 
    user = Reference(user_id, 'User.id')
52
 
    preferences_id = Int()
53
 
    preferences = Reference(preferences_id, 'Preferences.id')
54
 
 
55
 
    def __init__(self, email, display_name):
56
 
        super(Address, self).__init__()
57
 
        lower_case = email.lower()
58
 
        self.email = lower_case
59
 
        self.display_name = display_name
60
 
        self._original = (None if lower_case == email else email)
61
 
        self.registered_on = now()
62
 
 
63
 
    def __str__(self):
64
 
        addr = (self.email if self._original is None else self._original)
65
 
        return formataddr((self.display_name, addr))
66
 
 
67
 
    def __repr__(self):
68
 
        verified = ('verified' if self.verified_on else 'not verified')
69
 
        address_str = str(self)
70
 
        if self._original is None:
71
 
            return '<Address: {0} [{1}] at {2:#x}>'.format(
72
 
                address_str, verified, id(self))
73
 
        else:
74
 
            return '<Address: {0} [{1}] key: {2} at {3:#x}>'.format(
75
 
                address_str, verified, self.email, id(self))
76
 
 
77
 
    @property
78
 
    def verified_on(self):
79
 
        return self._verified_on
80
 
 
81
 
    @verified_on.setter
82
 
    def verified_on(self, timestamp):
83
 
        self._verified_on = timestamp
84
 
        notify(AddressVerificationEvent(self))
85
 
 
86
 
    @property
87
 
    def original_email(self):
88
 
        return (self.email if self._original is None else self._original)