1 # Copyright (C) 2007-2015 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """An implementation of an IRoster.
20 These are hard-coded rosters which know how to filter a set of members to find
21 the ones that fit a particular role. These are used as the member, owner,
22 moderator, and administrator roster filters.
26 'AdministratorRoster',
32 'RegularMemberRoster',
37 from mailman
.database
.transaction
import dbconnection
38 from mailman
.interfaces
.member
import DeliveryMode
, MemberRole
39 from mailman
.interfaces
.roster
import IRoster
40 from mailman
.model
.address
import Address
41 from mailman
.model
.member
import Member
42 from sqlalchemy
import and_
, or_
43 from zope
.interface
import implementer
49 """An abstract IRoster class.
51 This class takes the simple approach of implemented the 'users' and
52 'addresses' properties in terms of the 'members' property. This may not
53 be the most efficient way, but it works.
55 This requires that subclasses implement the 'members' property.
59 def __init__(self
, mlist
):
63 def _query(self
, store
):
64 return store
.query(Member
).filter(
65 Member
.list_id
== self
._mlist
.list_id
,
66 Member
.role
== self
.role
)
71 for member
in self
._query
():
75 def member_count(self
):
77 return self
._query
().count()
82 # Members are linked to addresses, which in turn are linked to users.
83 # So while the 'members' attribute does most of the work, we have to
84 # keep a set of unique users. It's possible for the same user to be
85 # subscribed to a mailing list multiple times with different
87 users
= set(member
.address
.user
for member
in self
.members
)
94 # Every Member is linked to exactly one address so the 'members'
95 # attribute does most of the work.
96 for member
in self
.members
:
100 def get_member(self
, store
, email
):
102 results
= self
._query
().filter(
103 Address
.email
== email
,
104 Member
.address_id
== Address
.id)
105 if results
.count() == 0:
107 elif results
.count() == 1:
110 raise AssertionError(
111 'Too many matching member results: {0}'.format(
116 class MemberRoster(AbstractRoster
):
117 """Return all the members of a list."""
120 role
= MemberRole
.member
124 class NonmemberRoster(AbstractRoster
):
125 """Return all the nonmembers of a list."""
128 role
= MemberRole
.nonmember
132 class OwnerRoster(AbstractRoster
):
133 """Return all the owners of a list."""
136 role
= MemberRole
.owner
140 class ModeratorRoster(AbstractRoster
):
141 """Return all the owners of a list."""
144 role
= MemberRole
.moderator
148 class AdministratorRoster(AbstractRoster
):
149 """Return all the administrators of a list."""
151 name
= 'administrator'
154 def _query(self
, store
):
155 return store
.query(Member
).filter(
156 Member
.list_id
== self
._mlist
.list_id
,
157 or_(Member
.role
== MemberRole
.owner
,
158 Member
.role
== MemberRole
.moderator
))
161 def get_member(self
, store
, email
):
163 results
= store
.query(Member
).filter(
164 Member
.list_id
== self
._mlist
.list_id
,
165 or_(Member
.role
== MemberRole
.moderator
,
166 Member
.role
== MemberRole
.owner
),
167 Address
.email
== email
,
168 Member
.address_id
== Address
.id)
169 if results
.count() == 0:
171 elif results
.count() == 1:
174 raise AssertionError(
175 'Too many matching member results: {0}'.format(results
))
179 class DeliveryMemberRoster(AbstractRoster
):
180 """Return all the members having a particular kind of delivery."""
182 role
= MemberRole
.member
185 def member_count(self
):
187 # XXX 2012-03-15 BAW: It would be nice to make this more efficient.
188 # The problem is that you'd have to change the loop in _get_members()
189 # checking the delivery mode to a query parameter.
190 return len(tuple(self
.members
))
193 def _get_members(self
, store
, *delivery_modes
):
194 """The set of members for a mailing list, filter by delivery mode.
196 :param delivery_modes: The modes to filter on.
197 :type delivery_modes: sequence of `DeliveryMode`.
198 :return: A generator of members.
201 results
= store
.query(Member
).filter_by(
202 list_id
= self
._mlist
.list_id
,
203 role
= MemberRole
.member
)
204 for member
in results
:
205 if member
.delivery_mode
in delivery_modes
:
209 class RegularMemberRoster(DeliveryMemberRoster
):
210 """Return all the regular delivery members of a list."""
212 name
= 'regular_members'
217 for member
in self
._get
_members
(DeliveryMode
.regular
):
222 class DigestMemberRoster(DeliveryMemberRoster
):
223 """Return all the regular delivery members of a list."""
225 name
= 'digest_members'
230 for member
in self
._get
_members
(DeliveryMode
.plaintext_digests
,
231 DeliveryMode
.mime_digests
,
232 DeliveryMode
.summary_digests
):
237 class Subscribers(AbstractRoster
):
238 """Return all subscribed members regardless of their role."""
243 def _query(self
, store
):
244 return store
.query(Member
).filter_by(list_id
= self
._mlist
.list_id
)
248 @implementer(IRoster
)
250 """A roster of a single user's memberships."""
254 def __init__(self
, user
):
258 def _query(self
, store
):
259 results
= store
.query(Member
).filter(
260 or_(Member
.user_id
== self
._user
.id,
261 and_(Address
.user_id
== self
._user
.id,
262 Member
.address_id
== Address
.id)))
263 return results
.distinct()
266 def member_count(self
):
268 return self
._query
().count()
273 for member
in self
._query
():
284 for address
in self
._user
.addresses
:
288 def get_member(self
, store
, email
):
290 results
= store
.query(Member
).filter(
291 Member
.address_id
== Address
.id,
292 Address
.user_id
== self
._user
.id)
293 if results
.count() == 0:
295 elif results
.count() == 1:
298 raise AssertionError(
299 'Too many matching member results: {0}'.format(