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

« back to all changes in this revision

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

  • Committer: William Grant
  • Date: 2010-07-20 04:52:31 UTC
  • mto: This revision was merged to the branch mainline in revision 1826.
  • Revision ID: grantw@unimelb.edu.au-20100720045231-8ia3uk8nao8zdq1i
Replace cjson with json, or simplejson if json is not available (Python <2.6)

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import os
24
24
import urlparse
25
25
 
26
 
import cjson
 
26
try:
 
27
    import json
 
28
except ImportError:
 
29
    import simplejson as json
 
30
 
27
31
import genshi.template
28
32
 
29
33
from ivle.webapp.base.views import BaseView
89
93
              req.headers_in['X-IVLE-Patch-Semantics'].lower() == 'yes'):
90
94
            self.authorize_method(req, self.PATCH)
91
95
            try:
92
 
                input = cjson.decode(req.read())
93
 
            except cjson.DecodeError:
 
96
                input = json.loads(req.read())
 
97
            except ValueError:
94
98
                raise BadRequest('Invalid JSON data')
95
99
            outjson = self.PATCH(req, input)
96
100
        elif req.method == 'PUT':
97
101
            self.authorize_method(req, self.PUT)
98
102
            try:
99
 
                input = cjson.decode(req.read())
100
 
            except cjson.DecodeError:
 
103
                input = json.loads(req.read())
 
104
            except ValueError:
101
105
                raise BadRequest('Invalid JSON data')
102
106
            outjson = self.PUT(req, input)
103
107
        # POST implies named operation.
113
117
    #This is a separate function to allow additional data to be passed through
114
118
    def write_json(self, req, outjson):
115
119
        if outjson is not None:
116
 
            req.write(cjson.encode(outjson))
 
120
            req.write(json.dumps(outjson))
117
121
            req.write("\n")
118
122
 
119
123
    def _named_operation(self, req, opargs, readonly=False):
185
189
    # This renders the template and adds it to the json
186
190
    def write_json(self, req, outjson):
187
191
        outjson["html"] = self.render_fragment()
188
 
        req.write(cjson.encode(outjson))
 
192
        req.write(json.dumps(outjson))
189
193
        req.write("\n")
190
194
 
191
195
class _named_operation(object):