~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: Add support for argument defaults in
    named operations, and raise a BadRequest if we have extra arguments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
                raise BadRequest('Invalid named operation.')
101
101
 
102
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())
105
 
            if missing:
106
 
                raise BadRequest('Missing arguments: ' + ', '.join(missing))
 
103
            (args, vaargs, varkw, defaults) = inspect.getargspec(op)
 
104
            args = args[1:]
 
105
 
 
106
            # To find missing arguments, we eliminate the provided arguments
 
107
            # from the set of remaining function signature arguments. If the
 
108
            # remaining signature arguments are in the args[-len(defaults):],
 
109
            # we are OK.
 
110
            unspec = set(args) - set(opargs.keys())
 
111
            if unspec and not defaults:
 
112
                raise BadRequest('Missing arguments: ' + ','.join(unspec))
 
113
 
 
114
            unspec = [k for k in unspec if k not in args[-len(defaults):]]
 
115
 
 
116
            if unspec:
 
117
                raise BadRequest('Missing arguments: ' + ','.join(unspec))
 
118
 
 
119
            # We have extra arguments if the are no match args in the function
 
120
            # signature, AND there is no **.
 
121
            extra = set(opargs.keys()) - set(args)
 
122
            if extra and not varkw:
 
123
                raise BadRequest('Extra arguments: ' + ', '.join(extra))
107
124
 
108
125
            outjson = op(**opargs)
109
126
        else: