1 # Copyright (C) 2015-2023 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 <https://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 (
32 from mailman
.rest
.validator
import Validator
33 from public
import public
34 from zope
.component
import getUtility
37 class _QueuesBase(CollectionMixin
):
38 """Shared base class for queues."""
40 def _resource_as_dict(self
, name
):
41 """See `CollectionMixin`."""
42 switchboard
= config
.switchboards
[name
]
43 files
= switchboard
.files
45 name
=switchboard
.name
,
46 directory
=switchboard
.queue_directory
,
49 self_link
=self
.api
.path_to('queues/{}'.format(name
)),
52 def _get_collection(self
, request
):
53 """See `CollectionMixin`."""
54 return sorted(config
.switchboards
)
58 class AQueue(_QueuesBase
):
61 def __init__(self
, name
):
64 def on_get(self
, request
, response
):
65 """Return a single queue resource."""
66 if self
._name
not in config
.switchboards
:
69 okay(response
, self
._resource
_as
_json
(self
._name
))
71 def on_post(self
, request
, response
):
72 """Inject a message into the queue."""
74 validator
= Validator(list_id
=str,
76 values
= validator(request
)
77 except ValueError as error
:
78 bad_request(response
, str(error
))
80 list_id
= values
['list_id']
81 mlist
= getUtility(IListManager
).get_by_list_id(list_id
)
83 bad_request(response
, 'No such list: {}'.format(list_id
))
86 filebase
= inject_text(
87 mlist
, values
['text'], switchboard
=self
._name
)
88 except Exception as error
:
89 bad_request(response
, str(error
))
92 location
= self
.api
.path_to(
93 'queues/{}/{}'.format(self
._name
, filebase
))
94 created(response
, location
)
99 def __init__(self
, name
, filebase
):
101 self
._filebase
= filebase
103 def on_delete(self
, request
, response
):
104 """Delete the queue file."""
105 switchboard
= config
.switchboards
.get(self
._name
)
106 if switchboard
is None:
107 not_found(response
, 'No such queue: {}'.format(self
._name
))
110 switchboard
.dequeue(self
._filebase
)
111 except FileNotFoundError
:
113 'No such queue file: {}'.format(self
._filebase
))
119 class AllQueues(_QueuesBase
):
122 def on_get(self
, request
, response
):
124 resource
= self
._make
_collection
(request
)
125 resource
['self_link'] = self
.api
.path_to('queues')
126 okay(response
, etag(resource
))