1
'''Decorators useful for actions in the IVLE AJAX API.
3
The first argument to any method wrapped by these needs to be a request.
6
class require_method(object):
7
'''Require that the request has been made with the specified HTTP method.
9
def __init__(self, method):
12
def __call__(self, func):
13
def method_or_die(req, *args, **kwargs):
14
if req.method != self.method:
15
req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
16
"Only %s requests can be made to this action." % self.method)
17
func(req, *args, **kwargs)
20
class require_cap(object):
21
'''Require that the logged in user has the specified capability.'''
22
def __init__(self, cap):
25
def __call__(self, func):
26
def cap_or_die(req, *args, **kwargs):
27
if not req.user or not req.user.hasCap(self.cap):
28
req.throw_error(req.HTTP_FORBIDDEN,
29
"You do not have permission to use this action.")
30
func(req, *args, **kwargs)