~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/model/usermanager.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 user 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__ = [
27
27
 
28
28
from zope.interface import implements
29
29
 
30
 
from mailman.config import config
 
30
from mailman.database.transaction import dbconnection
31
31
from mailman.interfaces.address import ExistingAddressError
32
32
from mailman.interfaces.usermanager import IUserManager
33
33
from mailman.model.address import Address
48
48
            user.link(address)
49
49
        return user
50
50
 
51
 
    def delete_user(self, user):
 
51
    @dbconnection
 
52
    def delete_user(self, store, user):
52
53
        """See `IUserManager`."""
53
 
        config.db.store.remove(user)
 
54
        store.remove(user)
54
55
 
55
 
    def get_user(self, email):
 
56
    @dbconnection
 
57
    def get_user(self, store, email):
56
58
        """See `IUserManager`."""
57
 
        addresses = config.db.store.find(Address, email=email.lower())
 
59
        addresses = store.find(Address, email=email.lower())
58
60
        if addresses.count() == 0:
59
61
            return None
60
62
        return addresses.one().user
61
63
 
62
 
    def get_user_by_id(self, user_id):
 
64
    @dbconnection
 
65
    def get_user_by_id(self, store, user_id):
63
66
        """See `IUserManager`."""
64
 
        users = config.db.store.find(User, _user_id=user_id)
 
67
        users = store.find(User, _user_id=user_id)
65
68
        if users.count() == 0:
66
69
            return None
67
70
        return users.one()
68
71
 
69
72
    @property
70
 
    def users(self):
 
73
    @dbconnection
 
74
    def users(self, store):
71
75
        """See `IUserManager`."""
72
 
        for user in config.db.store.find(User):
 
76
        for user in store.find(User):
73
77
            yield user
74
78
 
75
 
    def create_address(self, email, display_name=None):
 
79
    @dbconnection
 
80
    def create_address(self, store, email, display_name=None):
76
81
        """See `IUserManager`."""
77
 
        addresses = config.db.store.find(Address, email=email.lower())
 
82
        addresses = store.find(Address, email=email.lower())
78
83
        if addresses.count() == 1:
79
84
            found = addresses[0]
80
85
            raise ExistingAddressError(found.original_email)
85
90
        # constructor will do the right thing.
86
91
        address = Address(email, display_name)
87
92
        address.preferences = Preferences()
88
 
        config.db.store.add(address)
 
93
        store.add(address)
89
94
        return address
90
95
 
91
 
    def delete_address(self, address):
 
96
    @dbconnection
 
97
    def delete_address(self, store, address):
92
98
        """See `IUserManager`."""
93
99
        # If there's a user controlling this address, it has to first be
94
100
        # unlinked before the address can be deleted.
95
101
        if address.user:
96
102
            address.user.unlink(address)
97
 
        config.db.store.remove(address)
 
103
        store.remove(address)
98
104
 
99
 
    def get_address(self, email):
 
105
    @dbconnection
 
106
    def get_address(self, store, email):
100
107
        """See `IUserManager`."""
101
 
        addresses = config.db.store.find(Address, email=email.lower())
 
108
        addresses = store.find(Address, email=email.lower())
102
109
        if addresses.count() == 0:
103
110
            return None
104
111
        return addresses.one()
105
112
 
106
113
    @property
107
 
    def addresses(self):
 
114
    @dbconnection
 
115
    def addresses(self, store):
108
116
        """See `IUserManager`."""
109
 
        for address in config.db.store.find(Address):
 
117
        for address in store.find(Address):
110
118
            yield address
111
119
 
112
120
    @property
113
 
    def members(self):
 
121
    @dbconnection
 
122
    def members(self, store):
114
123
        """See `IUserManager."""
115
 
        for member in config.db.store.find(Member):
 
124
        for member in store.find(Member):
116
125
                yield member