~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/app/subscriptions.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
"""Module stuff."""
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__ = [
36
36
from mailman.app.membership import add_member, delete_member
37
37
from mailman.config import config
38
38
from mailman.core.constants import system_preferences
 
39
from mailman.database.transaction import dbconnection
39
40
from mailman.interfaces.address import IEmailValidator
40
41
from mailman.interfaces.listmanager import (
41
42
    IListManager, ListDeletedEvent, NoSuchListError)
90
91
                sorted(by_role.get('member', []), key=address_of_member))
91
92
        return all_members
92
93
 
93
 
    def get_member(self, member_id):
 
94
    @dbconnection
 
95
    def get_member(self, store, member_id):
94
96
        """See `ISubscriptionService`."""
95
 
        members = config.db.store.find(
 
97
        members = store.find(
96
98
            Member,
97
99
            Member._member_id == member_id)
98
100
        if members.count() == 0:
101
103
            assert members.count() == 1, 'Too many matching members'
102
104
            return members[0]
103
105
 
104
 
    def find_members(self, subscriber=None, fqdn_listname=None, role=None):
 
106
    @dbconnection
 
107
    def find_members(self, store,
 
108
                     subscriber=None, fqdn_listname=None, role=None):
105
109
        """See `ISubscriptionService`."""
106
110
        # If `subscriber` is a user id, then we'll search for all addresses
107
111
        # which are controlled by the user, otherwise we'll just search for
137
141
            query.append(Member.mailing_list == fqdn_listname)
138
142
        if role is not None:
139
143
            query.append(Member.role == role)
140
 
        results = config.db.store.find(Member, And(*query))
 
144
        results = store.find(Member, And(*query))
141
145
        return sorted(results, key=_membership_sort_key)
142
146
 
143
147
    def __iter__(self):