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)
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/>.
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
38 name
=switchboard
.name
,
39 directory
=switchboard
.queue_directory
,
42 self_link
=self
.api
.path_to('queues/{}'.format(name
)),
45 def _get_collection(self
, request
):
46 """See `CollectionMixin`."""
47 return sorted(config
.switchboards
)
51 class AQueue(_QueuesBase
):
54 def __init__(self
, name
):
57 def on_get(self
, request
, response
):
58 """Return a single queue resource."""
59 if self
._name
not in config
.switchboards
:
62 okay(response
, self
._resource
_as
_json
(self
._name
))
64 def on_post(self
, request
, response
):
65 """Inject a message into the queue."""
67 validator
= Validator(list_id
=str,
69 values
= validator(request
)
70 except ValueError as error
:
71 bad_request(response
, str(error
))
73 list_id
= values
['list_id']
74 mlist
= getUtility(IListManager
).get_by_list_id(list_id
)
76 bad_request(response
, 'No such list: {}'.format(list_id
))
79 filebase
= inject_text(
80 mlist
, values
['text'], switchboard
=self
._name
)
81 except Exception as error
:
82 bad_request(response
, str(error
))
85 location
= self
.api
.path_to(
86 'queues/{}/{}'.format(self
._name
, filebase
))
87 created(response
, location
)
92 def __init__(self
, name
, filebase
):
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
))
103 switchboard
.dequeue(self
._filebase
)
104 except FileNotFoundError
:
106 'No such queue file: {}'.format(self
._filebase
))
112 class AllQueues(_QueuesBase
):
115 def on_get(self
, request
, response
):
117 resource
= self
._make
_collection
(request
)
118 resource
['self_link'] = self
.api
.path_to('queues')
119 okay(response
, etag(resource
))