~azzar1/unity/add-show-desktop-key

1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# Author: Matt Giuca, Will Grant
19
20
import cjson
21
1099.1.6 by William Grant
ivle.webapp.errors: Add, with a base HTTPError and BadRequest, FileNotFound
22
from ivle.webapp.errors import BadRequest
23
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
24
class BaseView(object):
25
    """
26
    Abstract base class for all view objects.
27
    """
28
    def __init__(self, req, **kwargs):
29
        pass
30
    def render(self, req):
31
        pass
32
33
class RESTView(BaseView):
34
    """
35
    A view which provides a RESTful interface. The content type is
36
    unspecified (see JSONRESTView for a specific content type).
37
    """
38
    content_type = "application/octet-stream"
39
40
    def __init__(self, req, *args, **kwargs):
41
        pass
42
43
    def render(self, req):
44
        if req.method == 'GET':
45
            outstr = self.GET(req)
46
        # XXX PATCH hack
47
        if req.method == 'PUT':
48
            outstr = self.PATCH(req, req.read())
49
        req.content_type = self.content_type
50
        req.write(outstr)
51
52
class JSONRESTView(RESTView):
53
    """
54
    A special case of RESTView which deals entirely in JSON.
55
    """
56
    content_type = "application/json"
57
58
    def render(self, req):
59
        if req.method == 'GET':
60
            outjson = self.GET(req)
1099.1.6 by William Grant
ivle.webapp.errors: Add, with a base HTTPError and BadRequest, FileNotFound
61
        elif req.method == 'PATCH' or (req.method == 'PUT' and
62
              'X-IVLE-Patch-Semantics' in req.headers_in and
63
              req.headers_in['X-IVLE-Patch-Semantics'].lower() == 'yes'):
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
64
            outjson = self.PATCH(req, cjson.decode(req.read()))
1099.1.6 by William Grant
ivle.webapp.errors: Add, with a base HTTPError and BadRequest, FileNotFound
65
        elif req.method == 'PUT':
66
            outjson = self.PUT(req, cjson.decode(req.read()))
67
        else:
68
            raise BadRequest
1099.1.1 by Matt Giuca
Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
69
        req.content_type = self.content_type
1099.1.3 by William Grant
ivle.webapp.base.views.JSONRestView: Don't render anything if None is
70
        if outjson is not None:
71
            req.write(cjson.encode(outjson))
72
            req.write("\n")