1
# Copyright (C) 2010-2015 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/>.
18
"""REST for User Tasks."""
26
from lazr.config import as_boolean
27
from mailman.config import config
28
from mailman.interfaces.task import ITask, ITaskManager
29
from mailman.rest.helpers import (
30
CollectionMixin, GetterSetter, NotFound, bad_request, child, created,
31
etag, no_content, not_found, okay, paginate, path_to)
32
from mailman.rest.validator import Validator
33
from operator import attrgetter
34
from zope.component import getUtility
38
class _TaskBase(CollectionMixin):
39
"""Shared base class for User Task representations."""
41
def _resource_as_dict(self,utask):
42
"""See `CollectionMixin`."""
44
task_id = utask.task_id,
45
task_type = utask.task_type,
46
list_id = utask.list_id,
47
moderation_subject = utask.moderation_subject,
48
caller_email = utask.caller_email,
49
caller_name = utask.caller_name,
50
self_link=path_to('tasks/{0}'.format(utask.task_id)),
53
def _get_collection(self, request):
54
"""See `CollectionMixin`."""
55
return list(getUtility(ITaskManager))
58
class ATask(_TaskBase):
61
def __init__(self, task):
64
def on_get(self, request, response):
65
"""Return a Single Task EndPoint."""
66
task = getUtility(ITaskManager).get(self._task)
70
okay(response,self._resource_as_json(task))
72
def on_delete(self, request, response):
73
"""Delete the task."""
75
getUtility(ITaskManager).remove(self._task)
82
class AllTasks(_TaskBase):
85
def on_post(self, request, response):
86
"""Create a New Task"""
87
task_manager = getUtility(ITaskManager)
89
validator = Validator(task_id=int, task_type=str, list_id=str)
90
values = validator(request)
91
task = task_manager.add(**values)
92
except ValueError as error:
93
bad_request(response, str(error))
95
created(response, path_to('tasks/{0}'.format(task.task_id)))
97
def on_get(self, request, response):
99
resource = self._make_collection(request)
100
okay(response, etag(resource))