~stephen-xemacs/mailman/sprint-2012-overview

« back to all changes in this revision

Viewing changes to src/mailman/rest/moderation.py

  • Committer: Barry Warsaw
  • Date: 2012-01-30 15:37:16 UTC
  • Revision ID: barry@list.org-20120130153716-s9qx07i6i0rltyax
 * Held messages can now be moderated through the REST API.  Mailing list
   resources now accept a `held` path component.  GETing this returns all held
   messages for the mailing list.  POSTing to a specific request id under this
   url can dispose of the message using `Action` enums.
 * `IRequests` interface is removed.  Now just use adaptation from
   `IListRequests` directly (which takes an `IMailingList` object).
 * `handle_message()` now allows for `Action.hold` which is synonymous with
   `Action.defer` (since the message is already being held).
 * `IListRequests.get_request()` now takes an optional `request_type`
   argument to narrow the search for the given request.

- also, print_function is now a standard __future__ import.  The template has
  been updated, but add this to modules as you edit them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2012 by the Free Software Foundation, Inc.
 
2
#
 
3
# This file is part of GNU Mailman.
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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/>.
 
17
 
 
18
"""REST API for Message moderation."""
 
19
 
 
20
from __future__ import absolute_import, print_function, unicode_literals
 
21
 
 
22
__metaclass__ = type
 
23
__all__ = [
 
24
    'HeldMessage',
 
25
    'HeldMessages',
 
26
    ]
 
27
 
 
28
 
 
29
from restish import http, resource
 
30
from zope.component import getUtility
 
31
 
 
32
from mailman.app.moderator import handle_message
 
33
from mailman.interfaces.action import Action
 
34
from mailman.interfaces.messages import IMessageStore
 
35
from mailman.interfaces.requests import IListRequests, RequestType
 
36
from mailman.rest.helpers import CollectionMixin, etag, no_content
 
37
from mailman.rest.validator import Validator, enum_validator
 
38
 
 
39
 
 
40
 
 
41
class HeldMessage(resource.Resource, CollectionMixin):
 
42
    """Resource for moderating a held message."""
 
43
 
 
44
    def __init__(self, mlist, request_id):
 
45
        self._mlist = mlist
 
46
        self._request_id = request_id
 
47
 
 
48
    @resource.GET()
 
49
    def details(self, request):
 
50
        requests = IListRequests(self._mlist)
 
51
        try:
 
52
            request_id = int(self._request_id)
 
53
        except ValueError:
 
54
            return http.bad_request()
 
55
        results = requests.get_request(request_id, RequestType.held_message)
 
56
        if results is None:
 
57
            return http.not_found()
 
58
        key, data = results
 
59
        msg = getUtility(IMessageStore).get_message_by_id(key)
 
60
        resource = dict(
 
61
            key=key,
 
62
            data=data,
 
63
            msg=msg.as_string(),
 
64
            id=request_id,
 
65
            )
 
66
        return http.ok([], etag(resource))
 
67
 
 
68
    @resource.POST()
 
69
    def moderate(self, request):
 
70
        try:
 
71
            validator = Validator(action=enum_validator(Action))
 
72
            arguments = validator(request)
 
73
        except ValueError as error:
 
74
            return http.bad_request([], str(error))
 
75
        requests = IListRequests(self._mlist)
 
76
        try:
 
77
            request_id = int(self._request_id)
 
78
        except ValueError:
 
79
            return http.bad_request()
 
80
        results = requests.get_request(request_id, RequestType.held_message)
 
81
        if results is None:
 
82
            return http.not_found()
 
83
        handle_message(self._mlist, request_id, **arguments)
 
84
        return no_content()
 
85
 
 
86
 
 
87
 
 
88
class HeldMessages(resource.Resource, CollectionMixin):
 
89
    """Resource for messages held for moderation."""
 
90
 
 
91
    def __init__(self, mlist):
 
92
        self._mlist = mlist
 
93
 
 
94
    def _resource_as_dict(self, req):
 
95
        """See `CollectionMixin`."""
 
96
        key, data = self._requests.get_request(req.id)
 
97
        return dict(
 
98
            key=key,
 
99
            data=data,
 
100
            id=req.id,
 
101
            )
 
102
 
 
103
    def _get_collection(self, request):
 
104
        requests = IListRequests(self._mlist)
 
105
        self._requests = requests
 
106
        return list(requests.of_type(RequestType.held_message))
 
107
 
 
108
    @resource.GET()
 
109
    def requests(self, request):
 
110
        """/lists/listname/held"""
 
111
        resource = self._make_collection(request)
 
112
        return http.ok([], etag(resource))
 
113
 
 
114
    @resource.child('{id}')
 
115
    def message(self, request, segments, **kw):
 
116
        return HeldMessage(self._mlist, kw['id'])