Add per list member roster visibility option
[mailman.git] / src / mailman / rest / queues.py
blob719e96888d7e484baa9c5ee95c9d1e94da22cb4a
1 # Copyright (C) 2015-2019 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)
8 # any later version.
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
13 # more details.
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 """<api>/queues."""
20 from mailman.app.inject import inject_text
21 from mailman.config import config
22 from mailman.interfaces.listmanager import IListManager
23 from mailman.rest.helpers import (
24 CollectionMixin, bad_request, created, etag, no_content, not_found, okay)
25 from mailman.rest.validator import Validator
26 from public import public
27 from zope.component import getUtility
30 class _QueuesBase(CollectionMixin):
31 """Shared base class for queues."""
33 def _resource_as_dict(self, name):
34 """See `CollectionMixin`."""
35 switchboard = config.switchboards[name]
36 files = switchboard.files
37 return dict(
38 name=switchboard.name,
39 directory=switchboard.queue_directory,
40 count=len(files),
41 files=files,
42 self_link=self.api.path_to('queues/{}'.format(name)),
45 def _get_collection(self, request):
46 """See `CollectionMixin`."""
47 return sorted(config.switchboards)
50 @public
51 class AQueue(_QueuesBase):
52 """A single queue."""
54 def __init__(self, name):
55 self._name = name
57 def on_get(self, request, response):
58 """Return a single queue resource."""
59 if self._name not in config.switchboards:
60 not_found(response)
61 else:
62 okay(response, self._resource_as_json(self._name))
64 def on_post(self, request, response):
65 """Inject a message into the queue."""
66 try:
67 validator = Validator(list_id=str,
68 text=str)
69 values = validator(request)
70 except ValueError as error:
71 bad_request(response, str(error))
72 return
73 list_id = values['list_id']
74 mlist = getUtility(IListManager).get_by_list_id(list_id)
75 if mlist is None:
76 bad_request(response, 'No such list: {}'.format(list_id))
77 return
78 try:
79 filebase = inject_text(
80 mlist, values['text'], switchboard=self._name)
81 except Exception as error:
82 bad_request(response, str(error))
83 return
84 else:
85 location = self.api.path_to(
86 'queues/{}/{}'.format(self._name, filebase))
87 created(response, location)
90 @public
91 class AQueueFile:
92 def __init__(self, name, filebase):
93 self._name = name
94 self._filebase = filebase
96 def on_delete(self, request, response):
97 """Delete the queue file."""
98 switchboard = config.switchboards.get(self._name)
99 if switchboard is None:
100 not_found(response, 'No such queue: {}'.format(self._name))
101 return
102 try:
103 switchboard.dequeue(self._filebase)
104 except FileNotFoundError:
105 not_found(response,
106 'No such queue file: {}'.format(self._filebase))
107 else:
108 no_content(response)
111 @public
112 class AllQueues(_QueuesBase):
113 """All queues."""
115 def on_get(self, request, response):
116 """<api>/queues"""
117 resource = self._make_collection(request)
118 resource['self_link'] = self.api.path_to('queues')
119 okay(response, etag(resource))