Esempio n. 1
0
 def prepare(self):
     print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
     print("prepare")
     super(BaseController, self).prepare()
     website_id = self.request.headers.get("X-Website-ID")
     if website_id:
         website_id = int(website_id)
     else:
         res = get_model("website").search([["state", "=", "active"]])
         if not res:
             raise Exception("No website found")
         website_id = res[0]
     self.website_id = website_id
     website = get_model("website").browse(website_id)
     template.set_active_theme(website.theme_id.id)
     browse_ctx = {
         "website_id":
         website.id,
         "theme_id":
         website.theme_id.id,
         "sale_channel_id":
         website.sale_channel_id.id,
         "pricelist_id":
         website.sale_channel_id.pricelist_id.id
         if website.sale_channel_id else None,
     }
     lang = self.get_argument("set_lang", None)
     if lang:
         set_active_locale(lang)
         self.set_cookie("locale", lang)
     ctx = {}
     ctx["website"] = website
     ctx["database"] = get_active_db()
     ctx["locale"] = get_active_locale()
     ctx["ga_script"] = website.ga_script
     ctx["linklists"] = get_model("cms.linklist").search_browse([])
     ctx["product_groups"] = get_model("product.group").search_browse(
         [], context=browse_ctx)
     ctx["countries"] = get_model("country").search_browse([])  # XXX
     cart_id = self.get_cookie("cart_id", None)
     if cart_id:
         cart_id = int(cart_id)
         res = get_model("ecom.cart").search([["id", "=", cart_id]])
         if res:
             ctx["cart"] = get_model("ecom.cart").browse(cart_id)
         else:  # handle invalid cart_id cookie
             self.clear_cookie("cart_id")
     user_id = self.get_cookie("user_id", None)
     if user_id:
         user_id = int(user_id)
         user = get_model("base.user").browse(user_id)
         ctx["customer"] = user.contact_id
     offset = self.get_argument("offset", None)
     if offset:
         ctx["offset"] = int(offset)
     ctx["url"] = self.request.uri
     self.context = ctx
Esempio n. 2
0
 def prepare(self):
     print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
     print("prepare")
     super(BaseController,self).prepare()
     website_id=self.request.headers.get("X-Website-ID")
     if website_id:
         website_id=int(website_id)
     else:
         res=get_model("website").search([["state","=","active"]])
         if not res:
             raise Exception("No website found")
         website_id=res[0]
     self.website_id=website_id
     website=get_model("website").browse(website_id)
     template.set_active_theme(website.theme_id.id)
     browse_ctx={
         "website_id": website.id,
         "theme_id": website.theme_id.id,
         "sale_channel_id": website.sale_channel_id.id,
         "pricelist_id": website.sale_channel_id.pricelist_id.id if website.sale_channel_id else None,
     }
     lang=self.get_argument("set_lang",None)
     if lang:
         set_active_locale(lang)
         self.set_cookie("locale",lang)
     ctx={}
     ctx["website"]=website
     ctx["database"]=get_active_db()
     ctx["locale"]=get_active_locale()
     ctx["ga_script"]=website.ga_script
     ctx["linklists"]=get_model("cms.linklist").search_browse([])
     ctx["product_groups"]=get_model("product.group").search_browse([],context=browse_ctx)
     ctx["countries"]=get_model("country").search_browse([]) # XXX
     cart_id=self.get_cookie("cart_id",None)
     if cart_id:
         cart_id=int(cart_id)
         res=get_model("ecom.cart").search([["id","=",cart_id]])
         if res:
             ctx["cart"]=get_model("ecom.cart").browse(cart_id)
         else: # handle invalid cart_id cookie
             self.clear_cookie("cart_id")
     user_id=self.get_cookie("user_id",None)
     if user_id:
         user_id=int(user_id)
         user=get_model("base.user").browse(user_id)
         ctx["customer"]=user.contact_id
     offset=self.get_argument("offset",None)
     if offset:
         ctx["offset"]=int(offset)
     ctx["url"]=self.request.uri
     self.context=ctx
Esempio n. 3
0
 def get(self):
     #url="https://v5.inspection.in.th/inspectionreport/checkqr?id=1541844&password=f45681f1"
     current_local=get_active_locale()
     set_active_user(1) #admin
     set_active_locale('th_TH')
     db=get_connection() # prevent to error get transaction
     try:
         db.begin()
         id=self.get_argument("id")
         if not id:
             self.write("Missing ID")
             return
         password=self.get_argument("password")
         if not password:
             self.write("Missing Password")
             return
         ctx={
             'obj': None,
         }
         #res=db.query("select * from inspection where number=%s and password=%s",id,password)
         dom=[
             ['number','=', id],
             ['password','=', password]
         ]
         res=get_model("inspection").search_read(dom)
         if res:
             #t0=res[-1]['date']
             #t1=t0.split(" ")
             #res[-1]['date']='%s %s'%(sale_utils.date2thai(t1[0], format='%(d)s %(Tm)s %(BY)s'), t1[1])
             #res[-1]['date_register']=sale_utils.date2thai(res[-1]['date_register'], format='%(d)s %(Tm)s %(BY)s')
             ctx['obj']=res[-1]
         else:
             ctx['nothing']=True
         data=ctx['obj']
         html=render("checkqr",context=ctx, data=data)
         self.write(html)
     except Exception as e:
         self.write("ERROR : %s"%str(e))
     finally:
         if db:
             db.commit()
     set_active_locale(current_local)
Esempio n. 4
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()
Esempio n. 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()
Esempio n. 6
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()
Esempio n. 7
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()