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

« back to all changes in this revision

Viewing changes to ivle/webapp/base/views.py

Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
Added package: ivle.webapp. This contains 'base', with some base class
    implementations for the new Plugins and Views, and 'admin', with 'user'
    (part of 'userservice') re-implemented for the new framework in a RESTful
    way.
dispatch: Added code to partly handle the new plugin system, then fall back to
    the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
22
class BaseView(object):
 
23
    """
 
24
    Abstract base class for all view objects.
 
25
    """
 
26
    def __init__(self, req, **kwargs):
 
27
        pass
 
28
    def render(self, req):
 
29
        pass
 
30
 
 
31
class RESTView(BaseView):
 
32
    """
 
33
    A view which provides a RESTful interface. The content type is
 
34
    unspecified (see JSONRESTView for a specific content type).
 
35
    """
 
36
    content_type = "application/octet-stream"
 
37
 
 
38
    def __init__(self, req, *args, **kwargs):
 
39
        pass
 
40
 
 
41
    def render(self, req):
 
42
        if req.method == 'GET':
 
43
            outstr = self.GET(req)
 
44
        # XXX PATCH hack
 
45
        if req.method == 'PUT':
 
46
            outstr = self.PATCH(req, req.read())
 
47
        req.content_type = self.content_type
 
48
        req.write(outstr)
 
49
 
 
50
class JSONRESTView(RESTView):
 
51
    """
 
52
    A special case of RESTView which deals entirely in JSON.
 
53
    """
 
54
    content_type = "application/json"
 
55
 
 
56
    def render(self, req):
 
57
        if req.method == 'GET':
 
58
            outjson = self.GET(req)
 
59
        # XXX PATCH hack
 
60
        if req.method == 'PUT':
 
61
            outjson = self.PATCH(req, cjson.decode(req.read()))
 
62
        req.content_type = self.content_type
 
63
        req.write(cjson.encode(outjson))
 
64
        req.write("\n")