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
|
# Copyright (C) 2010 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/>.
"""REST for mailing lists."""
from __future__ import absolute_import, unicode_literals
__metaclass__ = type
__all__ = [
'AList',
'AllLists',
]
from restish import http, resource
from zope.component import getUtility
from mailman.app.lifecycle import create_list
from mailman.interfaces.domain import BadDomainSpecificationError
from mailman.interfaces.listmanager import (
IListManager, ListAlreadyExistsError)
from mailman.interfaces.member import MemberRole
from mailman.rest.helpers import (
CollectionMixin, Validator, etag, path_to, restish_matcher)
from mailman.rest.members import AMember, MembersOfList
@restish_matcher
def member_matcher(request, segments):
"""A matcher of member URLs inside mailing lists.
e.g. /member/aperson@example.org
"""
if len(segments) != 2:
return None
try:
role = MemberRole[segments[0]]
except ValueError:
# Not a valid role.
return None
# No more segments.
# XXX 2010-02-25 barry Matchers are undocumented in restish; they return a
# 3-tuple of (match_args, match_kws, segments).
return (), dict(role=role, address=segments[1]), ()
@restish_matcher
def roster_matcher(request, segments):
"""A matcher of all members URLs inside mailing lists.
e.g. /roster/members
/roster/owners
/roster/moderators
The URL roles are the plural form of the MemberRole enum, because the
former reads better.
"""
if len(segments) != 2 or segments[0] != 'roster':
return None
role = segments[1][:-1]
try:
return (), dict(role=MemberRole[role]), ()
except ValueError:
# Not a valid role.
return None
class _ListBase(resource.Resource, CollectionMixin):
"""Shared base class for mailing list representations."""
def _resource_as_dict(self, mlist):
"""See `CollectionMixin`."""
return dict(
fqdn_listname=mlist.fqdn_listname,
host_name=mlist.host_name,
list_name=mlist.list_name,
real_name=mlist.real_name,
self_link=path_to('lists/{0}'.format(mlist.fqdn_listname)),
)
def _get_collection(self, request):
"""See `CollectionMixin`."""
return list(getUtility(IListManager))
class AList(_ListBase):
"""A mailing list."""
def __init__(self, list_name):
self._mlist = getUtility(IListManager).get(list_name)
@resource.GET()
def mailing_list(self, request):
"""Return a single mailing list end-point."""
if self._mlist is None:
return http.not_found()
return http.ok([], self._resource_as_json(self._mlist))
@resource.child(member_matcher)
def member(self, request, segments, role, address):
"""Return a single member representation."""
return AMember(self._mlist, role, address)
@resource.child(roster_matcher)
def roster(self, request, segments, role):
"""Return the collection of all a mailing list's members."""
return MembersOfList(self._mlist, role)
class AllLists(_ListBase):
"""The mailing lists."""
@resource.POST()
def create(self, request):
"""Create a new mailing list."""
try:
validator = Validator(fqdn_listname=unicode)
mlist = create_list(**validator(request))
except ListAlreadyExistsError:
return http.bad_request([], b'Mailing list exists')
except BadDomainSpecificationError as error:
return http.bad_request([], b'Domain does not exist {0}'.format(
error.domain))
except ValueError as error:
return http.bad_request([], str(error))
# wsgiref wants headers to be bytes, not unicodes.
location = path_to('lists/{0}'.format(mlist.fqdn_listname))
# Include no extra headers or body.
return http.created(location, [], None)
@resource.GET()
def collection(self, request):
"""/lists"""
resource = self._make_collection(request)
return http.ok([], etag(resource))
|