def get(self, url): sesid = self.get_cookie('sesid') or self.request.headers.get('Auth') #get session id cookie if primaryAuthorization == '1' and sesid is None: self.set_status(401,None) self.write('{"message":"No session"}') return squery = 'select * from framework.fn_userjson(%s)' userdetail = [] try: userdetail = yield self.db.execute(squery,(sesid,)) except Exception as e: showError(str(e), self) log('/admin/getsettings_Error',str(e)) return userdetail = userdetail.fetchone()[0] if userdetail is None: self.set_status(401,None) self.write('{"message":"no session or session was killed"}') return roles = userdetail.get('roles') if int(developerRole) not in roles: self.set_status(403,None) self.write('{"message":"access denied"}') return args = self.request.arguments for k in args: args[k] = args.get(k)[0].decode('utf-8') filename = args.get('filename') if filename is None: self.set_status(500,None) self.write('{"message":"enter filename"}') return filepath = './logs/' + filename f = open(filepath,encoding='utf-8') file_text = f.read() f.close() file_arr = file_text.split('\n') res_json = [] for x in file_arr: if x.find('||') != -1: res_json.append({'log_line': x}) else: index = len(res_json) - 1 if index >= 0: res_json[index] = {'log_line':res_json[index].get('log_line') + ' || ' + x} self.write(dumps(res_json)) return
def put(self, url): sesid = self.get_cookie('sesid') or self.request.headers.get('Auth') #get session id cookie if primaryAuthorization == '1' and sesid is None: self.set_status(401,None) self.write('{"message":"No session"}') return squery = 'select * from framework.fn_userjson(%s)' userdetail = [] try: userdetail = yield self.db.execute(squery,(sesid,)) except Exception as e: showError(str(e), self) return userdetail = userdetail.fetchone()[0] if userdetail is None: self.set_status(401,None) self.write('{"message":"no session or session was killed"}') return roles = userdetail.get('roles') if int(developerRole) not in roles: self.set_status(403,None) self.write('{"message":"access denied"}') return body = loads(self.request.body.decode('utf-8')) css_text = body.get('usercss') if css_text is None: self.set_status(500,None) self.write('{"message":"text is empty"}') return css_file = open('./user.css','wt') css_text = css_file.write(css_text) css_file.close() '''squery = 'select * from framework.fn_mainsettings_usercss(%s)' result = None try: result = yield self.db.execute(squery,(css_text,)) except Exception as e: showError(str(e), self) return ''' self.write('{"message":"OK"}') return
def get(self, url): sesid = self.get_cookie('sesid') or self.request.headers.get('Auth') #get session id cookie if primaryAuthorization == '1' and sesid is None: self.set_status(401,None) self.write('{"message":"No session"}') return squery = 'select * from framework.fn_userjson(%s)' userdetail = [] try: userdetail = yield self.db.execute(squery,(sesid,)) except Exception as e: showError(str(e), self) log('/admin/getsettings_Error',str(e)) return userdetail = userdetail.fetchone()[0] if userdetail is None: self.set_status(401,None) self.write('{"message":"no session or session was killed"}') return roles = userdetail.get('roles') if int(developerRole) not in roles: self.set_status(403,None) self.write('{"message":"access denied"}') return args = self.request.arguments for k in args: args[k] = args.get(k)[0].decode('utf-8') substr = args.get('substr') or '' pagenum = int(args.get('pagenum') or 1) pagesize = int(args.get('pagesize') or 20) off = (pagenum * pagesize) - pagesize logs = listdir(path = './logs') lgs = [] for x in logs: if (x.find(substr) != -1): lgs.append({'filename':x}) lgs = lgs[off:] lgs = lgs[:pagesize] self.write(dumps(lgs))
def onFileUpload(self, url, type): ''' Function for post request on universal api for upload file (for class Uplo) ''' args = {} #variable for arguments or body method = url[4:] #cut 4 symbols from url start, work only if it will be api/ files = [] #variable for files sesid = self.get_cookie("sesid") or self.request.headers.get('Auth') #get session id cookie if type != 1 and self.request.headers.get('Content-Type').find('multipart/form-data') == -1: log(url, 'args: ' + str(self.request.arguments) + '; body: ' + str(self.request.body.decode('utf-8')) + '; sess: ' + str(sesid) + '; type: ' + str(type)) else: log(url, 'args: ' + str(self.request.arguments) + '; sess: ' + str(sesid) + '; type: ' + str(type)) if primaryAuthorization == "1" and sesid == '': self.set_status(401,None) self.write('{"message":"No session"}') return args = self.request.arguments for k in args: args[k] = args.get(k)[0].decode('utf-8') files = self.request.files value = args.get('value') if not value: value = '[]' if files: value = loads(value) if args.get('config') and loads(args.get('config')).get('type') in ('file','image') and len(value) > 0: showError('for type file/image can not be more then 1 file',self) return value = value + savefile(self) #args['value'] = dumps(value) self.set_header('Content-Type','application/json charset="utf-8"') self.write(dumps(value, indent=4, default=lambda x:str(x),ensure_ascii=False)) self.set_status(200,None) self.finish()
def get(self, url): args = self.request.arguments for k in args: args[k] = args.get(k)[0].decode('utf-8') path = args.get('path') if path is None: showError('HINT:path not specified +++___', self) return method = url[7:].replace('/','').lower() sesid = self.get_cookie('sesid') or self.request.headers.get('Auth') #get session id cookie if primaryAuthorization == '1' and sesid is None: self.set_status(401,None) self.write('{"message":"No session"}') return squery = 'select * from framework.fn_userjson(%s)' userdetail = [] try: userdetail = yield self.db.execute(squery,(sesid,)) except Exception as e: showError(str(e), self) return userdetail = userdetail.fetchone()[0] userdetail['sessid'] = sesid squery = 'SELECT framework."fn_view_getByPath_showSQL"(%s)' result = [] roles = (userdetail.get('roles') or []) if int(developerRole) not in roles: self.set_status(403,None) self.write('{"message":"access denied"}') return try: result = yield self.db.execute(squery,(path,)) except Exception as e: showError(str(e), self) log(url + '_Error', str(e)) return result = result.fetchone() if not result: self.set_status(500,None) self.write('{"message":"view is not found"}') return result = result[0] #self.write(dumps(result)) query = getList(result, {}, userdetail=userdetail) squery = query[0] self.write(squery)
def post(self, url): sesid = self.get_cookie('sesid') or self.request.headers.get('Auth') #get session id cookie if primaryAuthorization == '1' and sesid is None: self.set_status(401,None) self.write('{"message":"No session"}') return squery = 'select * from framework.fn_userjson(%s)' userdetail = [] try: userdetail = yield self.db.execute(squery,(sesid,)) except Exception as e: showError(str(e), self) return userdetail = userdetail.fetchone()[0] if userdetail is None: self.set_status(401,None) self.write('{"message":"no session or session was killed"}') return roles = userdetail.get('roles') if int(developerRole) not in roles: self.set_status(403,None) self.write('{"message":"access denied"}') return body = loads(self.request.body.decode('utf-8')) #settingsFile = body.get('settings') settingsFile = body squery = 'select * from framework.fn_mainsettings_save(%s)' try: userdetail = yield self.db.execute(squery,(extras.Json(settingsFile),)) except Exception as e: showError(str(e), self) return if settingsFile: try: df = open('./settings.json','wt') df.write(dumps(settingsFile)) df.close() df = open('./settings.py','at') df.write(' ') df.close except Exception as e: showError(str(e), self) return log('/admin/savesettings',' settingsFile:' + str(settingsFile) + ' userdetail: ' + str(userdetail)) self.write('{"message":"OK"}')
def post(self, url): method = url[ 5:] #cut 4 symbols from url start, work only if it will be auth/ log(url, str(self.request.body)) self.clear_cookie('sesid') if method == 'logout': sesid = self.get_cookie('sesid') if sesid: squery = 'select * from framework.fn_logout(%s)' result = None try: result = yield self.db.execute(squery, (sesid, )) except Exception as e: showError(str(e), self) log(url + '_Error', str(e)) return self.write('{"message":"OK"}') elif method == 'auth_f': body = loads(self.request.body.decode('utf-8')) login = body.get('login') passw = body.get('pass') sesid = self.request.headers.get('Auth') passw = sha224(passw.encode('utf-8')).hexdigest() if login is None or passw is None: self.write('{"message":"login or password is null"}') self.set_status(500, None) return squery = 'select * from framework.fn_sess(%s,%s,%s);' try: result = yield self.db.execute(squery, (login, passw, sesid)) except Exception as e: showError(str(e), self) log(url + '_Error', str(e)) return result = result.fetchone()[0] self.set_cookie('sesid', result) self.write('{"message":"OK"}') elif method == 'auth_crypto': body = loads(self.request.body.decode('utf-8')) sesid = self.request.headers.get('Auth') squery = 'select * from framework.fn_cryptosess(%s,%s);' try: result = yield self.db.execute(squery, ( extras.Json(body), sesid, )) except Exception as e: showError(str(e), self) log(url + '_Error', str(e)) return result = result.fetchone()[0] self.set_cookie('sesid', result) self.write('{"message":"OK"}') else: self.set_status(404, None) self.write('{"message":"method not found"}')
def onRequest(self, url, type): ''' Function for get,post,put and delete requests on universal api (for class FApi) ''' args = {} #variable for arguments or body method = url[4:] #cut 4 symbols from url start, work only if it will be api/ files = [] #variable for files sesid = self.get_cookie("sesid") or self.request.headers.get('Auth') #get session id cookie if type != 1 and self.request.headers.get('Content-Type').find('multipart/form-data') == -1: log(url, 'args: ' + str(self.request.arguments) + '; body: ' + str(self.request.body.decode('utf-8')) + '; sess: ' + str(sesid) + '; type: ' + str(type)) else: log(url, 'args: ' + str(self.request.arguments) + '; sess: ' + str(sesid) + '; type: ' + str(type)) if primaryAuthorization == "1" and sesid == '': self.set_status(401,None) self.write('{"message":"No session"}') return args = self.request.arguments for k in args: args[k] = args.get(k)[0].decode('utf-8') if type in (2,4): files = self.request.files body = {} if files: value = args.get('value') if not value: value = '[]' value = loads(value) if args.get('config') and loads(args.get('config')).get('type') in ('file','image') and len(value) > 0: showError('for type file/image can not be more then 1 file',self) return value = value + savefile(self) args['value'] = dumps(value) else: body = loads(self.request.body.decode('utf-8')) #request body, expecting application/json type for k in args: body[k] = args.get(k) args = body for k in args: if args[k] == '': args[k] = None squery = 'select * from framework.fn_fapi(injson:=%s,apititle:=%s,apitype:=%s,sessid:=%s,primaryauthorization:=%s)' result = None try: result = yield self.db.execute(squery,(extras.Json(args),method,str(type),sesid,str(primaryAuthorization),)) except Exception as e: log(url + '_Error',' args: ' + str(extras.Json(args)) + '; sess: ' + str(sesid) + '; type: ' + str(type) + '; Error:' + str(e)) showError(str(e), self) return result = result.fetchone()[0] self.set_header("Content-Type",'application/json charset="utf-8"') self.write(dumps(result, indent=4, default=lambda x:str(x),ensure_ascii=False)) self.set_status(200,None) self.finish()
def Report(self, url): """ Function for call node js report method and get xls or xlsx file """ args = {} #variable for arguments or body report_path = url[4:] #cut 4 symbols from url start, work only if it will be rep/ sesid = self.get_cookie('sesid') or self.request.headers.get('Auth') #get session id cookie log(url, 'args: ' + str(self.request.arguments) + '; sess: ' + sesid + '; type: 1') if primaryAuthorization == "1" and sesid == '': self.set_status(401,None) self.write('{"message":"No session"}') return args = self.request.arguments for k in args: args[k] = args.get(k)[0].decode('utf-8') if args.get('filename') is None: showError('{"message":"filename is empty"}', self) return injson = {'injson':args, 'sess':sesid, 'report_path':report_path} squery = 'select * from reports.fn_call_report(injson:=%s)' result = None try: result = yield self.db.execute(squery,(extras.Json(injson),)) except Exception as e: log(url + '_Error',' args: ' + str(extras.Json(args)) + '; sess: ' + sesid + '; type: 1; Error:' + str(e)) showError(str(e), self) return res = result.fetchone()[0] data = res.get('outjson') reqBody = {'template':'..' + res.get('template_path'),'data':dumps(data), 'filename':args.get('filename')} http_client = AsyncHTTPClient(); req = HTTPRequest( url=reports_url, method='POST', headers={'Content-Type':'application/json'}, body=dumps(reqBody), connect_timeout=200.0, request_timeout=200.0 ); try: req = yield http_client.fetch(req) except HTTPError as e: if e.response and e.response.body: e = e.response.body.decode('utf-8') log(url + '_Error_NodeJs',' args: ' + str(extras.Json(args)) + '; sess: ' + sesid + '; type: 1; Error:' + str(e)) showError(str(e), self) return except Exception as err: system('cd reports && node index.js') # try start reports server try: req = yield http_client.fetch(req) except Exception as err: showError('No connection to the report server',self) return if res.get('ishtml'): html_report = StringIO() reportFilePath = './files/' + str(uuid4()) + '.xlsx' reportFile = open(reportFilePath, 'wb') reportFile.write(req.buffer.read()) reportFile.close() html = xlsx2html(reportFilePath, html_report) html_report.seek(0) html_report = html_report.read() self.set_header('Content-Type', 'text/html') html_report += ( '<script>window.print()</script>' + '<style type="text/css" media="print">' + '@page { size: auto; margin: 0mm; } </style>' ) self.write(html_report) else: self.set_header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') self.set_header('Cache-Control', 'public') self.set_header('Content-Disposition', 'attachment; filename=' + args.get('filename') + '.xlsx') self.set_header('Content-Description', 'File Transfer') self.write(req.body) self.set_status(200) self.finish()
def post(self, url): args = self.request.arguments for k in args: args[k] = args.get(k)[0].decode('utf-8') path = args.get('path') if path is None: showError('HINT:path not specified +++___', self) return body = loads(self.request.body.decode('utf-8')) method = url[7:].replace('/','').lower() sesid = self.get_cookie('sesid') or self.request.headers.get('Auth') #get session id cookie log(url, 'path: '+ path + '; body: ' + str(body) + ' sessid:' + str(sesid) ) if primaryAuthorization == '1' and sesid is None: self.set_status(401,None) self.write('{"message":"No session"}') return squery = 'select * from framework.fn_userjson(%s)' userdetail = [] try: userdetail = yield self.db.execute(squery,(sesid,)) except Exception as e: showError(str(e), self) return userdetail = userdetail.fetchone()[0] userdetail['sessid'] = sesid #userdetail = userdetail.get('outjson') if method == 'list': squery = 'SELECT framework."fn_view_getByPath"(%s,%s)' result = [] try: result = yield self.db.execute(squery,(path,'list',)) except Exception as e: showError(str(e), self) return result = result.fetchone()[0] if not result: self.set_status(500,None) self.write('{"message":"view is not found"}') return #result = result[0] if len(result.get('roles')) > 0: x = False else: x = True for col in result.get('roles'): if col.get('value') in (userdetail.get('roles') or []) and not x: x = True if not x: self.set_status(403,None) self.write('{"message":"access denied"}') return user = {} # if exist initial action onLoad actions = result.get('acts') onLoad = None for act in actions: if act.get('type') == 'onLoad': onLoad = act if onLoad: req_url = onLoad.get('act') if 'inputs' in body and onLoad.get('parametrs') is not None: req_url += '?' for param in onLoad.get('parametrs'): req_url += param.get('paramtitle') + '=' + (str(body.get('inputs').get(param.get('paraminput')) or '') ) + '&' if req_url[:4] != 'http': req_url = maindomain + req_url if onLoad.get('actapitype').lower() == 'get': req = HTTPRequest( url = req_url, method = onLoad.get('actapitype'), headers = {'Cookie':'sesid=' + sesid} ) else: req_body = {} if onLoad.get('parametrs') is not None: for param in onLoad.get('parametrs'): req_body[param.get('paramtitle')] = body.get('inputs').get(param.get('paraminput')) req = HTTPRequest( url = req_url, body = dumps(req_body), method = onLoad.get('actapitype'), headers = {'Cookie':'sesid=' + sesid} ) try: response = yield http_client.fetch(req) except HTTPError as e: if e.response and e.response.body: e = e.response.body.decode('utf-8') showError(str(e), self) log(req_url + '_Error_onLoad', str(e)) log(req_url + '_Error_act', str(onLoad)) return # if exist initial action onLoad data = [] count = 0 config = result.get('config') filters = result.get('filters') acts = result.get('acts') title = result.get('title') classname = result.get('classname') pagination = result.get('pagination') pagecount = result.get('pagecount') ispagesize = result.get('ispagesize') isfoundcount = result.get('isfoundcount') subscrible = result.get('subscrible') orderby = result.get('orderby') checker = result.get('checker') if result.get('viewtype').find('api_') == -1: query = getList(result, body, userdetail=userdetail) acts = result.get('acts') config = result.get('config') squery = query[0] scounquery = query[1] try: data = yield self.db.execute(squery) except Exception as e: showError(str(e), self) log(url + '_Error', str(e)) return data = curtojson([x for x in data],[x[0] for x in data.description]) try: count = yield self.db.execute(scounquery) except Exception as e: showError(str(e), self) log(url + '_Error_count', str(e)) return count = count.fetchone()[0] else: req_url = result.get('tablename') if req_url[:4] != 'http': req_url = maindomain + req_url req = HTTPRequest( url = req_url, body = dumps(body), method = 'POST', headers = {'Cookie':'sesid=' + sesid} ) try: response = yield http_client.fetch(req) except HTTPError as e: if e.response and e.response.body: e = e.response.body.decode('utf-8') showError(str(e), self) log(req_url + ' api error', str(e)) log(url + '_error_act', str(onLoad)) return data = loads(response.body.decode('utf-8')) if data is not None: if 'foundcount' in data: count = data.get('foundcount') else: count = None if 'config' in data and data.get('config') is not None: config = data.get('config') if 'acts' in data and data.get('acts') is not None: acts = data.get('acts') if 'filters' in data and data.get('filters') is not None: filters = data.get('filters') if 'classname' in data: classname = data.get('classname') if 'title' in data: title = data.get('title') if 'pagination' in data: pagination = data.get('pagination') if 'pagecount' in data: pagecount = data.get('pagecount') if 'ispagesize' in data: ispagesize = data.get('ispagesize') if 'isfoundcount' in data: isfoundcount = data.get('isfoundcount') if 'subscrible' in data: subscrible = data.get('subscrible') if 'orderby' in data: orderby = data.get('orderby') if 'checker' in data: checker = data.get('checker') if 'outjson' in data: data = data.get('outjson') else: data = [] useroles = userdetail.get('roles') or [] if acts: filteredActs = [] for act in acts: if 'roles' in act and len(act.get('roles')) > 0: fAct = [] for obj in act.get('roles'): fAct.append(obj.get('value')) fAct.append(developerRole) if len(list(set(fAct) & set(useroles))) > 0: filteredActs.append(act) else: filteredActs.append(act) acts = filteredActs if count is None: count = len(data) self.write(dumps({ 'foundcount': count, 'data': data, 'config': config, 'filters': filters, 'acts': acts, 'classname': classname, 'title': title, 'viewtype': result.get('viewtype'), 'pagination': pagination, 'ispagecount': pagecount, 'ispagesize': ispagesize, 'isfoundcount': isfoundcount, 'subscrible': subscrible, 'isorderby': orderby, 'viewid': result.get('id'), 'checker': checker, 'user':user })) elif method == 'getone': squery = 'SELECT framework."fn_view_getByPath"(%s,%s)' result = [] try: result = yield self.db.execute(squery,(path,'getone',)) except Exception as e: showError(str(e), self) return result = result.fetchone()[0] if not result: self.set_status(500,None) self.write('{"message":"view is not found"}') return #result = result[0] if len(result.get('roles')) > 0: x = False else: x = True for col in result.get('roles'): if col.get('value') in (userdetail.get('roles') or []) and not x: x = True if not x: self.set_status(403,None) self.write('{"message":"access denied"}') return # if exist initial action onLoad actions = result.get('acts') onLoad = None for act in actions: if act.get('type') == 'onLoad': onLoad = act if onLoad: req_url = onLoad.get('act') if 'inputs' in body and onLoad.get('parametrs') is not None: req_url += '?' for param in onLoad.get('parametrs'): req_url += param.get('paramtitle') + '=' + (str(body.get('inputs').get(param.get('paraminput')) or '') ) + '&' if req_url[:4] != 'http': req_url = maindomain + req_url if onLoad.get('actapitype').lower() == 'get': req = HTTPRequest( url = req_url, method = onLoad.get('actapitype'), headers = {'Cookie':'sesid=' + sesid} ) else: req_body = {} if onLoad.get('parametrs') is not None: for param in onLoad.get('parametrs'): req_body[param.get('paramtitle')] = body.get('inputs').get(param.get('paraminput')) req = HTTPRequest( url = req_url, body = dumps(req_body), method = onLoad.get('actapitype'), headers = {'Cookie':'sesid=' + sesid} ) try: response = yield http_client.fetch(req) except HTTPError as e: if e.response and e.response.body: e = e.response.body.decode('utf-8') showError(str(e), self) log(req_url + '_Error_onLoad', str(e)) log(url + '_Error_act', str(onLoad)) return # if exist initial action onLoad data = [] config = result.get('config') filters = result.get('filters') acts = result.get('acts') title = result.get('title') classname = result.get('classname') subscrible = result.get('subscrible') if result.get('viewtype').find('api_') == -1: query = getList(result, body, userdetail=userdetail) acts = result.get('acts') config = result.get('config') squery = query[0] try: data = yield self.db.execute(squery) except Exception as e: showError(str(e), self) log(url + '_Error', str(e)) return data = curtojson([x for x in data],[x[0] for x in data.description]) else: req_url = result.get('tablename') if req_url[:4] != 'http': req_url = maindomain + req_url req = HTTPRequest( url = req_url, body = dumps(body), method = 'POST', headers = {'Cookie':'sesid=' + sesid} ) try: response = yield http_client.fetch(req) except HTTPError as e: if e.response and e.response.body: e = e.response.body.decode('utf-8') showError(str(e), self) log(req_url + ' api error', str(e)) log(url + '_error_act', str(onLoad)) return data = loads(response.body.decode('utf-8')) if data is not None: if 'config' in data and data.get('config') is not None: config = data.get('config') if 'acts' in data and data.get('acts') is not None: acts = data.get('acts') if 'filters' in data and data.get('filters') is not None: filters = data.get('filters') if 'classname' in data: classname = data.get('classname') if 'title' in data: title = data.get('title') if 'subscrible' in data: subscrible = data.get('subscrible') if 'outjson' in data: data = data.get('outjson') else: data = [] useroles = userdetail.get('roles') or [] if acts: filteredActs = [] for act in acts: if 'roles' in act and len(act.get('roles')) > 0: fAct = [] for obj in act.get('roles'): fAct.append(obj.get('value')) fAct.append(developerRole) if len(list(set(fAct) & set(useroles))) > 0: filteredActs.append(act) else: filteredActs.append(act) acts = filteredActs if len(data) > 1: self.set_status(500,None) self.write('{"message":"getone can\'t return more then 1 row"}') return #count = count.fetchone()[0] self.set_status(200,None) self.write(dumps({ 'data': data, 'config': config, 'acts': acts, 'classname': classname, 'table': result.get('tablename'), 'subscrible': subscrible, 'title': title, 'viewtype': result.get('viewtype'), 'id': result.get('id') })) elif method == 'squery': squery = ''' SELECT row_to_json (d) FROM ( SELECT * FROM framework.views where path = %s ) as d ''' result = [] roles = userdetail.get('roles') if int(developerRole) not in roles: self.set_status(403,None) self.write('{"message":"access denied"}') return try: result = yield self.db.execute(squery,(path,)) except Exception as e: showError(str(e), self) log(url + '_Error', str(e)) return result = result.fetchone() if not result: self.set_status(500,None) self.write('{"message":"view is not found"}') return result = result[0] #self.write(dumps(result)) query = getList(result, body, userdetail=userdetail) squery = query[0] self.write(dumps({'squery':squery + '; '})) else: self.set_status(404,None) self.write('{"message":"method not found"}') return