summaryrefslogtreecommitdiff
path: root/src/mailman/rest/queues.py
diff options
context:
space:
mode:
authorBarry Warsaw2015-01-03 00:06:17 -0500
committerBarry Warsaw2015-01-03 00:06:17 -0500
commitde181c1a40965a3a7deedd56a034a946f45b6984 (patch)
treef5afb9467d8dde66b6c9693f225bfc01f18fd8aa /src/mailman/rest/queues.py
parentc5e5a12e9a79cbf2cc6bf65ceec7391ce3844ba3 (diff)
downloadmailman-de181c1a40965a3a7deedd56a034a946f45b6984.tar.gz
mailman-de181c1a40965a3a7deedd56a034a946f45b6984.tar.zst
mailman-de181c1a40965a3a7deedd56a034a946f45b6984.zip
Diffstat (limited to 'src/mailman/rest/queues.py')
-rw-r--r--src/mailman/rest/queues.py129
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))