Exemplo n.º 1
0
    def router(self, request):
        response = {}

        if self.login_required:
            if not request.user.is_authenticated():
                response['type'] = 'event'
                response['data'] = 'You must be authenticated to run this method.'
                response['name'] = self.event
                return HttpResponse(jsonDumpStripped(response), mimetype='application/json')

        if self.permission:
            if not request.user.has_perm(self.permission):
                response['type'] = 'result'
                response['data'] = 'You need `%s` permission to run this method' % self.permission
                response['name'] = self.event
                return HttpResponse(jsonDumpStripped(response), mimetype='application/json')

        try:
            if self.func:
                response['data'] = self.func(request)
                response['name'] = self.event
                response['type'] = 'event'
            else:
                raise RuntimeError("The server provider didn't register a function to run yet")

        except Exception as e:
            if settings.DEBUG:
                etype, evalue, etb = sys.exc_info()
                response['type'] = 'exception'
                response['message'] = traceback.format_exception_only(etype, evalue)[0]
                response['where'] = traceback.extract_tb(etb)[-1]
            else:
                raise e

        return HttpResponse(jsonDumpStripped(response), mimetype='application/json')
Exemplo n.º 2
0
    def api(self, request):
        conf = self._config
        descriptor = self.namespace + '.' + self.descriptor

        if 'format' in request.GET and request.GET['format'] == 'json':
            conf['descriptor'] = descriptor
            mimetype = 'application/json'
            response = jsonDumpStripped(conf)
        else:
            response = """
Ext.ns('%s');
%s = %s
""" % (self.namespace, descriptor, jsonDumpStripped(self._config))
            mimetype = 'text/javascript'

        return HttpResponse(response, mimetype=mimetype)
Exemplo n.º 3
0
    def script(self, request):
        """
        Return a HttpResponse with the javascript code needed
        to register the DirectProvider in Ext.

        You will have to add an urlpattern to your urls.py
        pointing to this method. Something like::

            remote_provider = ExtDirectProvider('/some-url/', ...)
            urlpatterns = patterns(
                ...,
                (r'^myprovider.js/$', remote_provider.script),
                ...
            )
        """
        config = jsonDumpStripped(self._config)
        js = SCRIPT % config

        return HttpResponse(js, mimetype='text/javascript')
Exemplo n.º 4
0
    def router(self, request):
        """
        Check if the request came from a Form POST and call
        the dispatcher for every ExtDirect request recieved.
        """

        if 'extAction' in request.POST:

            extdirect_request = dict(
                action=request.POST['extAction'],
                method=request.POST['extMethod'],
                tid=request.POST['extTID'],
                type=request.POST['extType'],
                isForm=True
            )

        elif request.body:
            extdirect_request = json.loads(request.body)

        else:
            return HttpResponseBadRequest('Invalid request')

        if isinstance(extdirect_request, list):
            #call in batch
            response = []
            for single_request in extdirect_request:
                response.append(self.dispatcher(request, single_request))

        elif isinstance(extdirect_request, dict):
           #single call
            response = self.dispatcher(request, extdirect_request)

        if request.POST.get('extUpload', False):
            #http://www.extjs.com/deploy/dev/docs/?class=Ext.form.BasicForm#Ext.form.BasicForm-fileUpload
            mimetype = 'text/html'
        else:
            mimetype = 'application/json'

        return HttpResponse(jsonDumpStripped(response), mimetype=mimetype)