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

« back to all changes in this revision

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

ivle.webapp.base.views#JSONRESTView: Check that methods are available before
    trying to execute them. Also raise a legal MethodNotAllowed with an
    'allowed' attribute indicating permitted methods
ivle.webapp.errors: Alter constructor to take mandatory 'allowed' argument.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
    """
63
63
    content_type = "application/json"
64
64
 
 
65
    _allowed_methods = property(
 
66
        lambda self: [m for m in ('GET', 'PUT', 'PATCH')
 
67
                      if hasattr(self, m)] + ['POST'])
 
68
 
65
69
    def render(self, req):
 
70
        if req.method not in self._allowed_methods:
 
71
            raise MethodNotAllowed(allowed=self._allowed_methods)
 
72
 
66
73
        if req.method == 'GET':
67
74
            outjson = self.GET(req)
68
75
        # Since PATCH isn't yet an official HTTP method, we allow users to
100
107
 
101
108
            outjson = op(**opargs)
102
109
        else:
103
 
            raise MethodNotAllowed()
 
110
            raise AssertionError('Unknown method somehow got through.')
104
111
 
105
112
        req.content_type = self.content_type
106
113
        if outjson is not None: