示例#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, 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
示例#2
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, 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
示例#3
0
    def api(self, request):
        conf = self._config
        descriptor = self.namespace + '.' + self.descriptor
        
        if request.GET.has_key('format') 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)
示例#4
0
    def api(self, request):
        conf = self._config
        descriptor = self.namespace + '.' + self.descriptor

        if request.GET.has_key('format') 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)
示例#5
0
    def api(self, request, template="extdirect/django/api.js"):
        conf = self._config
        descriptor = self.namespace + '.' + self.descriptor
        
        if request.GET.has_key('format') and request.GET['format'] == 'json':
            conf['descriptor'] = descriptor
            mimetype = 'application/json'
            response = jsonDumpStripped(conf)
        else:
            mimetype = 'text/javascript'
            template_vars = {
                'namespace': self.namespace,
                'descriptor': descriptor,
                'config': jsonDumpStripped(self._config), 
            }
            response = render(request, template, template_vars)

        return HttpResponse(response, mimetype=mimetype)
示例#6
0
    def api(self, request):
        conf = self._config
        descriptor = self.namespace + "." + self.descriptor

        if request.GET.has_key("format") 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)
示例#7
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, 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
示例#8
0
    def script(self, request, template="extdirect/django/script.js"):
        """
        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 = render(request, template, {'config': config})

        return HttpResponse(js, mimetype='text/javascript')
示例#9
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')
示例#10
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")
示例#11
0
    def router(self, request):
        """
        Check if the request came from a Form POST and call
        the dispatcher for every ExtDirect request recieved.
        """
        # print "routeur"
        if request.POST.has_key("extAction"):
            # print "ok"
            print 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.raw_post_data:
            # print 11
            extdirect_request = simplejson.loads(request.raw_post_data)
            # print extdirect_request
        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)
示例#12
0
    def router(self, request):
        """
        Check if the request came from a Form POST and call
        the dispatcher for every ExtDirect request recieved.
        """

        if request.POST.has_key('extAction'):

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

            extdirect_request = simplejson.loads(request.raw_post_data)

        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)
示例#13
0
    def router(self, request):
        """
        Check if the request came from a Form POST and call
        the dispatcher for every ExtDirect request recieved.
        """

        if request.POST.has_key('extAction'):

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

            extdirect_request = simplejson.loads(request.raw_post_data)

        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)
示例#14
0
class ExtPollingProvider(ExtDirectProvider):

    type = 'polling'

    def __init__(self,
                 url,
                 event,
                 func=None,
                 login_required=False,
                 permission=None,
                 id=None):
        super(ExtPollingProvider, self).__init__(url, self.type, id)

        self.func = func
        self.event = event

        self.login_required = login_required
        self.permission = permission

    @property
    def _config(self):
        config = {'url': self.url, 'type': self.type}
        if self.id:
            config['id'] = self.id

        return config

    def register(self, func, login_required=False, permission=None):
        self.func = func
        self.login_required = login_required
        self.permission = permission

    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, 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')