18
18
# Author: Matt Giuca, Will Grant
25
import genshi.template
29
from ivle.webapp.errors import BadRequest, MethodNotAllowed
31
20
class BaseView(object):
33
22
Abstract base class for all view objects.
35
def __init__(self, req, **kwargs):
37
def render(self, req):
40
class RESTView(BaseView):
42
A view which provides a RESTful interface. The content type is
43
unspecified (see JSONRESTView for a specific content type).
45
content_type = "application/octet-stream"
47
def __init__(self, req, *args, **kwargs):
50
def render(self, req):
51
if req.method == 'GET':
52
outstr = self.GET(req)
54
if req.method == 'PUT':
55
outstr = self.PATCH(req, req.read())
56
req.content_type = self.content_type
59
class JSONRESTView(RESTView):
61
A special case of RESTView which deals entirely in JSON.
63
content_type = "application/json"
65
_allowed_methods = property(
66
lambda self: [m for m in ('GET', 'PUT', 'PATCH')
67
if hasattr(self, m)] + ['POST'])
69
def render(self, req):
70
if req.method not in self._allowed_methods:
71
raise MethodNotAllowed(allowed=self._allowed_methods)
73
if req.method == 'GET':
74
outjson = self.GET(req)
75
# Since PATCH isn't yet an official HTTP method, we allow users to
76
# turn a PUT into a PATCH by supplying a special header.
77
elif req.method == 'PATCH' or (req.method == 'PUT' and
78
'X-IVLE-Patch-Semantics' in req.headers_in and
79
req.headers_in['X-IVLE-Patch-Semantics'].lower() == 'yes'):
80
outjson = self.PATCH(req, cjson.decode(req.read()))
81
elif req.method == 'PUT':
82
outjson = self.PUT(req, cjson.decode(req.read()))
83
# POST implies named operation.
84
elif req.method == 'POST':
85
# TODO: Check Content-Type and implement multipart/form-data.
86
opargs = dict(cgi.parse_qsl(req.read()))
88
opname = opargs['ivle.op']
91
raise BadRequest('No named operation specified.')
94
op = getattr(self, opname)
95
except AttributeError:
96
raise BadRequest('Invalid named operation.')
98
if not hasattr(op, '_rest_api_callable') or \
99
not op._rest_api_callable:
100
raise BadRequest('Invalid named operation.')
102
# Find any missing arguments, except for the first one (self).
103
argspec = inspect.getargspec(op)
104
missing = frozenset(argspec[0][1:]) - frozenset(opargs.keys())
106
raise BadRequest('Missing arguments: ' + ', '.join(missing))
108
outjson = op(**opargs)
110
raise AssertionError('Unknown method somehow got through.')
112
req.content_type = self.content_type
113
if outjson is not None:
114
req.write(cjson.encode(outjson))
117
def named_operation(meth):
118
'''Declare a function to be accessible to HTTP users via the REST API.
120
meth._rest_api_callable = True
123
class XHTMLView(BaseView):
125
A view which provides a base class for views which need to return XHTML
126
It is expected that apps which use this view will be written using Genshi
129
def __init__(self, req, **kwargs):
131
setattr(self, key, kwargs[key])
133
def render(self, req):
134
req.content_type = 'text/html' # TODO: Detect application/xhtml+xml
135
ctx = genshi.template.Context()
136
self.populate(req, ctx)
137
self.populate_headings(req, ctx)
139
ctx['app_styles'] = req.styles
140
ctx['scripts'] = req.scripts
141
ctx['scripts_init'] = req.scripts_init
142
app_template = os.path.join(os.path.dirname(
143
inspect.getmodule(self).__file__), self.app_template)
144
req.write_html_head_foot = False
145
loader = genshi.template.TemplateLoader(".", auto_reload=True)
146
tmpl = loader.load(app_template)
147
app = tmpl.generate(ctx)
148
ctx['app_template'] = app
149
tmpl = loader.load(os.path.join(os.path.dirname(__file__),
150
'ivle-headings.html'))
151
req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
153
def populate_headings(self, req, ctx):
154
ctx['favicon'] = None
155
ctx['root_dir'] = ivle.conf.root_dir
156
ctx['public_host'] = ivle.conf.public_host
157
ctx['write_javascript_settings'] = req.write_javascript_settings
159
ctx['login'] = req.user.login
160
ctx['logged_in'] = True
161
ctx['nick'] = req.user.nick
164
ctx['publicmode'] = req.publicmode
165
ctx['apps_in_tabs'] = []
166
for urlname in ivle.conf.apps.apps_in_tabs:
168
app = ivle.conf.apps.app_url[urlname]
169
new_app['this_app'] = urlname == self.appname
171
new_app['has_icon'] = True
172
icon_dir = ivle.conf.apps.app_icon_dir
173
icon_url = ivle.util.make_path(os.path.join(icon_dir, app.icon))
174
new_app['icon_url'] = icon_url
175
if new_app['this_app']:
176
ctx['favicon'] = icon_url
178
new_app['has_icon'] = False
179
new_app['path'] = ivle.util.make_path(urlname)
180
new_app['desc'] = app.desc
181
new_app['name'] = app.name
182
ctx['apps_in_tabs'].append(new_app)
25
subpath_allowed = False
27
def __init__(self, req, context, subpath=None):
28
self.context = context
29
if self.subpath_allowed:
30
self.subpath = subpath
32
def render(self, req):
33
raise NotImplementedError()
35
def get_permissions(self, user):
36
return self.context.get_permissions(user)
38
def authorize(self, req):
39
self.perms = self.get_permissions(req.user)
41
return self.permission is None or self.permission in self.perms