Example #1
0
 def post(self):
     dbname=self.get_argument("dbname")
     uuid=self.get_argument("uuid")
     submit=self.get_argument("submit")
     db=database.connect(dbname)
     clear_cache()
     try:
         res=get_model("sale.quot").search([["uuid","=",uuid]])
         if not res:
             raise Exception("Invalid UUID")
         quot_id=res[0]
         quot=get_model("sale.quot").browse(quot_id)
         if submit=="accept":
             quot.write({"state":"won"})
         elif submit=="reject":
             quot.write({"state":"lost"})
         elif submit=="send":
             vals={"related_id":"sale.quot,%s"%quot_id,"body": self.get_argument("message"),"from_id":1}
             get_model("message").create(vals)
         self.redirect("/view_quot?dbname=%s&uuid=%s"%(dbname,uuid))
         db.commit()
     except Exception as e:
         db.rollback()
         import traceback
         traceback.print_exc()
Example #2
0
 def post(self):
     dbname = self.get_argument("dbname")
     uuid = self.get_argument("uuid")
     submit = self.get_argument("submit")
     db = database.connect(dbname)
     clear_cache()
     try:
         res = get_model("sale.quot").search([["uuid", "=", uuid]])
         if not res:
             raise Exception("Invalid UUID")
         quot_id = res[0]
         quot = get_model("sale.quot").browse(quot_id)
         if submit == "accept":
             quot.write({"state": "won"})
         elif submit == "reject":
             quot.write({"state": "lost"})
         elif submit == "send":
             vals = {
                 "related_id": "sale.quot,%s" % quot_id,
                 "body": self.get_argument("message"),
                 "from_id": 1
             }
             get_model("message").create(vals)
         self.redirect("/view_quot?dbname=%s&uuid=%s" % (dbname, uuid))
         db.commit()
     except Exception as e:
         db.rollback()
         import traceback
         traceback.print_exc()
Example #3
0
def run_job(dbname, job):
    print("run_job dbname=%s pid=%s job='%s'" %
          (dbname, os.getpid(), job["name"]))
    database.connections.clear()
    set_active_user(1)
    database.set_active_db(dbname)
    db = database.get_connection()
    db.begin()
    clear_cache()
    m = get_model(job["model"])
    f = getattr(m, job["method"])
    if job["args"]:
        args = json.loads(job["args"])
    else:
        args = []
    db.execute("UPDATE cron_job SET state='running' WHERE id=%s", job["id"])
    db.commit()
    print("starting job '%s'" % job["name"])
    try:
        with utils.timeout(seconds=job["timeout"]):
            f(*args)
        db.commit()
        print("job '%s' success" % job["name"])
    except Exception as e:
        print("WARNING: job '%s' failed: %s" % (job["name"], e))
        db.rollback()
        t = time.strftime("%Y-%m-%d %H:%M:%S")
        msg = traceback.format_exc()
        db.execute(
            "UPDATE cron_job SET last_error_time=%s,error_message=%s WHERE id=%s",
            t, msg, job["id"])
        db.commit()
    t1 = time.strftime("%Y-%m-%s %H:%M:%S")
    if job["interval_num"]:
        if job["interval_type"] == "second":
            dt = timedelta(seconds=job["interval_num"])
        elif job["interval_type"] == "minute":
            dt = timedelta(minutes=job["interval_num"])
        elif job["interval_type"] == "hour":
            dt = timedelta(hours=job["interval_num"])
        elif job["interval_type"] == "day":
            dt = timedelta(days=job["interval_num"])
        else:
            raise Exception("Missing interval unit")
        next_date = datetime.strptime(job["date"], "%Y-%m-%d %H:%M:%S")
        now = datetime.now()
        while next_date <= now:  # TODO: make this faster
            next_date += dt
        if next_date < _check_times[dbname]:
            _check_times[dbname] = next_date
        db.execute("UPDATE cron_job SET date=%s,state='waiting' WHERE id=%s",
                   next_date.strftime("%Y-%m-%d %H:%M:%S"), job["id"])
    else:
        db.execute("UPDATE cron_job SET state='done' WHERE id=%s", job["id"])
    db.commit()
Example #4
0
def run_job(dbname, job):
    print("run_job dbname=%s pid=%s job='%s'"%(dbname, os.getpid(), job["name"]))
    database.connections.clear()
    set_active_user(1)
    database.set_active_db(dbname)
    db = database.get_connection()
    db.begin()
    clear_cache()
    m = get_model(job["model"])
    f = getattr(m, job["method"])
    if job["args"]:
        args = json.loads(job["args"])
    else:
        args = []
    db.execute("UPDATE cron_job SET state='running' WHERE id=%s", job["id"])
    db.commit()
    print("starting job '%s'"%job["name"])
    try:
            with utils.timeout(seconds=job["timeout"]):
                f(*args)
            db.commit()
            print("job '%s' success" % job["name"])
    except Exception as e:
        print("WARNING: job '%s' failed: %s"%(job["name"],e))
        db.rollback()
        t=time.strftime("%Y-%m-%d %H:%M:%S")
        msg=traceback.format_exc()
        db.execute("UPDATE cron_job SET last_error_time=%s,error_message=%s WHERE id=%s", t, msg, job["id"])
        db.commit()
    t1 = time.strftime("%Y-%m-%s %H:%M:%S")
    if job["interval_num"]:
        if job["interval_type"] == "second":
            dt = timedelta(seconds=job["interval_num"])
        elif job["interval_type"] == "minute":
            dt = timedelta(minutes=job["interval_num"])
        elif job["interval_type"] == "hour":
            dt = timedelta(hours=job["interval_num"])
        elif job["interval_type"] == "day":
            dt = timedelta(days=job["interval_num"])
        else:
            raise Exception("Missing interval unit")
        next_date = datetime.strptime(job["date"], "%Y-%m-%d %H:%M:%S")
        now = datetime.now()
        while next_date <= now:  # TODO: make this faster
            next_date += dt
        if next_date < _check_times[dbname]:
            _check_times[dbname] = next_date
        db.execute("UPDATE cron_job SET date=%s,state='waiting' WHERE id=%s",
                   next_date.strftime("%Y-%m-%d %H:%M:%S"), job["id"])
    else:
        db.execute("UPDATE cron_job SET state='done' WHERE id=%s", job["id"])
    db.commit()
Example #5
0
 def get(self):
     dbname=self.get_argument("dbname")
     uuid=self.get_argument("uuid")
     db=database.connect(dbname)
     clear_cache()
     try:
         res=get_model("sale.quot").search([["uuid","=",uuid]])
         if not res:
             raise Exception("Invalid UUID")
         quot_id=res[0]
         quot=get_model("sale.quot").browse(quot_id)
         comp=get_model("company").browse(1)
         data={
             "logo": comp.logo,
             "state": quot.state,
             "partner_name": quot.partner_id.name,
             "number": quot.number,
             "exp_date": quot.exp_date,
             "user_name": quot.user_id.name,
             "comments": [],
         }
         if quot.documents:
             doc=quot.documents[0]
             fname=doc.file
         else:
             fname=None
         data["file"]=fname
         for msg in quot.comments:
             vals={
                 "date": msg.date,
                 "body": msg.body,
             }
             data["comments"].append(vals)
         data["num_comments"]=len(data["comments"])
         tmpl=get_template("view_quot")
         html=tmpl.render(data)
         self.write(html)
         db.commit()
     except Exception as e:
         db.rollback()
         import traceback
         traceback.print_exc()
Example #6
0
 def get(self):
     dbname = self.get_argument("dbname")
     uuid = self.get_argument("uuid")
     db = database.connect(dbname)
     clear_cache()
     try:
         res = get_model("sale.quot").search([["uuid", "=", uuid]])
         if not res:
             raise Exception("Invalid UUID")
         quot_id = res[0]
         quot = get_model("sale.quot").browse(quot_id)
         comp = get_model("company").browse(1)
         data = {
             "logo": comp.logo,
             "state": quot.state,
             "partner_name": quot.partner_id.name,
             "number": quot.number,
             "exp_date": quot.exp_date,
             "user_name": quot.user_id.name,
             "comments": [],
         }
         if quot.documents:
             doc = quot.documents[0]
             fname = doc.file
         else:
             fname = None
         data["file"] = fname
         for msg in quot.comments:
             vals = {
                 "date": msg.date,
                 "body": msg.body,
             }
             data["comments"].append(vals)
         data["num_comments"] = len(data["comments"])
         tmpl = get_template("view_quot")
         html = tmpl.render(data)
         self.write(html)
         db.commit()
     except Exception as e:
         db.rollback()
         import traceback
         traceback.print_exc()
Example #7
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 #8
0
 def get(self):  # TODO: cleanup
     db = get_connection()
     if db:
         db.begin()
     try:
         clear_cache()
         ctx = {
             "request": self.request,
             "request_handler": self,
             "dbname": get_active_db(),
         }
         data = self.get_cookies()
         if data:
             ctx.update(data)
         action_vals = parse_args(self)
         ctx.update(action_vals)
         name = action_vals.get("name")
         if name:
             action_ctx = action_vals
             action = get_action(name, action_ctx)
             for k, v in action.items():
                 if k not in action_vals:
                     action_vals[k] = v
         if "context" in action_vals:
             ctx.update(action_vals["context"])
         action_vals["context"] = ctx
         self.clear_flash()
         type = action_vals.get("type", "view")
         if type == "report":  # XXX: deprecated
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             refer_id = action_vals.get("refer_id")
             format = action_vals.get("format", "pdf")
             if action_vals.get("ids") and isinstance(
                     action_vals["ids"], str):
                 ids = json.loads(action_vals["ids"])
                 action_vals["ids"] = ids
             m = get_model(model)
             f = getattr(m, method, None)
             if action_vals.get("multi_page"):
                 datas = []
                 if "ids" in action_vals:
                     ids = action_vals["ids"]
                 else:
                     ids = [int(action_vals["refer_id"])]
                 for obj_id in ids:
                     ctx = action_vals.copy()
                     ctx["refer_id"] = obj_id
                     data = f(context=ctx)
                     datas.append(data)
             else:
                 data = f(context=action_vals)
             template = action_vals.get("template")
             if not template and action_vals.get("template_method"):
                 f = getattr(m, action_vals["template_method"])
                 template = f(ids, context=action_vals)
             if action_vals.get("multi_page"):
                 out = get_report_jasper_multi_page(template,
                                                    datas,
                                                    format=format)
             elif "pages" in data:
                 datas = []
                 for obj in data["pages"]:
                     d = data.copy()
                     d.update(obj)
                     datas.append(d)
                 out = get_report_jasper_multi_page(template,
                                                    datas,
                                                    format=format)
             else:
                 out = get_report_jasper(template, data, format=format)
             db = get_connection()
             if db:
                 db.commit()
             if format == "pdf":
                 fname = template + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".pdf"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/pdf")
             elif format == "xls":
                 fname = template + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".xls"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/vnd.ms-excel")
             elif format == "ods":
                 fname = template + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".ods"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header(
                     "Content-Type",
                     "application/vnd.oasis.opendocument.spreadsheet")
             else:
                 raise Exception("Invalid format: %s" % format)
             self.write(out)
         elif type == "report_jasper":  # new jasper
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             refer_id = action_vals.get("refer_id")
             if refer_id:
                 refer_id = int(refer_id)
                 ids = [refer_id]
             else:
                 ids = json.loads(action_vals.get("ids"))
             format = action_vals.get("format", "pdf")
             m = get_model(model)
             f = getattr(m, method, None)
             data = f(ids, context=action_vals)
             if not data:
                 raise Exception("Missing report data")
             template = action_vals.get("template")
             if not template and action_vals.get("template_method"):
                 f = getattr(m, action_vals["template_method"])
                 template = f(ids, context=action_vals)
             if action_vals.get("multi_page"):
                 datas = []
                 for obj in data["objs"]:
                     d = data.copy()
                     d["obj"] = obj
                     datas.append(d)
                 out = get_report_jasper_multi_page(template,
                                                    datas,
                                                    format=format)
             else:
                 out = get_report_jasper(template, data, format=format)
             db = get_connection()
             if db:
                 db.commit()
             if format == "pdf":
                 fname = template + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".pdf"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/pdf")
             elif format == "xls":
                 fname = template + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".xls"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/vnd.ms-excel")
             elif format == "ods":
                 fname = template + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".ods"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header(
                     "Content-Type",
                     "application/vnd.oasis.opendocument.spreadsheet")
             else:
                 raise Exception("Invalid format: %s" % format)
             self.write(out)
         elif type == "report_html":  # XXX: deprecated
             model = action_vals["model"]
             method = action_vals["method"]
             m = get_model(model)
             f = getattr(m, method, None)
             ctx2 = ctx.copy()  # XXX
             ctx2.update(action_vals)
             data = f(context=ctx2)
             tmpl_name = action_vals.get("template")
             tmpl = netforce.template.get_template(tmpl_name)
             ctx2["data"] = data
             html = tmpl.render({"context": ctx2})
             out = netforce.report.html2pdf(html)
             db = get_connection()
             if db:
                 db.commit()
             fname = tmpl_name + "-" + time.strftime(
                 "%Y-%m-%dT%H:%M:%S") + ".pdf"
             self.set_header("Content-Disposition",
                             "attachment; filename=%s" % fname)
             self.set_header("Content-Type", "application/pdf")
             self.write(out)
         elif type == "report_xls":
             model = action_vals["model"]
             method = action_vals["method"]
             m = get_model(model)
             f = getattr(m, method, None)
             ctx2 = ctx.copy()  # XXX
             ctx2.update(action_vals)
             data = f(context=ctx2)
             tmpl_name = action_vals.get("template")
             if not tmpl_name and action_vals.get("template_method"):
                 f = getattr(m, action_vals["template_method"])
                 tmpl_name = f(context=action_vals)
             out = report_render_xls(tmpl_name, data)
             db = get_connection()
             if db:
                 db.commit()
             fname = tmpl_name + "-" + time.strftime(
                 "%Y-%m-%dT%H:%M:%S") + ".xlsx"
             self.set_header("Content-Disposition",
                             "attachment; filename=%s" % fname)
             self.set_header(
                 "Content-Type",
                 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
             )
             self.write(out)
         elif type == "report_doc":  # XXX: deprecated
             model = action_vals["model"]
             method = action_vals["method"]
             convert = action_vals.get("convert")
             m = get_model(model)
             f = getattr(m, method, None)
             ctx2 = ctx.copy()  # XXX
             ctx2.update(action_vals)
             data = f(context=ctx2)
             tmpl_name = action_vals.get("template")
             if not tmpl_name and action_vals.get("template_method"):
                 f = getattr(m, action_vals["template_method"])
                 tmpl_name = f(context=action_vals)
             out = report_render_doc(tmpl_name, data)
             db = get_connection()
             if db:
                 db.commit()
             if convert == "pdf":
                 fname = tmpl_name + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".pdf"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/pdf")
                 out = convert_to_pdf(out, "docx")
             else:
                 fname = tmpl_name + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".docx"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header(
                     "Content-Type",
                     "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                 )
             self.write(out)
         elif type == "report_odt":  # XXX: deprecated
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             convert = action_vals.get("convert")
             m = get_model(model)
             f = getattr(m, method, None)
             if action_vals.get("ids") and isinstance(
                     action_vals["ids"], str):
                 ids = json.loads(action_vals["ids"])
                 action_vals["ids"] = ids
                 print("ids", ids)
             if action_vals.get("ids") and convert == "pdf":  # FIXME
                 outs = []
                 for id in ids:
                     ctx = action_vals.copy()
                     ctx["refer_id"] = str(id)
                     data = f(context=ctx)
                     tmpl_name = action_vals.get("template")
                     if not tmpl_name and action_vals.get(
                             "template_method"):
                         f = getattr(m, action_vals["template_method"])
                         tmpl_name = f(context=action_vals)
                     out_odt = report_render_odt(tmpl_name, data)
                     out_pdf = convert_to_pdf(out_odt, "odt")  # XXX
                     outs.append(out_pdf)
                 out = merge_pdf(outs)
             else:
                 ctx2 = ctx.copy()  # XXX
                 ctx2.update(action_vals)
                 data = f(context=ctx2)
                 tmpl_name = action_vals.get("template")
                 if not tmpl_name and action_vals.get("template_method"):
                     f = getattr(m, action_vals["template_method"])
                     tmpl_name = f(context=action_vals)
                 out = report_render_odt(tmpl_name, data)
                 if convert == "pdf":
                     out = convert_to_pdf(out, "odt")
             db = get_connection()
             if db:
                 db.commit()
             if convert == "pdf":
                 fname = tmpl_name + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".pdf"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/pdf")
             else:
                 fname = tmpl_name + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".odt"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header("Content-Type",
                                 "application/vnd.oasis.opendocument.text")
             self.write(out)
         elif type == "report_odt2":  # XXX: use this instead of report_odt later
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             convert = action_vals.get("convert")
             refer_id = action_vals.get("refer_id")
             m = get_model(model)
             f = getattr(m, method, None)
             if "ids" in action_vals:
                 ids = json.loads(action_vals["ids"])
             elif "refer_id" in action_vals:
                 ids = [int(action_vals["refer_id"])]
             else:
                 raise Exception("Missing report ids")
             print("ids", ids)
             ctx = action_vals.copy()
             data = f(ids, context=ctx)
             tmpl_name = action_vals.get("template")
             if not tmpl_name and action_vals.get("template_method"):
                 f = getattr(m, action_vals["template_method"])
                 tmpl_name = f(ids, context=action_vals)
             out = report_render_odt(tmpl_name, data)
             db = get_connection()
             if db:
                 db.commit()
             if convert == "pdf":
                 out = convert_to_pdf(out, "odt")
                 fname = tmpl_name + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".pdf"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/pdf")
             else:
                 fname = tmpl_name + "-" + time.strftime(
                     "%Y-%m-%dT%H:%M:%S") + ".odt"
                 self.set_header("Content-Disposition",
                                 "attachment; filename=%s" % fname)
                 self.set_header("Content-Type",
                                 "application/vnd.oasis.opendocument.text")
             self.write(out)
         elif type == "report_txt":  # XXX: deprecated
             model = action_vals["model"]
             method = action_vals["method"]
             m = get_model(model)
             f = getattr(m, method, None)
             if not f:
                 raise Exception("method %s of %s doesn't exist" %
                                 (method, m._name))
             ctx2 = ctx.copy()  # XXX
             ctx2.update(action_vals)
             res = f(context=ctx2)
             if isinstance(res, str):
                 res = {
                     "data": res,
                 }
             out = res.get("data",
                           "").encode(action_vals.get("encoding", "utf-8"))
             filename = res.get("filename", "report.txt")
             db = get_connection()
             if db:
                 db.commit()
             self.set_header("Content-Disposition",
                             "attachment; filename=%s" % filename)
             self.set_header("Content-Type", "text/plain")
             self.write(out)
         elif type == "report_file":
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             m = get_model(model)
             f = getattr(m, method, None)
             if "ids" in action_vals:
                 ids = json.loads(action_vals["ids"])
             else:
                 ids = None
             print("ids", ids)
             ctx = action_vals.copy()
             if ids is not None:
                 res = f(ids, context=ctx)
             else:
                 res = f(context=ctx)
             db = get_connection()
             if db:
                 db.commit()
             out = res["data"]
             fname = res["filename"]
             mtype = res["mimetype"]
             self.set_header("Content-Disposition",
                             "attachment; filename=%s" % fname)
             self.set_header("Content-Type", mtype)
             self.write(out)
         elif type == "report_jsx":
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             m = get_model(model)
             f = getattr(m, method, None)
             if "ids" in action_vals:
                 ids = json.loads(action_vals["ids"])
             else:
                 ids = None
             print("ids", ids)
             ctx = action_vals.copy()
             data = f(ids, context=ctx)
             tmpl_name = action_vals.get("template")
             tmpl_body = get_report_template(tmpl_name, "jsx")
             out = report_render_jsx(tmpl_body, data)
             db = get_connection()
             if db:
                 db.commit()
             fname = tmpl_name + "-" + time.strftime(
                 "%Y-%m-%dT%H:%M:%S") + ".pdf"
             self.set_header("Content-Disposition",
                             "attachment; filename=%s" % fname)
             self.set_header("Content-Type", "application/pdf")
             self.write(out)
         else:
             raise Exception("Invalid report type: %s" % type)
     except Exception as e:
         import traceback
         traceback.print_exc(file=sys.stdout)
         db = get_connection()
         if db:
             db.rollback()
         html = netforce.template.render("report_error", {"error": str(e)})
         self.write(html)
Example #9
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 #10
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 #11
0
 def get(self):  # TODO: cleanup
     db = get_connection()
     if db:
         db.begin()
     try:
         clear_cache()
         ctx = {
             "request": self.request,
             "request_handler": self,
             "dbname": get_active_db(),
         }
         data = self.get_cookies()
         if data:
             ctx.update(data)
         action_vals = parse_args(self)
         ctx.update(action_vals)
         name = action_vals.get("name")
         if name:
             action_ctx = action_vals
             action = get_action(name, action_ctx)
             for k, v in action.items():
                 if k not in action_vals:
                     action_vals[k] = v
         if "context" in action_vals:
             ctx.update(action_vals["context"])
         action_vals["context"] = ctx
         self.clear_flash()
         type = action_vals.get("type", "view")
         if type == "report":  # XXX: deprecated
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             refer_id = action_vals.get("refer_id")
             format = action_vals.get("format", "pdf")
             if action_vals.get("ids") and isinstance(action_vals["ids"], str):
                 ids = json.loads(action_vals["ids"])
                 action_vals["ids"] = ids
             m = get_model(model)
             f = getattr(m, method, None)
             if action_vals.get("multi_page"):
                 datas = []
                 if "ids" in action_vals:
                     ids = action_vals["ids"]
                 else:
                     ids = [int(action_vals["refer_id"])]
                 for obj_id in ids:
                     ctx = action_vals.copy()
                     ctx["refer_id"] = obj_id
                     data = f(context=ctx)
                     datas.append(data)
             else:
                 data = f(context=action_vals)
             template = action_vals.get("template")
             if not template and action_vals.get("template_method"):
                 f = getattr(m, action_vals["template_method"])
                 template = f(ids, context=action_vals)
             if action_vals.get("multi_page"):
                 out = get_report_jasper_multi_page(template, datas, format=format)
             elif "pages" in data:
                 datas = []
                 for obj in data["pages"]:
                     d = data.copy()
                     d.update(obj)
                     datas.append(d)
                 out = get_report_jasper_multi_page(template, datas, format=format)
             else:
                 out = get_report_jasper(template, data, format=format)
             db = get_connection()
             if db:
                 db.commit()
             if format == "pdf":
                 fname = template + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".pdf"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/pdf")
             elif format == "xls":
                 fname = template + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".xls"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/vnd.ms-excel")
             elif format == "ods":
                 fname = template + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".ods"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/vnd.oasis.opendocument.spreadsheet")
             else:
                 raise Exception("Invalid format: %s" % format)
             self.write(out)
         elif type == "report_jasper":  # new jasper
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             refer_id = action_vals.get("refer_id")
             if refer_id:
                 refer_id = int(refer_id)
                 ids = [refer_id]
             else:
                 ids = json.loads(action_vals.get("ids"))
             format = action_vals.get("format", "pdf")
             m = get_model(model)
             f = getattr(m, method, None)
             data = f(ids, context=action_vals)
             if not data:
                 raise Exception("Missing report data")
             template = action_vals.get("template")
             if not template and action_vals.get("template_method"):
                 f = getattr(m, action_vals["template_method"])
                 template = f(ids, context=action_vals)
             if action_vals.get("multi_page"):
                 datas = []
                 for obj in data["objs"]:
                     d = data.copy()
                     d["obj"] = obj
                     datas.append(d)
                 out = get_report_jasper_multi_page(template, datas, format=format)
             else:
                 out = get_report_jasper(template, data, format=format)
             db = get_connection()
             if db:
                 db.commit()
             if format == "pdf":
                 fname = template + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".pdf"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/pdf")
             elif format == "xls":
                 fname = template + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".xls"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/vnd.ms-excel")
             elif format == "ods":
                 fname = template + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".ods"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/vnd.oasis.opendocument.spreadsheet")
             else:
                 raise Exception("Invalid format: %s" % format)
             self.write(out)
         elif type == "report_html":  # XXX: deprecated
             model = action_vals["model"]
             method = action_vals["method"]
             m = get_model(model)
             f = getattr(m, method, None)
             ctx2 = ctx.copy()  # XXX
             ctx2.update(action_vals)
             data = f(context=ctx2)
             tmpl_name = action_vals.get("template")
             tmpl = netforce.template.get_template(tmpl_name)
             ctx2["data"] = data
             html = tmpl.render({"context": ctx2})
             out = netforce.report.html2pdf(html)
             db = get_connection()
             if db:
                 db.commit()
             fname = tmpl_name + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".pdf"
             self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
             self.set_header("Content-Type", "application/pdf")
             self.write(out)
         elif type == "report_xls":
             model = action_vals["model"]
             method = action_vals["method"]
             m = get_model(model)
             f = getattr(m, method, None)
             ctx2 = ctx.copy()  # XXX
             ctx2.update(action_vals)
             data = f(context=ctx2)
             tmpl_name = action_vals.get("template")
             if not tmpl_name and action_vals.get("template_method"):
                 f = getattr(m, action_vals["template_method"])
                 tmpl_name = f(context=action_vals)
             out = report_render_xls(tmpl_name, data)
             db = get_connection()
             if db:
                 db.commit()
             fname = tmpl_name + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".xlsx"
             self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
             self.set_header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
             self.write(out)
         elif type == "report_doc":  # XXX: deprecated
             model = action_vals["model"]
             method = action_vals["method"]
             convert = action_vals.get("convert")
             m = get_model(model)
             f = getattr(m, method, None)
             ctx2 = ctx.copy()  # XXX
             ctx2.update(action_vals)
             data = f(context=ctx2)
             tmpl_name = action_vals.get("template")
             if not tmpl_name and action_vals.get("template_method"):
                 f = getattr(m, action_vals["template_method"])
                 tmpl_name = f(context=action_vals)
             out = report_render_doc(tmpl_name, data)
             db = get_connection()
             if db:
                 db.commit()
             if convert == "pdf":
                 fname = tmpl_name + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".pdf"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/pdf")
                 out = convert_to_pdf(out, "docx")
             else:
                 fname = tmpl_name + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".docx"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header(
                     "Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
             self.write(out)
         elif type == "report_odt":  # XXX: deprecated
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             convert = action_vals.get("convert")
             m = get_model(model)
             f = getattr(m, method, None)
             if action_vals.get("ids") and isinstance(action_vals["ids"], str):
                 ids = json.loads(action_vals["ids"])
                 action_vals["ids"] = ids
                 print("ids", ids)
             if action_vals.get("ids") and convert == "pdf":  # FIXME
                 outs = []
                 for id in ids:
                     ctx = action_vals.copy()
                     ctx["refer_id"] = str(id)
                     data = f(context=ctx)
                     tmpl_name = action_vals.get("template")
                     if not tmpl_name and action_vals.get("template_method"):
                         f = getattr(m, action_vals["template_method"])
                         tmpl_name = f(context=action_vals)
                     out_odt = report_render_odt(tmpl_name, data)
                     out_pdf = convert_to_pdf(out_odt, "odt")  # XXX
                     outs.append(out_pdf)
                 out = merge_pdf(outs)
             else:
                 ctx2 = ctx.copy()  # XXX
                 ctx2.update(action_vals)
                 data = f(context=ctx2)
                 tmpl_name = action_vals.get("template")
                 if not tmpl_name and action_vals.get("template_method"):
                     f = getattr(m, action_vals["template_method"])
                     tmpl_name = f(context=action_vals)
                 out = report_render_odt(tmpl_name, data)
                 if convert == "pdf":
                     out = convert_to_pdf(out, "odt")
             db = get_connection()
             if db:
                 db.commit()
             if convert == "pdf":
                 fname = tmpl_name + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".pdf"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/pdf")
             else:
                 fname = tmpl_name + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".odt"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/vnd.oasis.opendocument.text")
             self.write(out)
         elif type == "report_odt2":  # XXX: use this instead of report_odt later
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             convert = action_vals.get("convert")
             refer_id = action_vals.get("refer_id")
             m = get_model(model)
             f = getattr(m, method, None)
             if "ids" in action_vals:
                 ids = json.loads(action_vals["ids"])
             elif "refer_id" in action_vals:
                 ids = [int(action_vals["refer_id"])]
             else:
                 raise Exception("Missing report ids")
             print("ids", ids)
             ctx = action_vals.copy()
             data = f(ids, context=ctx)
             tmpl_name = action_vals.get("template")
             if not tmpl_name and action_vals.get("template_method"):
                 f = getattr(m, action_vals["template_method"])
                 tmpl_name = f(ids, context=action_vals)
             out = report_render_odt(tmpl_name, data)
             db = get_connection()
             if db:
                 db.commit()
             if convert == "pdf":
                 out = convert_to_pdf(out, "odt")
                 fname = tmpl_name + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".pdf"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/pdf")
             else:
                 fname = tmpl_name + "-" + time.strftime("%Y-%m-%dT%H:%M:%S") + ".odt"
                 self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
                 self.set_header("Content-Type", "application/vnd.oasis.opendocument.text")
             self.write(out)
         elif type == "report_txt":  # XXX: deprecated
             model = action_vals["model"]
             method = action_vals["method"]
             m = get_model(model)
             f = getattr(m, method, None)
             if not f:
                 raise Exception("method %s of %s doesn't exist" % (method, m._name))
             ctx2 = ctx.copy()  # XXX
             ctx2.update(action_vals)
             res = f(context=ctx2)
             if isinstance(res, str):
                 res = {
                     "data": res,
                 }
             out = res.get("data", "").encode(action_vals.get("encoding", "utf-8"))
             filename = res.get("filename", "report.txt")
             db = get_connection()
             if db:
                 db.commit()
             self.set_header("Content-Disposition", "attachment; filename=%s" % filename)
             self.set_header("Content-Type", "text/plain")
             self.write(out)
         elif type == "report_file":
             model = action_vals["model"]
             method = action_vals.get("method", "get_report_data")
             m = get_model(model)
             f = getattr(m, method, None)
             if "ids" in action_vals:
                 ids = json.loads(action_vals["ids"])
             else:
                 ids = None
             print("ids", ids)
             ctx = action_vals.copy()
             if ids is not None:
                 res = f(ids, context=ctx)
             else:
                 res = f(context=ctx)
             db = get_connection()
             if db:
                 db.commit()
             out = res["data"]
             fname = res["filename"]
             mtype = res["mimetype"]
             self.set_header("Content-Disposition", "attachment; filename=%s" % fname)
             self.set_header("Content-Type", mtype)
             self.write(out)
         else:
             raise Exception("Invalid report type: %s" % type)
     except Exception as e:
         import traceback
         traceback.print_exc(file=sys.stdout)
         db = get_connection()
         if db:
             db.rollback()
         html = netforce.template.render("report_error", {"error": str(e)})
         self.write(html)
Example #12
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 #13
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 #14
0
 def get(self):
     db=get_connection()
     if db:
         db.begin()
     try:
         clear_cache()
         ctx={
             "request": self.request,
             "request_handler": self,
             "dbname": get_active_db(),
         }
         data=self.get_cookies()
         if data:
             ctx.update(data)
         action_vals=parse_args(self)
         ctx.update(action_vals)
         name=action_vals.get("name")
         if name:
             action_ctx=action_vals
             action=get_action(name,action_ctx)
             for k,v in action.items():
                 if k not in action_vals:
                     action_vals[k]=v
         if "context" in action_vals:
             ctx.update(action_vals["context"])
         action_vals["context"]=ctx
         self.clear_flash()
         type=action_vals.get("type","view")
         if type=="export":
             print("XXX export")
             model=action_vals["model"]
             m=get_model(model)
             ids=action_vals.get("ids")
             if ids:
                 if ids[0]=="[": # XXX
                     ids=ids[1:-1]
                 ids=[int(x) for x in ids.split(",")]
             else:
                 condition=action_vals.get("condition")
                 if condition:
                     print("condition",condition)
                     condition=json.loads(condition)
                     ids=m.search(condition)
                 else:
                     ids=m.search([]) # XXX
             ctx=action_vals.copy()
             if ctx.get("export_fields"):
                 if isinstance(ctx["export_fields"],str):
                     ctx["export_fields"]=json.loads(ctx["export_fields"])
             else:
                 try:
                     view=get_xml_view(model=model,type="export")
                     doc=etree.fromstring(view["layout"])
                     field_names=[]
                     for el in doc.iterfind(".//field"):
                         name=el.attrib["name"]
                         field_names.append(name)
                     ctx["export_fields"]=field_names
                 except: # default export fields
                     req_field_names=[]
                     other_field_names=[]
                     for n,f in m._fields.items():
                         if isinstance(f,(fields.One2Many,fields.Many2Many)):
                             continue
                         if isinstance(f,fields.Json):
                             continue
                         if not f.store and not f.function:
                             continue
                         if f.required:
                             req_field_names.append(n)
                         else:
                             other_field_names.append(n)
                     ctx["export_fields"]=sorted(req_field_names)+sorted(other_field_names)
             data=m.export_data(ids,context=ctx)
             db=get_connection()
             if db:
                 db.commit()
             filename=action_vals.get("filename","export.csv")
             self.set_header("Content-Disposition","attachment; filename=%s"%filename)
             self.set_header("Content-Type","text/csv")
             self.write(data)
         else:
             raise Exception("Invalid action type: %s"%type)
     except Exception as e:
         import traceback
         traceback.print_exc(file=sys.stdout)
         db=get_connection()
         if db:
             db.rollback()
         raise e