Sindbad~EG File Manager

Current Path : /home/numerotech/conference.numerotech.com/food_kit_scan_badges/core/model/
Upload File :
Current File : //home/numerotech/conference.numerotech.com/food_kit_scan_badges/core/model/AttendanceModel.py

from flask import session
from flask import request, Blueprint, jsonify
from sqlalchemy import create_engine, select, MetaData, Table,text
from sqlalchemy.sql import and_, or_

from core import app

engine = create_engine(app.config['DATABASE_URI'],pool_size=5000,pool_pre_ping=True,pool_recycle=3600)

class AttendanceModel():  
	def __init__(self):
		try:
			self.meta = MetaData()
			self.session_attendance_log  = Table("session_attendance_log",self.meta,autoload_with= engine)  
			
		except Exception as e:
			print(e)


	def getConf_open_closed(self,conf_id,conf_key,current_dt):
		with engine.connect() as conn:
			stmt =text("select * from conference where  conf_id ="+str(conf_id)+" and conf_key ='"+conf_key+"' and '"+current_dt+"' <= conf_end_time ;")
			# stmt =text("select * from conference where  conf_id ="+str(conf_id)+" and conf_key ='"+conf_key+"'  ;")
			result = conn.execute(stmt).first()
			results = dict(result._mapping) if result else None
			if results :
				return results
			else:
				return None
			

	
	def getDays(self,abs_session_table,conf_id) : 
		with engine.connect() as conn:
			stmt = text ("SELECT distinct(display_dt),dt from "+str(abs_session_table)+" where conf_id="+str(conf_id)+"  order by dt asc")
			results = conn.execute(stmt).all()
			result = [dict(r._mapping) for r in results] if results else None
			if result : 
				return result
			else:
				return None 

	def getHalls(self,abs_session_table,conf_id,conf_date) : 
		with engine.connect() as conn:
			stmt = text ("SELECT distinct(hall),hall_id,dt,display_dt from "+str(abs_session_table)+" where conf_id="+str(conf_id)+" and dt= '"+str(conf_date)+"' order by hall_id ;")
			results = conn.execute(stmt).all()
			result = [dict(r._mapping) for r in results] if results else None
			if result : 
				return result
			else:
				return None 

	def getSessionName(self,abs_session_table,conf_id,hall_id,conf_date) : 
		with engine.connect() as conn:
			stmt = text ("SELECT * from "+str(abs_session_table)+" where conf_id="+str(conf_id)+" and  hall_id="+str(hall_id)+" and dt= '"+str(conf_date)+"' and abs_session_id is not null order by starts_by")
			results = conn.execute(stmt).all()
			result = [dict(r._mapping) for r in results] if results else None
			if result : 
				return result
			else:
				return None 

	def get_session(self,conf_id,asession_id):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor     = connection.cursor()
			date       = None
			hall       = None
			cursor.callproc("usp_get_abstract_session_spakers",[conf_id,date,hall,asession_id])
			while 1:
				names = [c[0] for c in cursor.description]
				set_ = []
				while 1:
					row_raw = cursor.fetchone()
					if row_raw is None:
						break
					row = dict(zip(names, row_raw))
					set_.append(row)
				sets.append(list(set_))
				if cursor.nextset() is None:
					break
				if cursor.description is None:
					break
		finally:
			connection.close()
		return sets

	def getCurrentSession(self,abs_session_table,conf_id,current_dt,hall_id,conf_date):
		with engine.connect() as conn:
			stmt = text("select * from "+str(abs_session_table)+" where conf_id ="+str(conf_id)+" and hall_id='"+str(hall_id)+"' and dt= '"+str(conf_date)+"' and '"+str(current_dt)+"' between starts_by and ends_by ;")
			results = conn.execute(stmt).first()
			return results._mapping if results else None
			

	   
 
	def usp_get_session_query(self,conf_id,hall_id,conf_date,current_dt):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_get_session_query",[conf_id,hall_id,conf_date,current_dt])
			while 1:
				#(column_name, type_, ignore_, ignore_, ignore_, null_ok, column_flags)
				names = [c[0] for c in cursor.description]
				set_ = []
				while 1:
					row_raw = cursor.fetchone()
					if row_raw is None:
						break
						
					row = dict(zip(names, row_raw))
					set_.append(row)
	 
				sets.append(list(set_))
	 
				if cursor.nextset() is None:
					break
	 
				# nextset() doesn't seem to be sufficiant to tell the end.
				if cursor.description is None:
					break
		finally:
			# Return the connection to the pool (won't actually close).
			connection.close()
			
		return sets 

	def getsessiondata_session_id(self,asession_id): 
		with engine.connect() as conn:
			stmt = text(f"select * from  abs_session_types  where abs_session_id={asession_id};")
			results = conn.execute(stmt).first()
			result = dict(results._mapping)  if results else None
			if result : 
				return result
			else:
				return None 

	def update_attendance(self,speaker_data,conf_schema):
		with engine.connect() as conn:
			stmt = 'UPDATE '+str(conf_schema)+'.abs_topics SET '
			stmt1 = 'UPDATE '+str(conf_schema)+'.abs_judges SET '
			keyname = ["is_present"]
			judge_pkid = []
			author_pkid = []
			for j in keyname:
				stmt = stmt + j +" = case "
				stmt1 = stmt1 + j +" = case "
				for i in speaker_data:
					if int(i["is_judge"]) == 0:
						if i[j]:
							stmt = stmt + ' When topic_id = ' + str(i["pk_id"]) + " then " + i[j]
							author_pkid.append(i["pk_id"])
					else:
						if i[j]:
							stmt1 = stmt1 + ' When judges_id = ' + str(i["pk_id"]) + " then " + i[j]
							judge_pkid.append(i["pk_id"])
				stmt = stmt + ' END, '
				stmt1 = stmt1 + ' END, '
			stmt = stmt[:-2]
			stmt1 = stmt1[:-2]
			author_pkid =  ','.join(str(e) for e in author_pkid)
			judge_pkid =  ','.join(str(e) for e in judge_pkid)
			stmt = stmt + ' Where topic_id in (' +str(author_pkid)+' );'
			stmt1 = stmt1 + ' Where judges_id in (' +str(judge_pkid)+' );'
			if len(judge_pkid)!=0:
				result = conn.execute(text(stmt1))
			if len(author_pkid)!=0:
				result = conn.execute(text(stmt))
			conn.commit()
			return "Success"

	def update_session(self,asession_id,datetime,remarks,conf_schema):
		with engine.connect() as conn:
			stmt = text("update {}.abs_session_types set attendance_on = '{}',remarks = '{}' where abs_session_id = {}".format(conf_schema,datetime,remarks,asession_id))
			results = conn.execute(stmt)
			conn.commit()
			return 'success'

	def insert_session_attendance_log(self,data):
		with engine.connect() as conn:
			result = conn.execute(self.session_attendance_log.insert(), data)
			conn.commit()
			return 'success'

	def generate_certificates(self,conf_id,session_id):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_generate_certificate_by_session",[conf_id,session_id])
		 
		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 "success"
		else :
			return "fail"
		
	def get_session_volunteer(self,asession_id): 
		with engine.connect() as conn:
			stmt = text (f"select * from session_attendance_log where abs_session_id = {asession_id} order by loged_on")
			results = conn.execute(stmt).all()
			result = [dict(r._mapping) for r in results] if results else None
			if result : 
				return result
			else:
				return None 

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