Example #1
0
    def post(self):
        req = json.loads(self.request.body.decode())
        # open("/tmp/json_rpc.log","a").write(self.request.body.decode()+"\n###############################################################\n")
        db = database.get_connection()
        if db:
            db.begin()
        try:
            clear_cache()
            method = req["method"]
            params = req["params"]
            if method == "execute":
                model = params[0]
                method = params[1]
                if method.startswith("_"):
                    raise Exception("Invalid method")
                args = params[2]
                if len(params) >= 4:
                    opts = params[3] or {}
                else:
                    opts = {}
                user_id = access.get_active_user()
                rpc_log.info(
                    "EXECUTE db=%s model=%s method=%s user=%s" % (database.get_active_db(), model, method, user_id)
                )
                m = get_model(model)
                f = getattr(m, method)
                ctx = {"request_handler": self, "request": self.request}
                ctx.update(self.get_cookies())
                opts.setdefault("context", {}).update(ctx)
                with timeout(seconds=300):  # XXX: can make this faster? (less signal sys handler overhead)
                    t0 = time.time()
                    res = f(*args, **opts)
                    t1 = time.time()
                    dt = (t1 - t0) * 1000
                    rpc_log.info("<<< %d ms" % dt)
                resp = {"result": res, "error": None, "id": req["id"]}
            else:
                raise Exception("Invalid method: %s" % method)
            if db:
                db.commit()
        except Exception as e:
            try:
                msg = translate(str(e))
            except:
                print("WARNING: Failed to translate error message")
                msg = str(e)
            rpc_log.error(msg)
            if db:
                db.rollback()
            rpc_log.error(traceback.format_exc())
            err = {"message": msg}
            error_fields = getattr(e, "error_fields", None)
            if error_fields:
                err["error_fields"] = error_fields
            resp = {"result": None, "error": err, "id": req["id"]}
        access.clear_active_user()
        try:
            data = json_dumps(resp)
            self.add_header("Access-Control-Allow-Origin", "*")
            self.write(data)
        except:
            print("JSONRPC ERROR: invalid response")
            from pprint import pprint

            pprint(resp)
            traceback.print_exc()
Example #2
0
 def post(self):
     req = json.loads(urllib.parse.unquote(self.get_argument("request")))
     db = database.get_connection()
     if db:
         db.begin()
     try:
         clear_cache()
         method = req["method"]
         params = req["params"]
         if method == "execute":
             model = params[0]
             method = params[1]
             if method.startswith("_"):
                 raise Exception("Invalid method")
             args = params[2]
             if len(params) >= 4:
                 opts = params[3] or {}
             else:
                 opts = {}
             user_id = access.get_active_user()
             rpc_log.info("EXECUTE db=%s model=%s method=%s user=%s" %
                          (database.get_active_db(), model, method, user_id))
             m = get_model(model)
             f = getattr(m, method)
             ctx = {
                 "request_handler": self,
                 "request": self.request,
             }
             ctx.update(self.get_cookies())
             opts.setdefault("context", {}).update(ctx)
             with timeout(seconds=300):  # XXX: can make this faster? (less signal sys handler overhead)
                 t0 = time.time()
                 res = f(*args, **opts)
                 t1 = time.time()
                 dt = (t1 - t0) * 1000
                 rpc_log.info("<<< %d ms" % dt)
             resp = {
                 "result": res,
                 "error": None,
                 "id": req["id"],
             }
         else:
             raise Exception("Invalid method: %s" % method)
         db = database.get_connection()
         if db:
             db.commit()
     except Exception as e:
         try:
             msg = translate(str(e))
         except:
             print("WARNING: Failed to translate error message")
             msg = str(e)
         rpc_log.error(msg)
         db = database.get_connection()
         if db:
             db.rollback()
         rpc_log.error(traceback.format_exc())
         err = {
             "message": msg,
         }
         error_fields = getattr(e, "error_fields", None)
         if error_fields:
             err["error_fields"] = error_fields
         resp = {
             "result": None,
             "error": err,
             "id": req["id"],
         }
     access.clear_active_user()
     try:
         data = urllib.parse.quote(json_dumps(resp))
     except:
         print("JSONRPC ERROR: invalid response")
         from pprint import pprint
         pprint(resp)
     print("json_rpc_iframe result", data)
     self.write(data)
Example #3
0
 def post(self):
     req = json.loads(self.request.body.decode())
     # open("/tmp/json_rpc.log","a").write(self.request.body.decode()+"\n###############################################################\n")
     db = database.get_connection()
     if db:
         db.begin()
     try:
         clear_cache()
         method = req["method"]
         params = req["params"]
         if method == "execute":
             model = params[0]
             method = params[1]
             if method.startswith("_"):
                 raise Exception("Invalid method")
             args = params[2]
             if len(params) >= 4:
                 opts = params[3] or {}
             else:
                 opts = {}
             if len(params) >= 5:
                 cookies = params[4] or {}
             else:
                 cookies = {}
             if "locale" in cookies:
                 set_active_locale(cookies["locale"])
             if "user_id" in cookies:
                 user_id = int(cookies["user_id"])
                 token = cookies.get("token")
                 dbname = database.get_active_db()
                 schema = database.get_active_schema()
                 if check_token(dbname, user_id, token, schema=schema):
                     access.set_active_user(user_id)
             user_id = access.get_active_user()
             rpc_log.info(
                 "EXECUTE db=%s model=%s method=%s user=%s" %
                 (database.get_active_db(), model, method, user_id))
             m = get_model(model)
             f = getattr(m, method)
             ctx = {
                 "request_handler": self,
                 "request": self.request,
             }
             ctx.update(self.get_cookies())
             ctx.update(cookies)
             ctx.update(opts.get("context", {}))
             opts["context"] = ctx
             with timeout(
                     seconds=900
             ):  # XXX: can make this faster? (less signal sys handler overhead)
                 t0 = time.time()
                 res = f(*args, **opts)
                 t1 = time.time()
                 dt = (t1 - t0) * 1000
                 rpc_log.info("<<< %d ms" % dt)
             resp = {
                 "result": res,
                 "error": None,
                 "id": req["id"],
             }
         else:
             raise Exception("Invalid method: %s" % method)
         if db:
             db.commit()
     except Exception as e:
         try:
             msg = translate(str(e))
         except:
             print("WARNING: Failed to translate error message")
             msg = str(e)
         rpc_log.error(msg)
         if db:
             db.rollback()
         rpc_log.error(traceback.format_exc())
         err = {
             "message": msg,
         }
         error_fields = getattr(e, "error_fields", None)
         if error_fields:
             err["error_fields"] = error_fields
         resp = {
             "result": None,
             "error": err,
             "id": req["id"],
         }
     access.clear_active_user()
     try:
         data = json_dumps(resp)
         self.add_header("Access-Control-Allow-Origin", "*")
         self.write(data)
     except:
         print("JSONRPC ERROR: invalid response")
         from pprint import pprint
         pprint(resp)
         traceback.print_exc()
Example #4
0
 def get(self):
     db = database.get_connection()
     if db:
         db.begin()
     try:
         clear_cache()
         print(self)
         method = self.get_argument("method")
         model = self.get_argument("model")
         if method.startswith("_"):
             raise Exception("Invalid method")
         args = self.get_argument("args",None)
         if args:
             args=json.loads(args)
         else:
             args=[]
         opts = self.get_argument("opts",None)
         if opts:
             opts=json.loads(opts)
         else:
             opts={}
         cookies = self.get_argument("cookies",None)
         if cookies:
             cookies = json.loads(cookies)
         else:
             cookies = {}
         if "locale" in cookies:
             set_active_locale(cookies["locale"])
         user_id = access.get_active_user()
         rpc_log.info("EXECUTE db=%s model=%s method=%s user=%s" %
                      (database.get_active_db(), model, method, user_id))
         m = get_model(model)
         f = getattr(m, method)
         ctx = {
             "request_handler": self,
             "request": self.request,
         }
         ctx.update(self.get_cookies())
         opts.setdefault("context", {}).update(ctx)
         with timeout(seconds=900):  # XXX: can make this faster? (less signal sys handler overhead)
             t0 = time.time()
             res = f(*args, **opts)
             t1 = time.time()
             dt = (t1 - t0) * 1000
             rpc_log.info("<<< %d ms" % dt)
         resp = {
             "result": res,
             "error": None,
             "id": self.get_argument("id"),
         }
         if db:
             db.commit()
     except Exception as e:
         try:
             msg = translate(str(e))
         except:
             print("WARNING: Failed to translate error message")
             msg = str(e)
         rpc_log.error(msg)
         if db:
             db.rollback()
         rpc_log.error(traceback.format_exc())
         err = {
             "message": msg,
         }
         error_fields = getattr(e, "error_fields", None)
         if error_fields:
             err["error_fields"] = error_fields
         resp = {
             "result": None,
             "error": err,
             "id": self.get_argument("id",None),
         }
     access.clear_active_user()
     try:
         data = json_dumps(resp)
         self.add_header("Access-Control-Allow-Origin","*")
         self.add_header("Last-Modified",datetime.utcnow())
         self.write(data)
     except:
         print("JSONRPC ERROR: invalid response")
         from pprint import pprint
         pprint(resp)
         traceback.print_exc()
Example #5
0
 def get(self):
     db = database.get_connection()
     if db:
         db.begin()
     try:
         clear_cache()
         print(self)
         method = self.get_argument("method")
         model = self.get_argument("model")
         if method.startswith("_"):
             raise Exception("Invalid method")
         args = self.get_argument("args", None)
         if args:
             args = json.loads(args)
         else:
             args = []
         opts = self.get_argument("opts", None)
         if opts:
             opts = json.loads(opts)
         else:
             opts = {}
         cookies = self.get_argument("cookies", None)
         if cookies:
             cookies = json.loads(cookies)
         else:
             cookies = {}
         if "locale" in cookies:
             set_active_locale(cookies["locale"])
         user_id = access.get_active_user()
         rpc_log.info("EXECUTE db=%s model=%s method=%s user=%s" %
                      (database.get_active_db(), model, method, user_id))
         m = get_model(model)
         f = getattr(m, method)
         ctx = {
             "request_handler": self,
             "request": self.request,
         }
         ctx.update(self.get_cookies())
         opts.setdefault("context", {}).update(ctx)
         with timeout(
                 seconds=900
         ):  # XXX: can make this faster? (less signal sys handler overhead)
             t0 = time.time()
             res = f(*args, **opts)
             t1 = time.time()
             dt = (t1 - t0) * 1000
             rpc_log.info("<<< %d ms" % dt)
         resp = {
             "result": res,
             "error": None,
             "id": self.get_argument("id"),
         }
         if db:
             db.commit()
     except Exception as e:
         try:
             msg = translate(str(e))
         except:
             print("WARNING: Failed to translate error message")
             msg = str(e)
         rpc_log.error(msg)
         if db:
             db.rollback()
         rpc_log.error(traceback.format_exc())
         err = {
             "message": msg,
         }
         error_fields = getattr(e, "error_fields", None)
         if error_fields:
             err["error_fields"] = error_fields
         resp = {
             "result": None,
             "error": err,
             "id": self.get_argument("id", None),
         }
     access.clear_active_user()
     try:
         data = json_dumps(resp)
         self.add_header("Access-Control-Allow-Origin", "*")
         self.add_header("Last-Modified", datetime.utcnow())
         self.write(data)
     except:
         print("JSONRPC ERROR: invalid response")
         from pprint import pprint
         pprint(resp)
         traceback.print_exc()