~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/model/requests.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
"""Implementations of the pending requests interfaces."""
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__ = [
29
29
from zope.component import getUtility
30
30
from zope.interface import implements
31
31
 
32
 
from mailman.config import config
33
32
from mailman.database.model import Model
 
33
from mailman.database.transaction import dbconnection
34
34
from mailman.database.types import Enum
35
35
from mailman.interfaces.pending import IPendable, IPendings
36
36
from mailman.interfaces.requests import IListRequests, RequestType
49
49
        self.mailing_list = mailing_list
50
50
 
51
51
    @property
52
 
    def count(self):
53
 
        return config.db.store.find(
54
 
            _Request, mailing_list=self.mailing_list).count()
 
52
    @dbconnection
 
53
    def count(self, store):
 
54
        return store.find(_Request, mailing_list=self.mailing_list).count()
55
55
 
56
 
    def count_of(self, request_type):
57
 
        return config.db.store.find(
 
56
    @dbconnection
 
57
    def count_of(self, store, request_type):
 
58
        return store.find(
58
59
            _Request,
59
60
            mailing_list=self.mailing_list, request_type=request_type).count()
60
61
 
61
62
    @property
62
 
    def held_requests(self):
63
 
        results = config.db.store.find(
64
 
            _Request, mailing_list=self.mailing_list)
 
63
    @dbconnection
 
64
    def held_requests(self, store):
 
65
        results = store.find(_Request, mailing_list=self.mailing_list)
65
66
        for request in results:
66
67
            yield request
67
68
 
68
 
    def of_type(self, request_type):
69
 
        results = config.db.store.find(
 
69
    @dbconnection
 
70
    def of_type(self, store, request_type):
 
71
        results = store.find(
70
72
            _Request,
71
73
            mailing_list=self.mailing_list, request_type=request_type)
72
74
        for request in results:
73
75
            yield request
74
76
 
75
 
    def hold_request(self, request_type, key, data=None):
 
77
    @dbconnection
 
78
    def hold_request(self, store, request_type, key, data=None):
76
79
        if request_type not in RequestType:
77
80
            raise TypeError(request_type)
78
81
        if data is None:
87
90
            token = getUtility(IPendings).add(pendable, timedelta(days=5000))
88
91
            data_hash = token
89
92
        request = _Request(key, request_type, self.mailing_list, data_hash)
90
 
        config.db.store.add(request)
 
93
        store.add(request)
91
94
        return request.id
92
95
 
93
 
    def get_request(self, request_id, request_type=None):
94
 
        result = config.db.store.get(_Request, request_id)
 
96
    @dbconnection
 
97
    def get_request(self, store, request_id, request_type=None):
 
98
        result = store.get(_Request, request_id)
95
99
        if result is None:
96
100
            return None
97
101
        if request_type is not None and result.request_type != request_type:
104
108
        data.update(pendable)
105
109
        return result.key, data
106
110
 
107
 
    def delete_request(self, request_id):
108
 
        request = config.db.store.get(_Request, request_id)
 
111
    @dbconnection
 
112
    def delete_request(self, store, request_id):
 
113
        request = store.get(_Request, request_id)
109
114
        if request is None:
110
115
            raise KeyError(request_id)
111
116
        # Throw away the pended data.
112
117
        getUtility(IPendings).confirm(request.data_hash)
113
 
        config.db.store.remove(request)
 
118
        store.remove(request)
114
119
 
115
120
 
116
121