~barry/mailman/events-and-web

« back to all changes in this revision

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

  • Committer: Barry Warsaw
  • Date: 2012-04-22 21:33:33 UTC
  • mfrom: (7150.1.11 transactions)
  • Revision ID: barry@list.org-20120422213333-3skjqsjktooesgsl
Several non-functional improvements to the code base.

Reduce the explicit use of the config.db global by introducing two new
helpers:
 - A new transaction() context manager which will commit the transaction on
   successful exit, otherwise it will abort the transaction
 - A new dbconnection decorator which calls the decorated method with the
   Storm store object as the first argument (after self).  This can be used
   instead of config.db.store.

By reducing the explicit use of this global, we have a better chance of
refactoring it away in the future.  Still TODO: runner.py and lmtp.py.

Be explicit about the `store` attribute on the IDatabase interface.

More consistent use of __future__ imports.

Remove an obsolete command line script.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""A mailing list manager."""
19
19
 
20
 
from __future__ import absolute_import, unicode_literals
 
20
from __future__ import absolute_import, print_function, unicode_literals
21
21
 
22
22
__metaclass__ = type
23
23
__all__ = [
28
28
from zope.event import notify
29
29
from zope.interface import implements
30
30
 
31
 
from mailman.config import config
 
31
from mailman.database.transaction import dbconnection
32
32
from mailman.interfaces.address import InvalidEmailAddressError
33
33
from mailman.interfaces.listmanager import (
34
34
    IListManager, ListAlreadyExistsError, ListCreatedEvent, ListCreatingEvent,
43
43
 
44
44
    implements(IListManager)
45
45
 
46
 
    def create(self, fqdn_listname):
 
46
    @dbconnection
 
47
    def create(self, store, fqdn_listname):
47
48
        """See `IListManager`."""
48
49
        listname, at, hostname = fqdn_listname.partition('@')
49
50
        if len(hostname) == 0:
50
51
            raise InvalidEmailAddressError(fqdn_listname)
51
52
        notify(ListCreatingEvent(fqdn_listname))
52
 
        mlist = config.db.store.find(
 
53
        mlist = store.find(
53
54
            MailingList,
54
55
            MailingList.list_name == listname,
55
56
            MailingList.mail_host == hostname).one()
57
58
            raise ListAlreadyExistsError(fqdn_listname)
58
59
        mlist = MailingList(fqdn_listname)
59
60
        mlist.created_at = now()
60
 
        config.db.store.add(mlist)
 
61
        store.add(mlist)
61
62
        notify(ListCreatedEvent(mlist))
62
63
        return mlist
63
64
 
64
 
    def get(self, fqdn_listname):
 
65
    @dbconnection
 
66
    def get(self, store, fqdn_listname):
65
67
        """See `IListManager`."""
66
68
        listname, at, hostname = fqdn_listname.partition('@')
67
 
        return config.db.store.find(MailingList,
68
 
                                    list_name=listname,
69
 
                                    mail_host=hostname).one()
 
69
        return store.find(MailingList,
 
70
                          list_name=listname,
 
71
                          mail_host=hostname).one()
70
72
 
71
 
    def delete(self, mlist):
 
73
    @dbconnection
 
74
    def delete(self, store, mlist):
72
75
        """See `IListManager`."""
73
76
        fqdn_listname = mlist.fqdn_listname
74
77
        notify(ListDeletingEvent(mlist))
75
 
        config.db.store.remove(mlist)
 
78
        store.remove(mlist)
76
79
        notify(ListDeletedEvent(fqdn_listname))
77
80
 
78
81
    @property
79
 
    def mailing_lists(self):
 
82
    @dbconnection
 
83
    def mailing_lists(self, store):
80
84
        """See `IListManager`."""
81
 
        for mlist in config.db.store.find(MailingList):
 
85
        for mlist in store.find(MailingList):
82
86
            yield mlist
83
87
 
84
 
    def __iter__(self):
 
88
    @dbconnection
 
89
    def __iter__(self, store):
85
90
        """See `IListManager`."""
86
 
        for mlist in config.db.store.find(MailingList):
 
91
        for mlist in store.find(MailingList):
87
92
            yield mlist
88
93
 
89
94
    @property
90
 
    def names(self):
 
95
    @dbconnection
 
96
    def names(self, store):
91
97
        """See `IListManager`."""
92
 
        result_set = config.db.store.find(MailingList)
93
 
        for mail_host, list_name in result_set.values(MailingList.mail_host, 
 
98
        result_set = store.find(MailingList)
 
99
        for mail_host, list_name in result_set.values(MailingList.mail_host,
94
100
                                                      MailingList.list_name):
95
101
            yield '{0}@{1}'.format(list_name, mail_host)
96
102
 
97
103
    @property
98
 
    def name_components(self):
 
104
    @dbconnection
 
105
    def name_components(self, store):
99
106
        """See `IListManager`."""
100
 
        result_set = config.db.store.find(MailingList)
101
 
        for mail_host, list_name in result_set.values(MailingList.mail_host, 
 
107
        result_set = store.find(MailingList)
 
108
        for mail_host, list_name in result_set.values(MailingList.mail_host,
102
109
                                                      MailingList.list_name):
103
110
            yield list_name, mail_host