~barry/mailman/events-and-web

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# Copyright (C) 2007-2012 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""An implementation of an IRoster.

These are hard-coded rosters which know how to filter a set of members to find
the ones that fit a particular role.  These are used as the member, owner,
moderator, and administrator roster filters.
"""

from __future__ import absolute_import, print_function, unicode_literals

__metaclass__ = type
__all__ = [
    'AdministratorRoster',
    'DigestMemberRoster',
    'MemberRoster',
    'Memberships',
    'ModeratorRoster',
    'OwnerRoster',
    'RegularMemberRoster',
    'Subscribers',
    ]


from storm.expr import And, Or
from zope.interface import implements

from mailman.database.transaction import dbconnection
from mailman.interfaces.member import DeliveryMode, MemberRole
from mailman.interfaces.roster import IRoster
from mailman.model.address import Address
from mailman.model.member import Member



class AbstractRoster:
    """An abstract IRoster class.

    This class takes the simple approach of implemented the 'users' and
    'addresses' properties in terms of the 'members' property.  This may not
    be the most efficient way, but it works.

    This requires that subclasses implement the 'members' property.
    """
    implements(IRoster)

    role = None

    def __init__(self, mlist):
        self._mlist = mlist

    @dbconnection
    def _query(self, store):
        return store.find(
            Member,
            mailing_list=self._mlist.fqdn_listname,
            role=self.role)

    @property
    def members(self):
        """See `IRoster`."""
        for member in self._query():
            yield member

    @property
    def member_count(self):
        """See `IRoster`."""
        return self._query().count()

    @property
    def users(self):
        """See `IRoster`."""
        # Members are linked to addresses, which in turn are linked to users.
        # So while the 'members' attribute does most of the work, we have to
        # keep a set of unique users.  It's possible for the same user to be
        # subscribed to a mailing list multiple times with different
        # addresses.
        users = set(member.address.user for member in self.members)
        for user in users:
            yield user

    @property
    def addresses(self):
        """See `IRoster`."""
        # Every Member is linked to exactly one address so the 'members'
        # attribute does most of the work.
        for member in self.members:
            yield member.address

    @dbconnection
    def get_member(self, store, address):
        """See `IRoster`."""
        results = store.find(
            Member,
            Member.mailing_list == self._mlist.fqdn_listname,
            Member.role == self.role,
            Address.email == address,
            Member.address_id == Address.id)
        if results.count() == 0:
            return None
        elif results.count() == 1:
            return results[0]
        else:
            raise AssertionError(
                'Too many matching member results: {0}'.format(
                    results.count()))



class MemberRoster(AbstractRoster):
    """Return all the members of a list."""

    name = 'member'
    role = MemberRole.member



class NonmemberRoster(AbstractRoster):
    """Return all the nonmembers of a list."""

    name = 'nonmember'
    role = MemberRole.nonmember



class OwnerRoster(AbstractRoster):
    """Return all the owners of a list."""

    name = 'owner'
    role = MemberRole.owner



class ModeratorRoster(AbstractRoster):
    """Return all the owners of a list."""

    name = 'moderator'
    role = MemberRole.moderator



class AdministratorRoster(AbstractRoster):
    """Return all the administrators of a list."""

    name = 'administrator'

    @dbconnection
    def _query(self, store):
        return store.find(
            Member,
            Member.mailing_list == self._mlist.fqdn_listname,
            Or(Member.role == MemberRole.owner,
               Member.role == MemberRole.moderator))

    @dbconnection
    def get_member(self, store, address):
        """See `IRoster`."""
        results = store.find(
                Member,
                Member.mailing_list == self._mlist.fqdn_listname,
                Or(Member.role == MemberRole.moderator,
                   Member.role == MemberRole.owner),
                Address.email == address,
                Member.address_id == Address.id)
        if results.count() == 0:
            return None
        elif results.count() == 1:
            return results[0]
        else:
            raise AssertionError(
                'Too many matching member results: {0}'.format(results))



class DeliveryMemberRoster(AbstractRoster):
    """Return all the members having a particular kind of delivery."""

    @property
    def member_count(self):
        """See `IRoster`."""
        # XXX 2012-03-15 BAW: It would be nice to make this more efficient.
        # The problem is that you'd have to change the loop in _get_members()
        # checking the delivery mode to a query parameter.
        return len(tuple(self.members))

    @dbconnection
    def _get_members(self, store, *delivery_modes):
        """The set of members for a mailing list, filter by delivery mode.

        :param delivery_modes: The modes to filter on.
        :type delivery_modes: sequence of `DeliveryMode`.
        :return: A generator of members.
        :rtype: generator
        """
        results = store.find(
            Member,
            And(Member.mailing_list == self._mlist.fqdn_listname,
                Member.role == MemberRole.member))
        for member in results:
            if member.delivery_mode in delivery_modes:
                yield member


class RegularMemberRoster(DeliveryMemberRoster):
    """Return all the regular delivery members of a list."""

    name = 'regular_members'

    @property
    def members(self):
        """See `IRoster`."""
        for member in self._get_members(DeliveryMode.regular):
            yield member



class DigestMemberRoster(DeliveryMemberRoster):
    """Return all the regular delivery members of a list."""

    name = 'digest_members'

    @property
    def members(self):
        """See `IRoster`."""
        for member in self._get_members(DeliveryMode.plaintext_digests,
                                        DeliveryMode.mime_digests,
                                        DeliveryMode.summary_digests):
            yield member



class Subscribers(AbstractRoster):
    """Return all subscribed members regardless of their role."""

    name = 'subscribers'

    @dbconnection
    def _query(self, store):
        return store.find(Member, mailing_list=self._mlist.fqdn_listname)



class Memberships:
    """A roster of a single user's memberships."""

    implements(IRoster)

    name = 'memberships'

    def __init__(self, user):
        self._user = user

    @dbconnection
    def _query(self, store):
        results = store.find(
            Member,
            Or(Member.user_id == self._user.id,
               And(Address.user_id == self._user.id,
                   Member.address_id == Address.id)))
        return results.config(distinct=True)

    @property
    def member_count(self):
        """See `IRoster`."""
        return self._query().count()

    @property
    def members(self):
        """See `IRoster`."""
        for member in self._query():
            yield member

    @property
    def users(self):
        """See `IRoster`."""
        yield self._user

    @property
    def addresses(self):
        """See `IRoster`."""
        for address in self._user.addresses:
            yield address

    @dbconnection
    def get_member(self, store, address):
        """See `IRoster`."""
        results = store.find(
            Member,
            Member.address_id == Address.id,
            Address.user_id == self._user.id)
        if results.count() == 0:
            return None
        elif results.count() == 1:
            return results[0]
        else:
            raise AssertionError(
                'Too many matching member results: {0}'.format(
                    results.count()))