~bhavesh-goyal093/mailman/Tasks-Core

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright (C) 2010-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/>.

"""REST for User Tasks."""

__all__ = [
    'AllTasks',
    'ATask',
    ]


from lazr.config import as_boolean
from mailman.config import config
from mailman.interfaces.task import ITask, ITaskManager
from mailman.rest.helpers import (
    CollectionMixin, GetterSetter, NotFound, bad_request, child, created,
    etag, no_content, not_found, okay, paginate, path_to)
from mailman.rest.validator import Validator
from operator import attrgetter
from zope.component import getUtility



class _TaskBase(CollectionMixin):
    """Shared base class for User Task representations."""

    def _resource_as_dict(self,utask):
        """See `CollectionMixin`."""
        return dict(
            task_id = utask.task_id,
            task_type = utask.task_type,
            list_id = utask.list_id,
            moderation_subject = utask.moderation_subject,
            caller_email = utask.caller_email,
            caller_name = utask.caller_name,
            self_link=path_to('tasks/{0}'.format(utask.task_id)),
            )

    def _get_collection(self, request):
        """See `CollectionMixin`."""
        return list(getUtility(ITaskManager))


class ATask(_TaskBase):
    """A Task."""

    def __init__(self, task):
        self._task = task
    
    def on_get(self, request, response):
        """Return a Single Task EndPoint."""
        task = getUtility(ITaskManager).get(self._task)
        if task is None:
            not_found(response)
        else:
            okay(response,self._resource_as_json(task))

    def on_delete(self, request, response):
        """Delete the task."""
        try:
            getUtility(ITaskManager).remove(self._task)
        except KeyError:
            not_found(response)
        else:
            no_content(response) 


class AllTasks(_TaskBase):
    """The User Tasks."""

    def on_post(self, request, response):
        """Create a New Task"""
        task_manager = getUtility(ITaskManager)
        try:
            validator = Validator(task_id=int, task_type=str, list_id=str)
            values = validator(request)
            task = task_manager.add(**values)
        except ValueError as error:
            bad_request(response, str(error))
        else:
            created(response, path_to('tasks/{0}'.format(task.task_id)))

    def on_get(self, request, response):
        """/tasks"""
        resource = self._make_collection(request)
        okay(response, etag(resource))