Sindbad~EG File Manager

Current Path : /home/numerotech/imagepoll.numerotech.com/common_imagepoll_v1/core/model/
Upload File :
Current File : //home/numerotech/imagepoll.numerotech.com/common_imagepoll_v1/core/model/BoModel.py

from sqlalchemy import create_engine, MetaData, Table, insert, select,update,delete,text
from sqlalchemy.sql import and_, or_
from .. import Cryptography,Auth
from core import app
import json

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

class BoModel():
	def __init__(self):
		try:
			self.meta = MetaData()
			self.users        = Table("users", self.meta, autoload_with=engine)
			self.image_poll_session = Table("image_poll_session", self.meta, autoload_with=engine_app)
			self.image_poll = Table("image_poll", self.meta, autoload_with=engine_app)
			
		except Exception as e:
			print(e)

	def get_bo_dashboard_count(self,society_id):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("numerotech_imagepoll_db.usp_get_bo_imagepoll_dashboard_count",[society_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 results
		else :
			return None

	def get_bo_selected_and_submitted_data(self,in_status_id,society_id):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("numerotech_imagepoll_db.usp_data_for_image_submission",[in_status_id,society_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 results
		else :
			return None

	def GetSession(self,society_id):
		with engine_app.connect() as conn:
			stmt   = text("select  ps.* ,count(p.session_id) as submission_selected_count  from numerotech_imagepoll_db.image_poll_session ps left join numerotech_imagepoll_db.image_poll p on ps.session_id = p.session_id where ps.society_id= :society_id group by session_id order by ps.start_date desc;")
			results = conn.execute(stmt.bindparams(society_id=society_id)).all()
			return [dict(r._mapping) for r in results] if results else None

	def GetImages(self,society_id):
		with engine_app.connect() as conn:
			# stmt   = text("select   imp.*, ips.* from numerotech_imagepoll_db.image_poll imp left join numerotech_imagepoll_db.image_poll_session ips on ips.session_id=imp.session_id where imp.society_id= :society_id order by imp.img_poll_id desc;")
			stmt=text("select   imp.*, ips.* from numerotech_imagepoll_db.image_poll imp left join numerotech_imagepoll_db.image_poll_session ips on ips.session_id=imp.session_id where imp.society_id= :society_id order by ifnull(imp.abs_no,null) desc ;")
			results = conn.execute(stmt.bindparams(society_id=society_id)).all()
			return [dict(r._mapping) for r in results] if results else None

	def GetSessionData(self,session_id,society_id):
		with engine_app.connect() as conn:
			if society_id==18:
				stmt   = text("select * from numerotech_imagepoll_db.image_poll_session where society_id = {}".format(society_id))
			else:
				stmt   = text("select * from numerotech_imagepoll_db.image_poll_session where session_id = {}  and society_id = {}".format(session_id,society_id))
			result = conn.execute(stmt).first()
			if result :
				return dict(result._mapping)
			else:
				return None

	# def insert_session(self,data):
	# 	conn = engine.connect()
	# 	result = conn.execute(self.image_poll_session.insert(), data)
	# 	# stmt = text("insert into image_poll_session (session_key,start_date,end_date,is_active,society_id,session_name) values('{}','{}','{}',{},{},'{}')".format(data["session_key"],data["start_date"],data["end_date"],data["is_active"],data["society_id"],data["session_name"]))
	# 	# results = conn.execute(stmt)
	# 	# print(stmt)
	# 	# result = conn.execute(self.img_poll_rate.insert(), data)
	# 	print(result)
	# 	conn.close()
	# 	return result

	def get_session_name_count(self,session_name,session_id,society_id):
		with engine_app.connect() as conn:
			# stmt   = text("select * from numerotech_imagepoll_db.image_poll_session where session_name = '{}' and session_id !={}  and society_id = {}".format(session_name,session_id,society_id))
			stmt=text("select count(*) as sess_count from numerotech_imagepoll_db.image_poll_session where session_name = '"+str(session_name)+"' and society_id="+str(society_id)+";")
			result = conn.execute(stmt).first()
			if result :
				return result
			else:
				return None 

	def get_session_key_count(self,session_key,session_id,society_id):
		with engine_app.connect() as conn:
			# stmt   = text("select * from numerotech_imagepoll_db.image_poll_session where session_key = '{}' and session_id !={}  and society_id = {}".format(session_key,session_id,society_id))
			stmt=text("select count(*) as sess_count from numerotech_imagepoll_db.image_poll_session where session_key = '"+str(session_key)+"' and society_id="+str(society_id)+";")
			result = conn.execute(stmt).first()
			if result :
				return result
			else:
				return None 

	def update_session_data(self,session_id,data):
		with engine_app.connect() as conn:
			stmt = self.image_poll_session.update().where(self.image_poll_session.c.session_id.in_([session_id])).values(data)
			result = conn.execute(stmt)
			conn.commit()
			return result

	def insert_session(self,data):
		with engine_app.connect() as conn:
			result = conn.execute(self.image_poll_session.insert(), data)
			conn.commit()
			pk_id = result.inserted_primary_key[0] if result.inserted_primary_key[0] else None
			return pk_id

	def GetImage(self,session_id,society_id):
		with engine_app.connect() as conn:
			stmt   = text("select * from numerotech_imagepoll_db.image_poll  where session_id ={}  and status_id=2  and society_id = {}".format(session_id,society_id))
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None

	def GetFinalImage(self,session_id,society_id):
		with engine_app.connect() as conn:
			stmt   = text("select * from numerotech_imagepoll_db.image_poll  where status_id=2 and final_session_id={}  and society_id = {}".format(session_id,society_id))
			# print(stmt)
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None

	def getSessions(self,society_id):
		with engine_app.connect() as conn:
			stmt   = text("select * from numerotech_imagepoll_db.image_poll where status_id=1  and society_id = {}".format(society_id))
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None

	def getFinalSessions(self,society_id):
		with engine_app.connect() as conn:
# 			if society_id==18:
# 				stmt   = text("select * from numerotech_imagepoll_db.image_poll where status_id=1 and society_id = {}".format(society_id))
# 			else:
			stmt   = text("select * from numerotech_imagepoll_db.image_poll where status_id=2 and final_session_id is null  and society_id = {}".format(society_id))
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None


	def update_status_to_selected(self,status_id,session_id,image_poll_ids,current_dt):
		with engine_app.connect() as conn:
			stmt = text("update numerotech_imagepoll_db.image_poll set status_id = {} , session_id={} , selected_at='{}' where img_poll_id in ({});".format(status_id,session_id,current_dt,image_poll_ids))
			result = conn.execute(stmt)
			conn.commit()
			return result

	def update_final_selected(self,session_id,image_poll_ids,current_dt):
		with engine_app.connect() as conn:
			stmt = text("update numerotech_imagepoll_db.image_poll set final_session_id={} where img_poll_id in ({});".format(session_id,image_poll_ids))
			print(stmt)
			result = conn.execute(stmt)
			conn.commit()
			return result


	def remove_selected_to_submitted(self,img_poll_id,society_id):
		with engine_app.connect() as conn:
			stmt = text("update numerotech_imagepoll_db.image_poll set status_id = 1, session_id= null where img_poll_id in ({});".format(img_poll_id,society_id))
			result = conn.execute(stmt)
			conn.commit()
			return result

	def remove_final_selected_to_submitted(self,img_poll_id,society_id):
		with engine_app.connect() as conn:
			stmt = text("update numerotech_imagepoll_db.image_poll set final_session_id= null, final_order_no=null where img_poll_id in ({});".format(img_poll_id,society_id))
			result = conn.execute(stmt)
			conn.commit()
			return result

	def GetImagePoll(self,society_id):
		with engine.connect() as conn:
			stmt   = text("select * from numerotech_imagepoll_db.image_poll where  status_id=2  and society_id = {}".format(society_id))
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None

	def update_orderby(self,result,img_poll_id):
		with engine_app.connect() as conn:
			stmt = 'UPDATE image_poll SET '
			keyname = ["order_by"]
			for j in keyname:
				stmt = stmt + j +" = case "
				for i in result:
					stmt = stmt + ' When img_poll_id = ' + str(i["img_poll_id"]) + " then '" + i[j] + "'"
				stmt = stmt + ' END, '
			stmt = stmt[:-2]
			stmt =text(stmt + ' Where img_poll_id in (' +str(img_poll_id)+' )')
			result = conn.execute(stmt)
			conn.commit()
			return result

	
	def final_update_orderno(self,result,img_poll_id):
		with engine_app.connect() as conn:
			stmt = 'UPDATE image_poll SET '
			keyname = ["final_order_no"]
			for j in keyname:
				stmt = stmt + j +" = case "
				for i in result:
					stmt = stmt + ' When img_poll_id = ' + str(i["img_poll_id"]) + " then '" + i[j] + "'"
				stmt = stmt + ' END, '
			stmt = stmt[:-2]
			stmt =text(stmt + ' Where img_poll_id in (' +str(img_poll_id)+' )')
			result = conn.execute(stmt)
			conn.commit()
			return result

	
	def GetAuthorData(self,society_id,name):
		with engine.connect() as conn:
			stmt   = text("select * from users where society_id="+str(society_id)+" and (full_name like '%{n}%' or membership_no like '%{n}%' or email like '%{n}%');".format(n=name))
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None

	def UpdateUserDatas(self,data,img_poll_id,society_id):
		with engine_app.connect() as conn:
			stmt_1 = self.image_poll.update().where(self.image_poll.c.img_poll_id.in_([img_poll_id])).values(data)
			results = conn.execute(stmt_1)
			conn.commit()
			stmt=text("select * from image_poll where society_id="+str(society_id)+" and img_poll_id="+str(img_poll_id)+";")
			result = conn.execute(stmt).first()
			if result :
				return dict(result._mapping)
			else:
				return None

	def Insert_Image_poll_data(self,data_1,society_id):
		with engine_app.connect() as conn:
			result = conn.execute(self.image_poll.insert(), data_1)
			# result = conn.execute(stmt)
			conn.commit()
			pk_id = result.inserted_primary_key[0] if result.inserted_primary_key[0] else None
			return pk_id

	def Check_Data(self,img_poll_id):
		with engine.connect() as conn:
			stmt=text("select * from numerotech_imagepoll_db.image_poll im left join users u on u.user_id=im.user_id where img_poll_id="+str(img_poll_id)+" ;")
			result = conn.execute(stmt).first()
			if result :
				return dict(result._mapping)
			else:
				return None

# GET INCOMPLETE DATA

	def GetIncompleteImage(self,society_id,status_id):
		with engine_app.connect() as conn:
			stmt   = text("select * from numerotech_imagepoll_db.image_poll where status_id="+str(status_id)+"  and society_id = {}".format(society_id))
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None

	def DeleteIncompleteData(self,img_poll_id):
		with engine_app.connect() as conn:
			stmt   = text("DELETE FROM numerotech_imagepoll_db.image_poll WHERE img_poll_id="+str(img_poll_id)+";")
			result = conn.execute(stmt)
			conn.commit()
			return result
			
	def GetSessionDetail(self,society_id):
		with engine_app.connect() as conn:
			stmt   = text("select * from numerotech_imagepoll_db.image_poll_session where society_id = {}".format(society_id))
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None

	def usp_get_avg_rate_img_poll(self,session_id,date):
		sets = []
		try:
			connection = engine_app.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("numerotech_imagepoll_db.usp_get_avg_rate_img_poll",[session_id,date])
			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.commit()
			connection.close()
		return sets[0]


	def usp_get_img_poll_report(self,session_id):
		sets = []
		try:
			connection = engine_app.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("numerotech_imagepoll_db.usp_get_img_poll_report",[session_id])
			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.commit()
			connection.close()
		return sets

	def GetMaiSessionData(self,date):
		with engine_app.connect() as conn:
			stmt=text("select * from numerotech_imagepoll_db.image_poll_session where DATE(end_date)= date('"+str(date)+"') - INTERVAL 1 DAY and is_active=1 order by end_date desc ;")
			# print(stmt)
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None

	


	def GetSocietyData(self,society_id,session_id):
		with engine.connect() as conn:
			stmt=text("select * from numerotech_imagepoll_db.image_poll_session ims  left join societies s on s.society_id=ims.society_id left join society_applications a on a.society_id=ims.society_id inner join mail_setting ms on ms.mail_setting_id=a.mail_setting_id where s.society_id="+str(society_id)+" and ms.is_active=1 and a.app_type_id=7 and ims.session_id="+str(session_id)+";")
			result = conn.execute(stmt).first()
			if result :
				return dict(result._mapping)
			else:
				return None

	def GetSocietyDataCount(self,society_id):
		with engine.connect() as conn:
# 			stmt=text("select * from societies s inner join mail_setting ms where s.society_id="+str(society_id)+" and ms.is_active=1;")
			stmt=text("select * from societies s inner join society_applications so on so.society_id=s.society_id inner join mail_setting ms on ms.mail_setting_id = so.mail_setting_id where s.society_id="+str(society_id)+" and so.app_type_id=7 and ms.is_active=1;")
			result = conn.execute(stmt).first()
			if result :
				return dict(result._mapping)
			else:
				return None

	def auto_mail_templates(self,template_name):
		with engine.connect() as conn:
			stmt = text("SELECT * FROM mail_templates where template_name='"+str(template_name)+"' and app_type_id=7 and is_active=1;")
			result = conn.execute(stmt).first()
			if result :
				return dict(result._mapping)
			else:
				return None

	def get_author_by_img_id(self,society_id,img_poll_id):
		with engine_app.connect() as conn:
			stmt   = text("select * from image_poll where society_id= :society_id and img_poll_id= :img_poll_id ;")
			result = conn.execute(stmt.bindparams(society_id=society_id,img_poll_id=img_poll_id)).first()
			if result :
				return dict(result._mapping)
			else:
				return None

	def get_next(self,img_poll_id,society_id):
		with engine.connect() as conn:
			stmt=text("select * from numerotech_imagepoll_db.image_poll where img_poll_id < :img_poll_id and society_id= :society_id order by img_poll_id desc limit 0,1 ;")
			result = conn.execute(stmt.bindparams(img_poll_id=img_poll_id,society_id=society_id)).first()
			if result :
				return dict(result._mapping)
			else:
				return None

	def get_search_data(self,search_text,in_status_id,society_id):
		sets = []
		try:
			connection = engine_app.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("numerotech_imagepoll_db.usp_search_data_for_image_submission",[search_text,in_status_id,society_id])
			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.commit()
			connection.close()
		return sets

	def UpdateSessionDetails(self,session_id,data):
		with engine_app.connect() as conn:
			stmt_1 = self.image_poll_session.update().where(self.image_poll_session.c.session_id.in_([session_id])).values(data)
			results = conn.execute(stmt_1)
			conn.commit()
			return results

	def GetPollCountMail(self,session_id):
		sets = []
		try:
			connection = engine_app.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("numerotech_imagepoll_db.usp_get_total_poll_count",[session_id])
			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.commit()
			connection.close()
		return sets[0]

	def GetMailSubmissionCount(self,society_id,pre_date):
		sets = []
		try:
			connection = engine_app.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("numerotech_imagepoll_db.usp_get_submission_count",[society_id,pre_date])
			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.commit()
			connection.close()
		return sets[0]

	def GetSessionPollData(self,society_id):
		with engine_app.connect() as conn:
			stmt=text("SELECT * FROM numerotech_imagepoll_db.image_poll_session where current_date() > date(start_date) and current_date() <= date(end_date) and is_active=1 and society_id="+str(society_id)+";")
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None
			
	def GetTrigerMailData(self,society_id):
		with engine.connect() as conn:
			stmt=text(f"select * from trigger_daily_reports_mails where society_id={society_id} and app_type_id=7 and is_active=1 ;")
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None
	
	def GetSessionReport(self,session_id,date):
		with engine_app.connect() as conn:
			stmt=text(f"SELECT * FROM numerotech_imagepoll_db.image_poll_session where date(end_date)<'{date}' and session_id={session_id};")
			print(stmt)
			results=conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None
			
	def GetUserPollId(self,society_id,session_id):
		with engine.connect() as conn:
			stmt=text("SELECT concat(u.prefix,u.full_name) as full_name,u.email,u.mobile,case when confirm_on then 'completed' else 'incompleted' end as status FROM numerotech_imagepoll_db.img_poll_rate ir left join users u on u.user_id=ir.user_id where session_id="+str(session_id)+" group by ir.user_id order by u.full_name;")
			results=conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None
	
	def usp_get_img_poll_report_html(self,session_id):
		sets = []
		try:
			connection = engine_app.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("numerotech_imagepoll_db.usp_get_img_poll_report_html",[session_id])
			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.commit()
			connection.close()
		return sets

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