~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/model/roster.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:
22
22
moderator, and administrator roster filters.
23
23
"""
24
24
 
25
 
from __future__ import absolute_import, unicode_literals
 
25
from __future__ import absolute_import, print_function, unicode_literals
26
26
 
27
27
__metaclass__ = type
28
28
__all__ = [
40
40
from storm.expr import And, Or
41
41
from zope.interface import implements
42
42
 
43
 
from mailman.config import config
 
43
from mailman.database.transaction import dbconnection
44
44
from mailman.interfaces.member import DeliveryMode, MemberRole
45
45
from mailman.interfaces.roster import IRoster
46
46
from mailman.model.address import Address
64
64
    def __init__(self, mlist):
65
65
        self._mlist = mlist
66
66
 
67
 
    def _query(self):
68
 
        return config.db.store.find(
 
67
    @dbconnection
 
68
    def _query(self, store):
 
69
        return store.find(
69
70
            Member,
70
71
            mailing_list=self._mlist.fqdn_listname,
71
72
            role=self.role)
101
102
        for member in self.members:
102
103
            yield member.address
103
104
 
104
 
    def get_member(self, address):
 
105
    @dbconnection
 
106
    def get_member(self, store, address):
105
107
        """See `IRoster`."""
106
 
        results = config.db.store.find(
 
108
        results = store.find(
107
109
            Member,
108
110
            Member.mailing_list == self._mlist.fqdn_listname,
109
111
            Member.role == self.role,
157
159
 
158
160
    name = 'administrator'
159
161
 
160
 
    def _query(self):
161
 
        return config.db.store.find(
 
162
    @dbconnection
 
163
    def _query(self, store):
 
164
        return store.find(
162
165
            Member,
163
166
            Member.mailing_list == self._mlist.fqdn_listname,
164
167
            Or(Member.role == MemberRole.owner,
165
168
               Member.role == MemberRole.moderator))
166
169
 
167
 
    def get_member(self, address):
 
170
    @dbconnection
 
171
    def get_member(self, store, address):
168
172
        """See `IRoster`."""
169
 
        results = config.db.store.find(
 
173
        results = store.find(
170
174
                Member,
171
175
                Member.mailing_list == self._mlist.fqdn_listname,
172
176
                Or(Member.role == MemberRole.moderator,
194
198
        # checking the delivery mode to a query parameter.
195
199
        return len(tuple(self.members))
196
200
 
197
 
    def _get_members(self, *delivery_modes):
 
201
    @dbconnection
 
202
    def _get_members(self, store, *delivery_modes):
198
203
        """The set of members for a mailing list, filter by delivery mode.
199
204
 
200
205
        :param delivery_modes: The modes to filter on.
202
207
        :return: A generator of members.
203
208
        :rtype: generator
204
209
        """
205
 
        results = config.db.store.find(
 
210
        results = store.find(
206
211
            Member,
207
212
            And(Member.mailing_list == self._mlist.fqdn_listname,
208
213
                Member.role == MemberRole.member))
244
249
 
245
250
    name = 'subscribers'
246
251
 
247
 
    def _query(self):
248
 
        return config.db.store.find(
249
 
            Member,
250
 
            mailing_list=self._mlist.fqdn_listname)
 
252
    @dbconnection
 
253
    def _query(self, store):
 
254
        return store.find(Member, mailing_list=self._mlist.fqdn_listname)
251
255
 
252
256
 
253
257
 
261
265
    def __init__(self, user):
262
266
        self._user = user
263
267
 
264
 
    def _query(self):
265
 
        results = config.db.store.find(
 
268
    @dbconnection
 
269
    def _query(self, store):
 
270
        results = store.find(
266
271
            Member,
267
272
            Or(Member.user_id == self._user.id,
268
273
               And(Address.user_id == self._user.id,
291
296
        for address in self._user.addresses:
292
297
            yield address
293
298
 
294
 
    def get_member(self, address):
 
299
    @dbconnection
 
300
    def get_member(self, store, address):
295
301
        """See `IRoster`."""
296
 
        results = config.db.store.find(
 
302
        results = store.find(
297
303
            Member,
298
304
            Member.address_id == Address.id,
299
305
            Address.user_id == self._user.id)