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

« back to all changes in this revision

Viewing changes to ivle/rpc/decorators.py

Update ivle.rpc.decorators to use new exceptions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
The first argument to any method wrapped by these needs to be a request.
4
4
'''
5
5
 
 
6
from ivle.webapp.errors import MethodNotAllowed, Unauthorized
 
7
 
6
8
class require_method(object):
7
9
    '''Require that the request has been made with the specified HTTP method.
8
10
    '''
12
14
    def __call__(self, func):
13
15
        def method_or_die(req, *args, **kwargs):
14
16
           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
                raise MethodNotAllowed(allowed=[self.method])
17
18
           func(req, *args, **kwargs)
18
19
        return method_or_die
19
20
 
25
26
    def __call__(self, func):
26
27
        def cap_or_die(req, *args, **kwargs):
27
28
           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.")
 
29
                raise Unauthorized()
30
30
           func(req, *args, **kwargs)
31
31
        return cap_or_die
32
32