Sindbad~EG File Manager

Current Path : /home/numerotech/scans.numerotech.com/scan_v1/core/model/
Upload File :
Current File : //home/numerotech/scans.numerotech.com/scan_v1/core/model/KitModel.py

from sqlalchemy import create_engine, MetaData, Table, insert, select,update,delete,text
from sqlalchemy.sql import and_, or_
from core import app
import json
import datetime
from datetime import timedelta,date,datetime


engine    = create_engine(app.config['DATABASE_URI'],pool_recycle=3600,future=True)
engine_fk = create_engine(app.config['DATABASE_URI_FK'],pool_recycle=3600,future=True)

class KitModel():
	def __init__(self):
		try:
			self.meta        = MetaData()
			self.kc_sessions = Table("kc_sessions", self.meta,  autoload_with=engine_fk)
		except Exception as e:
			print(e)

	def getDataBySessionKey(self,session_key,current_dt,conf_id,conf_key):
		with engine_fk.connect() as conn:
			stmt = text("SELECT s.* from kc_sessions s where s.session_key = "+str(session_key)+ " and s.conf_id = "+ str(conf_id)+ " and conf_key = '"+conf_key +"';")
			result = conn.execute(stmt).one_or_none()
			if result :
				return dict(result._mapping)
			else:
				return None 

	def insertkcScannedData1(self,session_id,delegate_no,del_table_name,kc_del_session_table_name,kc_session_entry_table_name):
		connection = engine_fk.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_save_kc_scanned_data",[session_id,delegate_no,del_table_name,kc_del_session_table_name,kc_session_entry_table_name])
		if cursor.description :
			columns = [column[0] for column in cursor.description]
			results = []
			for row in cursor.fetchall():
				results.append(dict(zip(columns, row)))
			cursor.close()
			connection.commit()
			if results :
				return results
			else :
				return None
		else :
			cursor.close()
			connection.commit()
			return None
	
	#single result set store proc
	def getKITReport(self,session_id,conf_id,conf_key,del_table_name,kc_session_entry_table_name):
		connection = engine_fk.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_get_kc_reports",[session_id,conf_id,conf_key,del_table_name,kc_session_entry_table_name])
		if cursor.description :
			columns = [column[0] for column in cursor.description]
			results = []
			for row in cursor.fetchall():
				results.append(dict(zip(columns, row)))
			cursor.close()
			connection.commit()
			if results :
				return results
			else :
				return None
		else :
			cursor.close()
			connection.commit()
			return None

	# check session is open
	def checkSessionIsOpen(self,session_id,current_dt):
		with engine_fk.connect() as conn:
	# 		stmt = text("SELECT s.* from kc_sessions s where s.session_id = "+str(session_id)+" and '"+current_dt+"'  between s.start_time and s.end_time;")
			stmt = text("select  * from kc_sessions where session_id  = "+str(session_id)+" ;")
			result = conn.execute(stmt).one_or_none()
			if result :
				return dict(result._mapping)
			else:
				return None		

	# GET SCANNED RECORDS FOR DOWNLOAD REPORT #single result set store proc 
	def getKITReportDownload(self,session_id,conf_id,kc_session_entry_table_name,role):
		connection = engine_fk.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_get_kc_download_report",[session_id,conf_id,kc_session_entry_table_name,role])
		if cursor.description :
			columns = [column[0] for column in cursor.description]
			results = []
			for row in cursor.fetchall():
				results.append(dict(zip(columns, row)))
			cursor.close()
			connection.commit()
			if results :
				return results
			else :
				return None
		else :
			cursor.close()
			connection.commit()
			return None	


	def insert_kc_token_data(self,insert_stmt):
		with engine_fk.connect() as conn:
			try:
				stmt = text(insert_stmt)
				results = conn.execute(stmt)
				conn.commit()
				return "success"
			except Exception as e:
				return str(e)
	
	def getDataBySessionKeyFrontEnd(self,session_key,current_dt,conf_id,conf_key):
		with engine_fk.connect() as conn:
			stmt = text("SELECT s.* from kc_sessions s where s.session_key = "+str(session_key)+ " and s.conf_id = "+ str(conf_id)+ " and conf_key = '"+conf_key +"' and '"+current_dt+"'  between s.start_time and s.end_time;")
			result = conn.execute(stmt).one_or_none()
			if result :
				return dict(result._mapping)
			else:
				return None 
		
	#single result set store proc
	def getKITReportWithName(self,session_id,conf_id,conf_key,del_table_name,kc_session_entry_table_name):
		connection = engine_fk.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_get_kc_reports_with_name",[session_id,conf_id,conf_key,del_table_name,kc_session_entry_table_name])
		if cursor.description :
			columns = [column[0] for column in cursor.description]
			results = []
			for row in cursor.fetchall():
				results.append(dict(zip(columns, row)))
			cursor.close()
			connection.commit()
			if results :
				return results
			else :
				return None
		else :
			cursor.close()
			connection.commit()
			return None	
			
	def getKcScanErrorLogs(self,conf_id,conf_key,curr_date,is_column_only):
		with engine.connect() as conn :
			if is_column_only   == 0 :
				stmt = text("select id,url,message,host_url,scan_updated_at,date_format(created_at , '%d-%m-%Y,%H:%i:%s') as created_on,conf_id from numerotech_primary_db.logs where url like '%/post_kc_scan_data?conf_id="+str(conf_id)+"%' "
					+ " and host_url = 'conferencescan.numerotech.com' and date(created_at) = '"+curr_date+"' "
					+ " and scan_updated_at is null  order by id desc  limit 50 ;")	

				result  = conn.execute(stmt).all()
				results = [dict(r._mapping) for r in result] if result else None
				if results :
					return results if results else None
			else :
				stmt_2   = text("select conf_id,del_table_name,fc_del_session_table_name,kc_del_session_table_name,kc_session_entry_table_name,fc_session_entry_table_name  from conference where conf_id ="+str(conf_id)+" and conf_key = '"+conf_key+"'  limit 1;")
				result_1 = conn.execute(stmt_2).one_or_none()
				result_1 = dict(result_1._mapping) if result_1 else None
				return result_1


	def saveKCScanDataFromLogs(self,ids, data):
		with engine.connect() as conn:
			stmt      = self.logs.update().where(self.logs.c.id.in_(ids)).values(data)
			restult_1 = conn.execute(stmt)
			conn.commit()
		return "success"		

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists