~barry/mailman/events-and-web

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
# Copyright (C) 2008-2012 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Domains."""

from __future__ import absolute_import, print_function, unicode_literals

__metaclass__ = type
__all__ = [
    'Domain',
    'DomainManager',
    ]


from urlparse import urljoin, urlparse
from storm.locals import Int, Unicode
from zope.event import notify
from zope.interface import implementer

from mailman.database.model import Model
from mailman.database.transaction import dbconnection
from mailman.interfaces.domain import (
    BadDomainSpecificationError, DomainCreatedEvent, DomainCreatingEvent,
    DomainDeletedEvent, DomainDeletingEvent, IDomain, IDomainManager)
from mailman.model.mailinglist import MailingList



@implementer(IDomain)
class Domain(Model):
    """Domains."""

    id = Int(primary=True)

    mail_host = Unicode()
    description = Unicode()
    contact_address = Unicode()

    def __init__(self, mail_host, description=None, contact_address=None):
        """Create and register a domain.

        :param mail_host: The host name for the email interface.
        :type mail_host: string
        :param description: An optional description of the domain.
        :type description: string
        :param contact_address: The email address to contact a human for this
            domain.  If not given, postmaster@`mail_host` will be used.
        :type contact_address: string
        """
        self.mail_host = mail_host
        self.description = description
        self.contact_address = (contact_address
                                if contact_address is not None
                                else 'postmaster@' + mail_host)

    @property
    @dbconnection
    def mailing_lists(self, store):
        """See `IDomain`."""
        mailing_lists = store.find(
            MailingList,
            MailingList.mail_host == self.mail_host)
        for mlist in mailing_lists:
            yield mlist

    def __repr__(self):
        """repr(a_domain)"""
        if self.description is None:
            return ('<Domain {0.mail_host}, '
                    'contact_address: {0.contact_address}>').format(self)
        else:
            return ('<Domain {0.mail_host}, {0.description}, '
                    'contact_address: {0.contact_address}>').format(self)



@implementer(IDomainManager)
class DomainManager:
    """Domain manager."""

    @dbconnection
    def add(self, store, mail_host, description=None, contact_address=None):
        """See `IDomainManager`."""
        # Be sure the mail_host is not already registered.  This is probably
        # a constraint that should (also) be maintained in the database.
        if self.get(mail_host) is not None:
            raise BadDomainSpecificationError(
                'Duplicate email host: %s' % mail_host)
        notify(DomainCreatingEvent(mail_host))
        domain = Domain(mail_host, description, contact_address)
        store.add(domain)
        notify(DomainCreatedEvent(domain))
        return domain

    @dbconnection
    def remove(self, store, mail_host):
        domain = self[mail_host]
        notify(DomainDeletingEvent(domain))
        store.remove(domain)
        notify(DomainDeletedEvent(mail_host))
        return domain

    @dbconnection
    def get(self, store, mail_host, default=None):
        """See `IDomainManager`."""
        domains = store.find(Domain, mail_host=mail_host)
        if domains.count() < 1:
            return default
        assert domains.count() == 1, (
            'Too many matching domains: %s' % mail_host)
        return domains.one()

    def __getitem__(self, mail_host):
        """See `IDomainManager`."""
        missing = object()
        domain = self.get(mail_host, missing)
        if domain is missing:
            raise KeyError(mail_host)
        return domain

    @dbconnection
    def __len__(self, store):
        return store.find(Domain).count()

    @dbconnection
    def __iter__(self, store):
        """See `IDomainManager`."""
        for domain in store.find(Domain):
            yield domain

    @dbconnection
    def __contains__(self, store, mail_host):
        """See `IDomainManager`."""
        return store.find(Domain, mail_host=mail_host).count() > 0