Exemple #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)
Exemple #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)
    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)
Exemple #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)
Exemple #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)
Exemple #6
0
 def test_search_request(self):
     # mutually exclusive choices
     choices = [(0, 'Everywhere'), (1, 'Klewel'), (2, 'Scopia'), (3, 'TED')]
     headers = {'content-type': 'application/json'}
     for choice in choices:
         post_data = {'searchable': choice[0], 'keywords': 'cool'}
         request = RequestFactory().get('/')
         # another option to call the method would be: from manage.ajax import call_inevent_search
         # then call call_inevent_search[0] is added because call_inevent_search has been modified by dajaxice
         response = dajaxice_functions.get('inevent.call_inevent_search').call(request, post_data, '1')
         #  pp = pprint.PrettyPrinter(depth=6)
         self.assertTrue(response)  # to be changed
Exemple #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):

            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")
Exemple #8
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")
Exemple #9
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)
    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)