Ejemplo n.º 1
0
    def dispatch(self, request, name=None):

        if not name:
            raise Http404

        # Check if the function is callable
        if dajaxice_functions.is_callable(name, request.method):

            function = dajaxice_functions.get(name)
            data = getattr(request, function.method).get('argv', '')

            # Clean the argv
            if data != 'undefined':
                try:
                    data = safe_dict(json.loads(data))
                except Exception:
                    data = {}
            else:
                data = {}

            # Call the function. If something goes wrong, handle the Exception
            try:
                response = function.call(request, **data)
            except Exception:
                if settings.DEBUG:
                    raise
                response = dajaxice_config.DAJAXICE_EXCEPTION
            if StrictVersion(django.get_version()) >= StrictVersion('1.7'):
                return HttpResponse(response, content_type="application/x-json")
            else:
                return HttpResponse(response, mimetype="application/x-json")
        else:
            raise FunctionNotCallableError(name)
Ejemplo n.º 2
0
    def dispatch(self, request, name=None):

        if not name:
            raise Http404

        # Check if the function is callable
        if dajaxice_functions.is_callable(name, request.method):

            function = dajaxice_functions.get(name)
            data = getattr(request, function.method).get('argv', '')

            # Clean the argv
            if data != 'undefined':
                try:
                    data = safe_dict(json.loads(data))
                except Exception:
                    data = {}
            else:
                data = {}

            # Call the function. If something goes wrong, handle the Exception
            try:
                response = function.call(request, **data)
            except Exception:
                if settings.DEBUG:
                    raise
                response = dajaxice_config.DAJAXICE_EXCEPTION
            if StrictVersion(django.get_version()) >= StrictVersion('1.7'):
                return HttpResponse(response,
                                    content_type="application/x-json")
            else:
                return HttpResponse(response, mimetype="application/x-json")
        else:
            raise FunctionNotCallableError(name)
Ejemplo n.º 3
0
    def dispatch(self, request, name=None):

        if not name:
            raise Http404

        # Check if the function is callable
        if dajaxice_functions.is_callable(name, request.method):

            function = dajaxice_functions.get(name)
            data = getattr(request, function.method).get('argv', '')

            # Clean the argv
            if data != 'undefined':
                try:
                    data = safe_dict(simplejson.loads(data))
                except Exception:
                    data = {}
            else:
                data = {}

            # Call the function. If something goes wrong, handle the Exception
            try:
                response = function.call(request, **data)
            except Exception:
                if settings.DEBUG:
                    raise
                response = 'Error'

            return HttpResponse(response, mimetype="application/x-json")
        else:
            raise FunctionNotCallableError(name)
Ejemplo n.º 4
0
    def dispatch(self, request, name=None):

        if not name:
            raise Http404

        # Check if the function is callable
        if dajaxice_functions.is_callable(name, request.method):

            function = dajaxice_functions.get(name)
            data = getattr(request, function.method).get('argv', '')

            # Clean the argv
            if data != 'undefined':
                try:
                    data = safe_dict(json.loads(data))
                except Exception:
                    data = {}
            else:
                data = {}

            response_data = function.call(request, **data)
            if isinstance(response_data, HttpResponseBase):
                return response_data
            if isinstance(response_data,
                          dict) and not response_data.get('success'):
                log.warning('Dajaxice success=false.\nResponse data: %s',
                            response_data,
                            extra={'request': request})
            if not isinstance(response_data, six.string_types):
                response_data = json.dumps(response_data)
            return HttpResponse(response_data,
                                content_type="application/x-json")
        else:
            raise FunctionNotCallableError(name)
Ejemplo n.º 5
0
    def dispatch(self, request, name=None):

        if not name:
            raise Http404

        # Check if the function is callable
        if dajaxice_functions.is_callable(name, request.method):

            function = dajaxice_functions.get(name)
            data = getattr(request, function.method).get('argv', '')

            # Clean the argv
            if data != 'undefined':
                try:
                    data = safe_dict(json.loads(data))
                except Exception:
                    data = {}
            else:
                data = {}

            response_data = function.call(request, **data)
            if isinstance(response_data, HttpResponseBase):
                return response_data
            if isinstance(response_data, dict) and not response_data.get('success'):
                log.warning('Dajaxice success=false.\nResponse data: %s', response_data, extra={'request': request})
            if not isinstance(response_data, six.string_types):
                response_data = json.dumps(response_data)
            return HttpResponse(response_data, content_type="application/x-json")
        else:
            raise FunctionNotCallableError(name)
Ejemplo n.º 6
0
    def dispatch(self, request, name=None):

        if not name:
            raise Http404

        # Check if the function is callable
        if dajaxice_functions.is_callable(name, request.method):

            func = dajaxice_functions.get(name)
            data = getattr(request, func.method).get('argv', 'undefined')

            # Clean the argv
            if data != 'undefined':
                try:
                    data = json.loads(data)
                except json.decoder.JSONDecodeError:
                    log.exception('name=%s, data=%s', name, data)
                    data = {}
            else:
                data = {}

            # Call the function. If something goes wrong, handle the Exception
            try:
                response = func.call(request, **data)
            except:
                log.exception('name=%s, data=%s', name, data)
                if settings.DEBUG:
                    raise FunctionNotCallableError
                response = dajaxice_config.DAJAXICE_EXCEPTION

            return HttpResponse(
                response, content_type="application/x-json; charset=utf-8")
        else:
            if settings.DEBUG:
                raise FunctionNotCallableError
            log.error('Function %s is not callable. method=%s', name,
                      request.method)
            return HttpResponse(
                dajaxice_config.DAJAXICE_NOT_CALLABLE_RESPONSE,
                content_type="application/json; charset=utf-8")
Ejemplo n.º 7
0
    def dispatch(self, request, name=None):

        if not name:
            raise Http404

        # Check if the function is callable
        if dajaxice_functions.is_callable(name, request.method):

            function = dajaxice_functions.get(name)
            data = getattr(request, function.method).get('argv', '')

            # Clean the argv
            if data != 'undefined':
                try:
                    data = safe_dict(json.loads(data))
                except Exception:
                    data = {}
            else:
                data = {}

            # Call the function. If something goes wrong, handle the Exception
            try:
                response = function.call(request, **data)
            except:
                log.exception('name=%s, data=%s', name, data)
                if settings.DEBUG:
                    raise FunctionNotCallableError
                response = dajaxice_config.DAJAXICE_EXCEPTION
            if django.get_version() >= '1.7':
                return HttpResponse(
                    response, content_type="application/x-json; charset=utf-8")
            else:
                return HttpResponse(
                    response, mimetype="application/x-json; charset=utf-8")
        else:
            raise FunctionNotCallableError
            log.error('Function %s is not callable. method=%s', name,
                      request.method)
            return HttpResponse(dajaxice_config.DAJAXICE_NOT_CALLABLE_RESPONSE,
                                content_type="application/json; charset=utf-8")
Ejemplo n.º 8
0
    def dispatch(self, request, *args, **kwargs):
        name = kwargs.get('name')

        if not name and len(args) == 1:
            name = args[0]

        if not name:
            raise Http404

        # Check if the function is callable
        if dajaxice_functions.is_callable(name, request.method):
            function = dajaxice_functions.get(name)
            data = getattr(request, function.method).get('argv', '')

            # Clean the argv
            if data != 'undefined':
                try:
                    data = json.loads(data, encoding='utf-8')
                except Exception as ex:
                    log.error(ex)
                    data = {}
            else:
                data = {}

            # Call the function. If something goes wrong, handle the Exception
            try:
                response = function.call(request, **data)
            except Exception as ex:
                log.error(ex)

                if settings.DEBUG:
                    raise

                response = dajaxice_config.DAJAXICE_EXCEPTION

            return HttpResponse(response, content_type="application/x-json")
        else:
            raise FunctionNotCallableError(name)
Ejemplo n.º 9
0
    def dispatch(self, request, name=None):

        if not name:
            raise Http404

        # Check if the function is callable
        if dajaxice_functions.is_callable(name, request.method):

            function = dajaxice_functions.get(name)
            data = getattr(request, function.method).get('argv', '')

            # Clean the argv
            if data != 'undefined':
                try:
                    data = safe_dict(simplejson.loads(data))
                except Exception:
                    data = {}
            else:
                data = {}

            # Call the function. If something goes wrong, handle the Exception
            status = 200
            try:
                response = function.call(request, **data)
            except Exception:
                if settings.DEBUG:
                    raise
                response = dajaxice_config.DAJAXICE_EXCEPTION
                status = 500

            if isinstance(response, basestring):
                return HttpResponse(response,
                                    status=status,
                                    mimetype="application/json")
            else:
                return response
        else:
            raise FunctionNotCallableError(name)
 def _is_callable(self):
     """
     Return if the request function was registered.
     """
     return dajaxice_functions.is_callable(self.full_name)
Ejemplo n.º 11
0
	def _is_callable(self):
		"""
		Return if the request function was registered.
		"""
		return dajaxice_functions.is_callable(self.full_name)