~bhavesh-goyal093/mailman/Tasks-Core

« back to all changes in this revision

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

  • Committer: Bhavesh Goyal
  • Date: 2015-05-24 20:09:18 UTC
  • Revision ID: bhavesh.goyal093@gmail.com-20150524200918-0r249av8f9irjo60
Add untracked tasks files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010-2015 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 for User Tasks."""
 
19
 
 
20
__all__ = [
 
21
    'AllTasks',
 
22
    'ATask',
 
23
    ]
 
24
 
 
25
 
 
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
 
35
 
 
36
 
 
37
 
 
38
class _TaskBase(CollectionMixin):
 
39
    """Shared base class for User Task representations."""
 
40
 
 
41
    def _resource_as_dict(self,utask):
 
42
        """See `CollectionMixin`."""
 
43
        return dict(
 
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)),
 
51
            )
 
52
 
 
53
    def _get_collection(self, request):
 
54
        """See `CollectionMixin`."""
 
55
        return list(getUtility(ITaskManager))
 
56
 
 
57
 
 
58
class ATask(_TaskBase):
 
59
    """A Task."""
 
60
 
 
61
    def __init__(self, task):
 
62
        self._task = task
 
63
    
 
64
    def on_get(self, request, response):
 
65
        """Return a Single Task EndPoint."""
 
66
        task = getUtility(ITaskManager).get(self._task)
 
67
        if task is None:
 
68
            not_found(response)
 
69
        else:
 
70
            okay(response,self._resource_as_json(task))
 
71
 
 
72
    def on_delete(self, request, response):
 
73
        """Delete the task."""
 
74
        try:
 
75
            getUtility(ITaskManager).remove(self._task)
 
76
        except KeyError:
 
77
            not_found(response)
 
78
        else:
 
79
            no_content(response) 
 
80
 
 
81
 
 
82
class AllTasks(_TaskBase):
 
83
    """The User Tasks."""
 
84
 
 
85
    def on_post(self, request, response):
 
86
        """Create a New Task"""
 
87
        task_manager = getUtility(ITaskManager)
 
88
        try:
 
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))
 
94
        else:
 
95
            created(response, path_to('tasks/{0}'.format(task.task_id)))
 
96
 
 
97
    def on_get(self, request, response):
 
98
        """/tasks"""
 
99
        resource = self._make_collection(request)
 
100
        okay(response, etag(resource))
 
101