~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/model/autorespond.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__ = [
29
29
from storm.locals import And, Date, Desc, Int, Reference
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.autorespond import (
36
36
    IAutoResponseRecord, IAutoResponseSet, Response)
66
66
    def __init__(self, mailing_list):
67
67
        self._mailing_list = mailing_list
68
68
 
69
 
    def todays_count(self, address, response_type):
 
69
    @dbconnection
 
70
    def todays_count(self, store, address, response_type):
70
71
        """See `IAutoResponseSet`."""
71
 
        return config.db.store.find(
 
72
        return store.find(
72
73
            AutoResponseRecord,
73
74
            And(AutoResponseRecord.address == address,
74
75
                AutoResponseRecord.mailing_list == self._mailing_list,
75
76
                AutoResponseRecord.response_type == response_type,
76
77
                AutoResponseRecord.date_sent == today())).count()
77
78
 
78
 
    def response_sent(self, address, response_type):
 
79
    @dbconnection
 
80
    def response_sent(self, store, address, response_type):
79
81
        """See `IAutoResponseSet`."""
80
82
        response = AutoResponseRecord(
81
83
            self._mailing_list, address, response_type)
82
 
        config.db.store.add(response)
 
84
        store.add(response)
83
85
 
84
 
    def last_response(self, address, response_type):
 
86
    @dbconnection
 
87
    def last_response(self, store, address, response_type):
85
88
        """See `IAutoResponseSet`."""
86
 
        results = config.db.store.find(
 
89
        results = store.find(
87
90
            AutoResponseRecord,
88
91
            And(AutoResponseRecord.address == address,
89
92
                AutoResponseRecord.mailing_list == self._mailing_list,
90
93
                AutoResponseRecord.response_type == response_type)
91
 
            ).order_by(Desc(AutoResponseRecord.date_sent)) 
 
94
            ).order_by(Desc(AutoResponseRecord.date_sent))
92
95
        return (None if results.count() == 0 else results.first())