18
18
# Author: Matt Giuca, Will Grant
20
22
class BaseView(object):
22
24
Abstract base class for all view objects.
25
subpath_allowed = False
26
offsite_posts_allowed = False
28
def __init__(self, req, context, subpath=None):
30
self.context = context
31
if self.subpath_allowed:
32
self.subpath = subpath
34
def render(self, req):
35
raise NotImplementedError()
37
def get_permissions(self, user, config):
38
return self.context.get_permissions(user, config)
40
def authorize(self, req):
41
self.perms = self.get_permissions(req.user, req.config)
43
return self.permission is None or self.permission in self.perms
26
def __init__(self, req, **kwargs):
28
def render(self, req):
31
class RESTView(BaseView):
33
A view which provides a RESTful interface. The content type is
34
unspecified (see JSONRESTView for a specific content type).
36
content_type = "application/octet-stream"
38
def __init__(self, req, *args, **kwargs):
41
def render(self, req):
42
if req.method == 'GET':
43
outstr = self.GET(req)
45
if req.method == 'PUT':
46
outstr = self.PATCH(req, req.read())
47
req.content_type = self.content_type
50
class JSONRESTView(RESTView):
52
A special case of RESTView which deals entirely in JSON.
54
content_type = "application/json"
56
def render(self, req):
57
if req.method == 'GET':
58
outjson = self.GET(req)
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))