def __ui_app_info(context): app_id = context.get_arg("app_id", 1) fetch = build_fetchxml_by_alias(context, "api_ui_app", app_id) fetchparser = FetchXmlParser(fetch, context) rs_app = DatabaseServices.exec(fetchparser, context, run_as_system=True, fetch_mode=1) fetch = f""" <restapi type="select"> <table name="api_ui_app_nav_item" alias="a"/> <select> <field name="id" table_alias="a"/> <field name="name" table_alias="a"/> <field name="url" table_alias="a"/> <field name="type_id" table_alias="a"/> </select> <filter type="and"> <condition field="app_id" value="{app_id}" operator="="/> </filter> </restapi> """ fetchparser = FetchXmlParser(fetch, context) rs_items = DatabaseServices.exec(fetchparser, context, run_as_system=True, fetch_mode=0) return (rs_app, rs_items)
def post(self, table): try: context = g.context if request.json == None: abort( 400, "cannot extract json data in http request for insert %s" % (table)) fetch = build_fetchxml_by_alias(context, table, None, request.json, type="insert") fetchparser = FetchXmlParser(fetch, context) rs = DatabaseServices.exec(fetchparser, context, fetch_mode=0) result = { "rows_affected": rs.get_cursor().rowcount, "inserted_id": rs.get_inserted_id() } return result except RestApiNotAllowed as err: logger.exception(f"{err}") abort(400, f"{err}") except Exception as err: logger.exception(f"{err}") abort(500, f"{err}")
def post(self, table): try: context = g.context json = request.form.to_dict() fetch = build_fetchxml_by_alias(context, table, None, json, type="insert") fetchparser = FetchXmlParser(fetch, context) rs = DatabaseServices.exec(fetchparser, context, fetch_mode=0) result = { "rows_affected": rs.get_cursor().rowcount, "inserted_id": rs.get_inserted_id() } next = HTTPRequest.redirect( request, default=f"/ui/v1.0/data/{table}/{rs.get_inserted_id()}", id=rs.get_inserted_id()) return redirect(next, code=302) except RestApiNotAllowed as err: logger.info(f"RestApiNotAllowed Exception: {err}") return redirect(f"/ui/login?redirect=/ui/v1.0/data/{table}", code=302) except Exception as err: return make_response( JinjaTemplate.render_status_template(context, 500, err), 500)
def get(self, table, id): try: create_parser().parse_args() context = g.context table_meta = read_table_meta(context, alias=table) solution_id = table_meta['solution_id'] if solution_id == 1: file = f"templates/{table}_update.htm" else: file = f"solutions/{solution_id}/{table}_update.htm" fetch = build_fetchxml_by_alias(context, table, id, type="select") fetchparser = FetchXmlParser(fetch, context) rs = DatabaseServices.exec(fetchparser, context, fetch_mode=1) if rs.get_result() == None: abort(404, "Item not found => %s" % id) # # render the defined jinja template (in case ofahtm file) # logger.info(f"Redirect : {next}") template = JinjaTemplate.create_file_template(context, file) response = make_response( template.render({ "table": table, "pagemode": "dataformupdate", "id": id, "data": rs.get_result(), "context": context })) response.headers['content-type'] = 'text/html' return response except ConfigNotValid as err: logger.exception(f"Config not valid {err}") return make_response( JinjaTemplate.render_status_template(context, 500, err), 500) except jinja2.exceptions.TemplateNotFound as err: logger.exception(f"TemplateNotFound: {err}") return make_response( JinjaTemplate.render_status_template( context, 404, f"Template not found {err}"), 404) except FileNotFoundError as err: logger.exception(f"FileNotFound: {err}") return make_response( JinjaTemplate.render_status_template(context, 404, f"File not found {err}"), 404) except RestApiNotAllowed as err: logger.exception(f"RestApiNotAllowed Exception: {err}") #return redirect(f"/auth/login.htm?redirect=/{path}", code=302) return redirect(f"/ui/login?redirect={request.url}", code=302) except Exception as err: logger.exception(f"Exception: {err}") return make_response( JinjaTemplate.render_status_template(context, 500, err), 500)
def __datacomboview(table_alias, viewname, query): context = g.context table_meta = read_table_meta(context, alias=table_alias) view_meta = read_table_view_meta(g.context, table_meta['id'], viewname, 'SELECTVIEW') fetchparser = FetchXmlParser( str(view_meta['fetch_xml']).replace('$$query$$', query), context) rs = DatabaseServices.exec(fetchparser, context, fetch_mode=0) return rs.get_result()
def get(self): try: path = "templates/base/uihome.htm" logger.info(f"Path: {path}") create_parser().parse_args() context = g.context template = JinjaTemplate.create_file_template(context, path) fetch = f""" <restapi type="select"> <table name="api_ui_app" alias="a"/> <select> <field name="id" table_alias="a"/> <field name="name" table_alias="a"/> <field name="description" table_alias="a"/> <field name="home_url" table_alias="a"/> </select> </restapi> """ fetch_parser = FetchXmlParser(fetch, context) rs = DatabaseServices.exec(fetch_parser, context, fetch_mode=0) response = make_response( template.render({ "context": context, "apps": rs.get_result() })) response.headers['content-type'] = 'text/html' return response except ConfigNotValid as err: logger.exception(f"Config not valid {err}") return make_response( JinjaTemplate.render_status_template(500, err), 500) except jinja2.exceptions.TemplateNotFound as err: logger.exception(f"TemplateNotFound: {err}") return make_response( JinjaTemplate.render_status_template( 404, f"Template not found {err}"), 404) except FileNotFoundError as err: logger.exception(f"FileNotFound: {err}") return make_response( JinjaTemplate.render_status_template(404, f"File not found {err}"), 404) except RestApiNotAllowed as err: logger.exception(f"RestApiNotAllowed Exception: {err}") return redirect(f"/ui/login?redirect={request.url}", code=302) except Exception as err: logger.exception(f"Exception: {err}") return make_response( JinjaTemplate.render_status_template(500, err), 500)
def post(self): try: parser = create_parser().parse_args() context = g.context fetch = request.data fetchparser = FetchXmlParser(fetch, context) rs = DatabaseServices.exec(fetchparser, context, fetch_mode=0) return rs.get_result() except RestApiNotAllowed as err: abort(400, f"{err}") except Exception as err: abort(500, f"{err}")
def delete(self,table,id): try: create_parser_delete().parse_args() context=g.context fetch=build_fetchxml_by_alias(context,table,id, type="delete") fetchparser=FetchXmlParser(fetch, context) rs=DatabaseServices.exec(fetchparser,context, fetch_mode=0) result={"rows_affected": rs.get_cursor().rowcount} rs.close() return result except RestApiNotAllowed as err: abort(400, f"{err}") except Exception as err: abort(500,f"{err}")
def get(self, table): try: create_parser().parse_args() context = g.context fetch = build_fetchxml_by_alias(context, table, None, None, type="select") fetchparser = FetchXmlParser(fetch, context) rs = DatabaseServices.exec(fetchparser, context, fetch_mode=0) return rs.get_result() except RestApiNotAllowed as err: logger.info(f"{err}") abort(400, f"{err}") except Exception as err: abort(500, f"{err}")
def test_exec(self): xml = f""" <restapi type="select"> <table name="dummy" alias="d"/> <select> <field name="id" func="count"/> <field name="name" grouping="y"/> </select> <filter> <condition field="id" value="99" operator="geq" alias="d"/> </filter> </restapi> """ print(xml) fetch = FetchXmlParser(xml, self.context) dummy = DatabaseServices.exec(fetch, self.context, fetch_mode=1) #self.assertIsNone(dummy.get_result()) print(dummy)
def put(self,table,id): try: create_parser_put().parse_args() context=g.context if request.json==None: abort(400, "cannot extract json data in body %s %s" % (table,id)) fetch=build_fetchxml_by_alias(context, table, id, request.json, type="update") fetchparser=FetchXmlParser(fetch, context) rs=DatabaseServices.exec(fetchparser,context, fetch_mode=0) result={"rows_affected": rs.get_cursor().rowcount} rs.close() return result except RestApiNotAllowed as err: abort(400, f"{err}") except Exception as err: abort(500,f"{err}")
def __get_content(self, context, portal_id, content_name): fetch = f""" <restapi type="select"> <table name="api_portal_content"/> <filter type="and"> <condition field="name" value="{content_name}" operator="="/> <condition field="portal_id" value="{portal_id}" operator="="/> </filter> </restapi> """ fetchparser = FetchXmlParser(fetch, context) rs_content = DatabaseServices.exec(fetchparser, context, run_as_system=True, fetch_mode=1) if rs_content.get_eof(): return None return rs_content.get_result()
def get(self,table,id,field=""): try: create_parser_get().parse_args() context=g.context fetch=build_fetchxml_by_alias(context, table, id, type="select") fetchparser=FetchXmlParser(fetch, context) rs=DatabaseServices.exec(fetchparser,context, fetch_mode=1) if rs.get_result()==None: abort(400, "Item not found => %s" % id) if field=="": return rs.get_result() else: return rs.get_result()[field] except RestApiNotAllowed as err: abort(400, f"{err}") except Exception as err: abort(500,f"{err}")
def post(self,table,id): try: create_parser_post().parse_args() context=g.context json=request.form.to_dict() for key in list(json): if key.startswith("__"): del json[key] fetch=build_fetchxml_by_alias(context, table, id, json, type="update") fetchparser=FetchXmlParser(fetch, context) rs=DatabaseServices.exec(fetchparser,context, fetch_mode=0) next=HTTPRequest.redirect(request, default=f"/ui/v1.0/data/{table}/$$id$$", id=id) return redirect(next, code=302) except RestApiNotAllowed as err: logger.info(f"RestApiNotAllowed Exception: {err}") return redirect(f"/ui/login?redirect=/ui/v1.0/data/{table}/{id}", code=302) except Exception as err: return make_response(JinjaTemplate.render_status_template(500, err), 500)
def get(self, path="index.htm", session_id=None, content_name=None): try: logger.info(f"Path: {path}") logger.info(f"content_name: {content_name}") logger.info(f"session_id: {session_id}") create_parser().parse_args() context = g.context portal_id = "default" # # content class # if not path.endswith("login.htm"): pass # # render the defined jinja template (in case ofahtm file) # if path.endswith('.htm'): next = HTTPRequest.redirect(request) logger.info(f"Redirect : {next}") fetch = build_fetchxml_by_alias(context, "api_portal", portal_id, type="select") fetchparser = FetchXmlParser(fetch, context) rs = DatabaseServices.exec(fetchparser, context, fetch_mode=1) if rs.get_result() == None: abort(404, "Portal not found => %s" % id) # render the content first if content_name == None: content_name = HTTPRequest.get_querystring_value( request, "content_name", default="home") content = self.__get_content(context, portal_id, content_name) if content == None: abort(404, "Content not found => %s" % content_name) params = { "context": context, "content_name": content_name, "content_id": content['id'] } template = JinjaTemplate.create_string_template( context, content['content'].encode('utf-8').decode('utf-8')) content_str = template.render(params) # # Render the complete page # params['content'] = content_str template = JinjaTemplate.create_file_template(context, path) page_str = template.render(params) response = make_response(page_str) response.headers['content-type'] = 'text/html' return response else: www_root = FileSystemTools.format_path( AppInfo.get_current_config('ui', 'wwwroot', exception=True)) return send_file(f"{www_root}{path}") except ConfigNotValid as err: logger.exception(f"Config not valid {err}") return make_response( JinjaTemplate.render_status_template(context, 500, err), 500) except jinja2.exceptions.TemplateNotFound as err: logger.exception(f"TemplateNotFound: {err}") return make_response( JinjaTemplate.render_status_template( context, 404, f"Template not found {err}"), 404) except FileNotFoundError as err: logger.exception(f"FileNotFound: {err}") return make_response( JinjaTemplate.render_status_template(context, 404, f"File not found {err}"), 404) except RestApiNotAllowed as err: logger.exception(f"RestApiNotAllowed Exception: {err}") return redirect(f"/ui/login?redirect=/{path}", code=302) except Exception as err: logger.exception(f"Exception: {err}") return make_response( JinjaTemplate.render_status_template(context, 500, err), 500)
def test_exec(self): xml=f""" <restapi type="delete"> <table name="dummy"/> <filter> <condition field="id" value="99"/> </filter> </restapi> """ fetch=FetchXmlParser(xml,self.context) DatabaseServices.exec(fetch,self.context) xml=f""" <restapi type="select"> <table name="dummy"/> <filter> <condition field="id" value="99"/> </filter> </restapi> """ fetch=FetchXmlParser(xml, self.context) dummy=DatabaseServices.exec(fetch,self.context, fetch_mode=1) self.assertIsNone(dummy.get_result()) xml=f""" <restapi type="insert"> <table name="dummy"/> <filter> <condition field="id" value="99"/> </filter> <fields> <field name="id" value="99"/> <field name="name" value="IC735"/> <field name="port" value="3306"/> </fields> </restapi> """ fetch=FetchXmlParser(xml, self.context) DatabaseServices.exec(fetch,self.context) xml=f""" <restapi type="select"> <table name="dummy"/> <filter> <condition field="id" value="99"/> </filter> </restapi> """ fetch=FetchXmlParser(xml, self.context) dummy=DatabaseServices.exec(fetch,self.context, fetch_mode=1) self.assertIsNotNone (dummy.get_result()) xml=f""" <restapi type="update"> <table name="dummy"/> <filter> <condition field="id" value="99"/> </filter> <fields> <field name="name" value="UPDATE"/> </fields> </restapi> """ fetch=FetchXmlParser(xml, self.context) DatabaseServices.exec(fetch,self.context) xml=f""" <restapi type="select"> <table name="dummy"/> <filter> <condition field="id" value="99"/> </filter> </restapi> """ fetch=FetchXmlParser(xml, self.context) dummy=DatabaseServices.exec(fetch,self.context, fetch_mode=1) self.assertEqual(dummy.get_result()['name'], "UPDATE")
def execute(context, plugin_context, params): if not __validate(params): logger.warning(f"Missings params") return data = params['input'] fetch = f""" <restapi type="select"> <table name="aprs_object" alias="o"/> <select> <field name="object_name" table_alias="o"/> <field name="lat_deg" table_alias="o"/> <field name="long_deg" table_alias="o"/> <field name="overlay" table_alias="o"/> <field name="symbol_code" table_alias="o"/> <field name="comment" table_alias="o"/> <field name="type" table_alias="o"/> <field name="source_address" table_alias="o"/> <field name="lat_min" table_alias="o"/> <field name="lat_sec" table_alias="o"/> <field name="long_min" table_alias="o"/> <field name="long_sec" table_alias="o"/> <field name="callsign" table_alias="l"/> <field name="passcode" table_alias="l"/> </select> <orderby> <field name="id" alias="o" sort="DESC"/> </orderby> <joins> <join table="aprs_login" alias="l" condition="l.id=o.login_id" type="inner"/> </joins> </restapi> """ fetchparser = FetchXmlParser(fetch, context) rs = DatabaseServices.exec(fetchparser, context, fetch_mode=0, run_as_system=True) #https://www.koordinaten-umrechner.de for obj in rs.get_result(): callsign = obj['callsign'] passcode = obj['passcode'] obj_name = (obj['object_name']).ljust(9) timestamp = datetime.today().strftime('%d%H%M') + 'z' #lat="5205.23N" lat_dec_sec = int(round((100.0 / 60.0) * float(obj['lat_sec']), 0)) lat = str(obj['lat_deg']).rjust(2, '0') + str(obj['lat_min']).rjust( 2, '0') + '.' + str(lat_dec_sec).rjust(2, '0') + "N" #long="01022.14E" long_dec_sec = int(round((100.0 / 60.0) * float(obj['long_sec']), 0)) long = str(obj['long_deg']).rjust(3, '0') + str(obj['long_min']).rjust( 2, '0') + '.' + str(long_dec_sec).rjust(2, '0') + "E" overlay = (obj['overlay']).ljust(1) symbol = (obj['symbol_code']).ljust(1) comment = (obj['comment']) type = obj['type'] payload = f";{obj_name}{type}{timestamp}{lat}{overlay}{long}{symbol}{comment}" aprs = f"{obj['source_address']}>APRS,TCPIP*:{payload}" __send(aprs, callsign, passcode, logger) params['output']['aprs'] = ""
def get(self, table, view="default"): try: parser = create_parser().parse_args() context = g.context logger.info(f"app_id: {request.url}") file = f"templates/base/datalist.htm" query = "%" if 'query' in request.args: query = f"%{request.args.get('query')}%" table_meta = read_table_meta(context, alias=table) view_meta = read_table_view_meta(context, table_meta['id'], view, 'LISTVIEW') fetch = view_meta['fetch_xml'] #columns=json.loads(view_meta['col_definition']) fetch = fetch.replace("$$query$$", query) fetchparser = FetchXmlParser(fetch, context) rs = DatabaseServices.exec(fetchparser, context, fetch_mode=0) template = JinjaTemplate.create_file_template(context, file) response = make_response( template.render({ "data": rs.get_result(), "columns": fetchparser.get_columns(), "table": table, "table_meta": table_meta, "view_meta": view_meta, "pagemode": "dataformlist", "context": context })) response.headers['content-type'] = 'text/html' return response except ConfigNotValid as err: logger.exception(f"Config not valid {err}") return make_response( JinjaTemplate.render_status_template(context, 500, err), 500) except DataViewNotFound as err: logger.exception(f"Dataview on api_table_view not found: {err}") return make_response( JinjaTemplate.render_status_template(context, 500, "Dataview not found!"), 500) except jinja2.exceptions.TemplateNotFound as err: logger.exception(f"TemplateNotFound: {err}") return make_response( JinjaTemplate.render_status_template( context, 404, f"Template not found {err}"), 404) except FileNotFoundError as err: logger.exception(f"FileNotFound: {err}") return make_response( JinjaTemplate.render_status_template(context, 404, f"File not found {err}"), 404) except RestApiNotAllowed as err: logger.exception(f"RestApiNotAllowed Exception: {err}") #return redirect(f"/ui/login?redirect=/ui/v1.0/data/view/{table}/default", code=302) return redirect(f"/ui/login?redirect={request.url}", code=302) except Exception as err: logger.exception(f"Exception: {err}") return make_response( JinjaTemplate.render_status_template(context, 500, err), 500)