diff options
| author | Barry Warsaw | 2015-01-04 20:20:33 -0500 |
|---|---|---|
| committer | Barry Warsaw | 2015-01-04 20:20:33 -0500 |
| commit | 4a612db8e89afed74173b93f3b64fa567b8417a3 (patch) | |
| tree | 81a687d113079a25f93279f35c7eee2aa2572510 /src/mailman/rest/queues.py | |
| parent | 84af79988a4e916604cba31843778206efb7d1b8 (diff) | |
| parent | de181c1a40965a3a7deedd56a034a946f45b6984 (diff) | |
| download | mailman-4a612db8e89afed74173b93f3b64fa567b8417a3.tar.gz mailman-4a612db8e89afed74173b93f3b64fa567b8417a3.tar.zst mailman-4a612db8e89afed74173b93f3b64fa567b8417a3.zip | |
Diffstat (limited to 'src/mailman/rest/queues.py')
| -rw-r--r-- | src/mailman/rest/queues.py | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/mailman/rest/queues.py b/src/mailman/rest/queues.py new file mode 100644 index 000000000..f1007052e --- /dev/null +++ b/src/mailman/rest/queues.py @@ -0,0 +1,129 @@ +# Copyright (C) 2015 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/>. + +"""<api>/queues.""" + +__all__ = [ + 'AQueue', + 'AQueueFile', + 'AllQueues', + ] + + +import six + +from mailman.config import config +from mailman.app.inject import inject_text +from mailman.interfaces.listmanager import IListManager +from mailman.rest.helpers import ( + CollectionMixin, bad_request, created, etag, no_content, not_found, okay, + paginate, path_to) +from mailman.rest.validator import Validator +from zope.component import getUtility + + + +class _QueuesBase(CollectionMixin): + """Shared base class for queues.""" + + def _resource_as_dict(self, name): + """See `CollectionMixin`.""" + switchboard = config.switchboards[name] + files = switchboard.files + return dict( + name=switchboard.name, + directory=switchboard.queue_directory, + count=len(files), + files=files, + self_link=path_to('queues/{}'.format(name)), + ) + + @paginate + def _get_collection(self, request): + """See `CollectionMixin`.""" + return sorted(config.switchboards) + + + +class AQueue(_QueuesBase): + """A single queue.""" + + def __init__(self, name): + self._name = name + + def on_get(self, request, response): + """Return a single queue resource.""" + if self._name not in config.switchboards: + not_found(response) + else: + okay(response, self._resource_as_json(self._name)) + + def on_post(self, request, response): + """Inject a message into the queue.""" + try: + validator = Validator(list_id=six.text_type, + text=six.text_type) + values = validator(request) + except ValueError as error: + bad_request(response, str(error)) + return + list_id = values['list_id'] + mlist = getUtility(IListManager).get_by_list_id(list_id) + if mlist is None: + bad_request(response, 'No such list: {}'.format(list_id)) + return + try: + filebase = inject_text( + mlist, values['text'], switchboard=self._name) + except Exception as error: + bad_request(response, str(error)) + return + else: + location = path_to('queues/{}/{}'.format(self._name, filebase)) + created(response, location) + + + +class AQueueFile: + def __init__(self, name, filebase): + self._name = name + self._filebase = filebase + + def on_delete(self, request, response): + """Delete the queue file.""" + switchboard = config.switchboards.get(self._name) + if switchboard is None: + not_found(response, 'No such queue: {}'.format(self._name)) + return + try: + switchboard.dequeue(self._filebase) + except FileNotFoundError: + not_found(response, + 'No such queue file: {}'.format(self._filebase)) + else: + no_content(response) + + + +class AllQueues(_QueuesBase): + """All queues.""" + + def on_get(self, request, response): + """<api>/queues""" + resource = self._make_collection(request) + resource['self_link'] = path_to('queues') + okay(response, etag(resource)) |
