Exemple #1
0
    def __view_exec__(self, request, *args, **kwargs):
        import xdj
        from django.http import HttpResponse
        from django.shortcuts import redirect
        model = self.create_client_model(request)
        model.params = xdj.dobject(*args, **kwargs)
        if request.build_absolute_uri(self.rel_login_url).lower() != model.currentUrl.lower():
            if not self.on_authenticate(model):
                return redirect(model.appUrl + "/" + self.rel_login_url)
        if request.method == 'GET':
            return self.on_get(model)
        else:
            if not request.META.has_key("HTTP_AJAX_POST"):
                if request._get_post()!={}:
                    model.post_data.__dict__.update(
                        request._get_post()
                    )
                elif request.body and request.body != "":
                    from xdj import JSON
                    model.post_data.__dict__.update(
                        JSON.from_json(request.body)
                    )


                return self.on_post(model)
            else:
                try:
                    from xdj import JSON
                    if request.body.__len__()>0:
                        model.post_data.__dict__.update(JSON.from_json(request.body))
                    method_name = request.META["HTTP_AJAX_POST"]
                    method_items = method_name.split('.')
                    obj= self
                    for i in range(0,method_items.__len__()-1):
                        obj=getattr(obj,method_items[i])
                    method_exec = getattr(obj,method_items[method_items.__len__()-1])
                    ret = method_exec(model)
                    from util.json_request import JsonResponse
                    if type(ret) == JsonResponse:
                        return ret
                    json_data = JSON.to_json(ret)


                    return HttpResponse(json_data, content_type="application/json")
                except AttributeError as ex:
                    if not hasattr(obj,method_items[method_items.__len__()-1]):
                        raise Exception("{0} was not found in {1} or error '{2}'".format(
                            request.META["HTTP_AJAX_POST"],
                            self.on_get.im_func.func_code.co_filename,
                            ex.message
                        ))
                    else:
                        raise ex
Exemple #2
0
                    def exec_request_get(self, request, *args, **kwargs):
                        model = self.obj.owner.create_client_model(request)
                        model.params = xdj.dobject(kwargs)

                        def do_rendert(model):
                            return self.obj.owner.render_with_template(
                                model, self.obj.template)

                        self.obj.render = do_rendert
                        if request.method == "GET":
                            if hasattr(self.obj, "on_get"):
                                return self.obj.on_get(model)
                            else:
                                return self.obj.owner.render_with_template(
                                    model, self.obj.template)
                        else:
                            if request.META.has_key("HTTP_AJAX_POST"):
                                exec_method = request.META["HTTP_AJAX_POST"]
                                try:
                                    from xdj import JSON
                                    from django.http import HttpResponse
                                    from exceptions import ValueError
                                    try:
                                        model.post_data.__dict__.update(
                                            JSON.from_json(request.body))
                                    except ValueError as ex:
                                        pass
                                    method = getattr(self.obj, exec_method)
                                    ret = method(model)
                                    json_data = JSON.to_json(ret)
                                    return HttpResponse(
                                        json_data,
                                        content_type="application/json")
                                except AttributeError as ex:
                                    if not hasattr(self.obj, exec_method):
                                        import inspect
                                        code_file = inspect.getfile(
                                            self.obj.__class__)
                                        raise Exception(
                                            "{0} was not found in {1}".format(
                                                exec_method, code_file))
                                    else:
                                        raise ex
Exemple #3
0
def toJson(data):
    from xdj import JSON
    return JSON.to_json(data)
Exemple #4
0
 def from_json(self, txt):
     from xdj import JSON
     return JSON.from_json(txt)