- ext_requests = json.loads(request.raw_post_data)
- if type(ext_requests) is dict:
- ext_requests['upload'] = False
- response = self.handle_ext_request(request, ext_requests, app_label)
- else:
- responses = []
- for ext_request in ext_requests:
- ext_request['upload'] = False
- responses.append(self.handle_ext_request(request, ext_request, app_label))
- response = responses
-
- if submitted_form:
- if ext_request['upload'] is True:
- return HttpResponse(('<html><body><textarea>%s</textarea></body></html>' % json.dumps(response)))
- return HttpResponse(json.dumps(response), content_type=('application/json; charset=%s' % settings.DEFAULT_CHARSET))
-
- def handle_ext_request(self, request, ext_request, app_label=None):
- try:
- plugin = None
-
- if app_label is not None:
- try:
- plugin = self.model_registry[app_label][ext_request['action']]
- except KeyError:
- raise NotImplementedError('A model named \'%s\' has not been registered' % ext_request['action'])
- else:
- try:
- plugin = self.plugin_registry[ext_request['action']]
- except KeyError:
- raise NotImplementedError('Gilbert does not provide a class named \'%s\'' % ext_request['action'])
-
- method = plugin.get_method(ext_request['method'])
-
- if method is None or (method.restricted and not self.has_permission(request)):
- raise NotImplementedError('The method named \'%s\' is not available' % method.name)
-
- return {'type': 'rpc', 'tid': ext_request['tid'], 'action': ext_request['action'], 'method': ext_request['method'], 'result': method(request, *(ext_request['data'] or []))}
- except:
- exc_type, exc_value, exc_traceback = sys.exc_info()
- return {'type': 'exception', 'tid': ext_request['tid'], 'message': ('%s: %s' % (exc_type, exc_value)), 'where': format_tb(exc_traceback)[0]}