Sindbad~EG File Manager

Current Path : /proc/self/root/home/numerotech/profile.numerotech.com/common_profile_v1/core/model/
Upload File :
Current File : //proc/self/root/home/numerotech/profile.numerotech.com/common_profile_v1/core/model/UserModel.py

from sqlalchemy import create_engine, MetaData, Table, insert, select,update,delete,text
from sqlalchemy.sql import and_, or_
from core import app
from core.library.helper import Helper
import json
import sqlite3 
import pandas as pd

engine = create_engine(app.config['DATABASE_URI'],pool_recycle=3600)
engine_sqlite = create_engine('sqlite:///sqlite_database.db')



class UserModel():
	def __init__(self):
		try:
			self.meta = MetaData()
			self.users = Table("users", self.meta, autoload_with=engine)
			self.user_additional = Table("user_additional", self.meta, autoload_with=engine)
			self.user_attachments = Table("user_attachments", self.meta, autoload_with=engine)
			self.delegates = Table("delegates", self.meta, autoload_with=engine)
			self.user_participation = Table("user_participation", self.meta, autoload_with=engine)
			self.users_participate_sections = Table("users_participate_sections", self.meta, autoload_with=engine)
		except Exception as e:
			print(e)

	def get_user_by_email(self,email,society_id,profile_member_type):
		with engine.connect() as conn:
			stmt    = f"select * from users where email = '{email}' and society_id = {society_id} "
			if profile_member_type:
				stmt    += f" and member_type_id in ({profile_member_type})"
				
			# stmt = ("select * from users where email = '{}' and society_id = {} ;".format(email,society_id))
			results = conn.execute(text(stmt)).first()
			return  dict(results._mapping) if results else None

	
	def update_member(self,user_id,data):
		with engine.connect() as conn:
			stmt   = self.users.update().where(self.users.c.user_id.in_([user_id])).values(data)
			result = conn.execute(stmt)
			conn.commit()
			conn.close()
			return result
		
#-- User Additional---		
	def update_user_additional(self,user_id,data3):
		with engine.connect() as conn:
			stmt   = self.user_additional.update().where(self.user_additional.c.user_id.in_([user_id])).values(data3)
			result = conn.execute(stmt)
			conn.commit()
			conn.close()
			return result
		
	def insert_user_additional(self,data3):
		with engine.connect() as conn:
			result = conn.execute(self.user_additional.insert(), data3)
			conn.commit()
			conn.close()
			return result
		
	def get_user_additional(self,user_id):
		with engine.connect() as conn:
			stmt    = text("select * from user_additional where user_id = '{}' ;".format(user_id))
			result = conn.execute(stmt).first()
			conn.close()
			return  dict(result._mapping) if result else None
# --------
	
	def update_delegate_data(self,user_id,data):
		with engine.connect() as conn:
			# stmt = text('update delegates_boa23 set full_name = "{}{}" ,mobile = {},city = "{}",mc_number = {},state = (select state_name from states where state_id = {}) where user_id = {}'.format(data["prefix"],data["full_name"],data["mobile"],data["city"],data["mc_number"],data["state_id"],user_id) )
			stmt1 = self.delegates.update().where(self.delegates.c.user_id.in_([user_id])).values(data)
			# result = conn.execute(stmt)
			result = conn.execute(stmt1)
			conn.commit()
			conn.close()
			return result
		
	def insert_user_participation(self,data2):
		with engine.connect() as conn:
			result = conn.execute(self.user_participation.insert(), data2)
			conn.commit()
			conn.close()
			return result

	def get_user_data_by_uuid(self,uuid):
		with engine.connect() as conn:
			stmt    = text("select * from users where user_uuid = '{}' ;".format(uuid))
			result = conn.execute(stmt).first()
			conn.close()
			return  dict(result._mapping) if result else None

	def get_society_data(self,society_id):
		with engine.connect() as conn:
			stmt    = text("select app_id,app_type_id,app_title,app_host,sa.app_url,s.society_id,sa.is_active,m.driver,m.domain,m.secret_key,sa.ms_approved_status_id,index_content,profile_content,society_name,society_title,email_logo,society_style,society_key,e_support_mail,society_intial,e_backup_mails,e_from_mail_id,e_reply_mail_id,profile_member_type,s.secretary_name,s.secretary_mobile,s.e_secretary_mail_id,s.profile_lock_at,mat.attach_type_id,mat.attach_type,s.e_from_mail_name,s.profile_lock_message,s.clarity_script,s.profile_validate_from from society_applications sa "+
						" inner join societies s on s.society_id=sa.society_id"+
						" left join m_attachment_type mat on mat.society_id=sa.society_id"+
						" inner join mail_setting m on m.mail_setting_id=sa.mail_setting_id"+
						" where sa.app_type_id=4 and mat.attach_type='Photograph' and s.society_id={}".format(society_id))
			
			results = conn.execute(stmt).first()
			conn.close()
			if results : 
				return dict(results._mapping)
			else:
				return None		
			
	# def get_society_by_host(self,society_host):
	# 	with engine.connect() as conn:
	# 		stmt    = text("select app_id,app_type_id,app_title,app_host,s.society_id,sa.is_active,index_content,profile_content,society_name,society_title,email_logo,society_style,society_key,e_support_mail,society_intial,e_backup_mails,e_from_mail_id,e_reply_mail_id,profile_member_type,s.secretary_name,s.secretary_mobile,s.e_secretary_mail_id,s.clarity_script,m.driver,m.domain,m.secret_key from society_applications sa "+
	# 					" inner join societies s on s.society_id=sa.society_id"+
	# 					" inner join mail_setting m on m.mail_setting_id=sa.mail_setting_id"+
	# 					" where sa.app_type_id=4 and sa.app_host='{}'".format(society_host))
	# 		results = conn.execute(stmt).first()
	# 		if results : 
	# 			return dict(results._mapping)
	# 		else:
	# 			return None

	def get_society_contents(self,society_id):
		with engine.connect() as conn:
			stmt    = str(f"select sa.index_content,sa.profile_content from society_applications sa where society_id = {society_id} and sa.app_type_id=4")
			result = conn.execute(text(stmt)).first()
			conn.close()
			return  dict(result._mapping) if result else None
			
			
	def get_society_by_id(self,society_id):
		with engine_sqlite.connect() as conn:
			stmt    = str(f"select photograph_attach_type_id from societies where society_id = {society_id} ")
			result = conn.execute(text(stmt)).first()
			conn.close()
			return  dict(result._mapping) if result else None

			
	def get_society(self,society_id,society_key,host=None):
		with engine_sqlite.connect() as conn:
			stmt    = str("select app_id,app_type_id,app_title,app_host,s.society_id,sa.is_active,society_name,society_title,email_logo,society_style,society_key,e_support_mail,society_intial,e_backup_mails,e_from_mail_id,e_reply_mail_id,profile_member_type,aws_bucket,sa.mail_setting_id,s.profile_lock_at,s.profile_lock_message,m.* ,s.e_from_mail_name,sa.app_url,s.secretary_name,s.secretary_mobile,s.e_secretary_mail_id,s.favicon_url,sa.ms_approved_status_id,sa.index_content,sa.profile_content,mat.attach_type_id,mat.attach_type, s.profile_validate_from,s.aws_url from society_applications sa "+
						" inner join societies s on s.society_id=sa.society_id"+
						" left join m_attachment_type mat on mat.society_id=sa.society_id"+
						" inner join mail_setting m on m.mail_setting_id=sa.mail_setting_id"+
						" where sa.app_type_id=4 and mat.attach_type='Photograph' ")
			# if int(society_id)==1:
			# 	stmt = stmt   + "and mat.attach_type='Fellowship certificate for Life Membership'"
			if host:
				stmt 	=  stmt + f"  and sa.app_host='{host}'"
			if society_id:
				stmt 	=  stmt + f"  and s.society_id={society_id}"
			if society_key:
				stmt 	=  stmt + f"  and s.society_key='{society_key}'"
			
			result = conn.execute(text(stmt)).first()
			conn.close()
			return  dict(result._mapping) if result else None

	

	def update_attachemant(self,user_id,data,datetime):
		connection  = engine.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("usp_attachement_update",[user_id,data["attach_type_id"],data["attach_path"],data["attach_file_name"],datetime])
		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()
			connection.close()
		return "success"


	def get_otp_random(self,user_id):
		with engine.connect() as conn:
			stmt    = text(f"select otp from users where user_id = {user_id} ")
			results = conn.execute(stmt).first()
			conn.close()
			# return  dict(results._mapping) if results else None
			if results : 
				return dict(results._mapping)
			else:
				return None

	def get_member_attachment(self,user_id,attch_type_id):
# 		print(user_id)
		with engine.connect() as conn:
			stmt    = text("select u.*,ua.attach_path,ua.attach_file_name,c.country_name,com.country_name as com_country_name,s.state_name,mcs.state_name as mc_state, up.is_speaker, up.is_moderator,up.participation_id,uat.place_of_practice,uat.district_of_practice,uat.is_government_institute,uat.is_teaching_institute,uat.year_of_passing,uat.institution,uat.year_of_longterm,uat.institute_of_longterm,uat.place_of_longterm,uat.year_of_shortterm,uat.institute_of_shortterm,uat.place_of_shortterm,uat.year_of_experience,uat.year_of_experience_post_fellowship,uat.user_topics,uat.additional_info,uat.longterm_specialities,uat.shortterm_specialities,coms.state_name as com_state from users u left join user_attachments ua on u.user_id = ua.user_id and attach_type_id = {} left join countries c on u.country_id = c.country_id left join countries com on u.country_id = com.country_id left join states s on u.state_id = s.state_id left join states mcs on u.mc_state_id = mcs.state_id left join states coms on u.com_state_id = coms.state_id left join user_participation up on up.user_id = u.user_id left join user_additional uat on uat.user_id = u.user_id  where u.user_id = {} ".format(attch_type_id,user_id))
			results = conn.execute(stmt).first()
			conn.close()
			return  dict(results._mapping) if results else None
			# if results : 
			# 	return dict(results._mapping)
			# else:
			# 	return None

	def get_member(self,user_id):
		with engine.connect() as conn:
			stmt    = text("select *  from users  where user_id = {} ".format(user_id))
			results = conn.execute(stmt).first()
			conn.close()
			# return  dict(results._mapping) if results else None
			if results : 
				return dict(results._mapping)
			else:
				return None

	def get_state(self):
		with engine.connect() as conn:
			stmt    = text("select * from states where country_id = 101")
			result  = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None
			
	def get_districts(self):
		with engine.connect() as conn:
			stmt    = text("select * from districts")
			result  = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

	def get_country(self):
		with engine.connect() as conn:
			stmt    = text("select * from countries")
			result  = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

	
	def get_mobile_count(self,mobile,user_id,society_id):
		with engine.connect() as conn:
			stmt   = text("select count(*) as mobile_count from users where mobile = {} and user_id != {} and society_id = {}".format(mobile,user_id,society_id))
			result = conn.execute(stmt).first()
			conn.close()
			if result:
				results = dict(result._mapping)
				return results["mobile_count"]
			else:
				return 0	

	def count_whatsapp_num(self,whatsapp_number,user_id,society_id):
		with engine.connect() as conn:
			stmt    = text("select count(*) as count from users where user_id <> :user_id and (mobile= :whatsapp_number or whatsapp_number=:whatsapp_number) and society_id={} ;".format(society_id))
			result  = conn.execute(stmt.bindparams(user_id=user_id,whatsapp_number=whatsapp_number)).first()
			conn.close()
			if result:
				results = dict(result._mapping)
				return results["count"]
			else:
				return 0	

	def get_country_data(self,country_id):
		with engine.connect() as conn:
			stmt   = text("select * from states where country_id = {}".format(country_id))
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None



	def get_user_data(self,user_id,society_id,attach_type_id):
		with engine.connect() as conn:
			stmt   = text("select ua.*,u.*,c.country_name,s.state_name,mcs.state_name as mc_state from users u left join user_attachments ua on u.user_id = ua.user_id and attach_type_id = {} left join countries c on u.country_id = c.country_id left join states s on u.state_id = s.state_id  left join states mcs on u.mc_state_id = mcs.state_id where u.user_id = {} and society_id = {}".format(attach_type_id,user_id,society_id))
			# print(stmt)
			result = conn.execute(stmt).first()
			conn.close()
			if result:
				return dict(result._mapping)
			else:
				return None



## Harini ###
# for common profile update ##
	# def getSociety(self,society_id,society_key):
	# 	with engine.connect() as conn:
	# 		stmt     = text("select app_id,app_type_id,app_title,app_host,s.society_id,sa.is_active,society_name,society_title,email_logo,society_style,society_key,e_support_mail,society_intial,e_backup_mails,e_from_mail_id,e_reply_mail_id,profile_member_type,aws_bucket,sa.mail_setting_id,s.profile_lock_at,s.profile_lock_message,m.* ,s.e_from_mail_name,sa.app_url,s.secretary_name,s.secretary_mobile,s.e_secretary_mail_id,s.clarity_script from society_applications sa "+
	# 					" inner join societies s on s.society_id=sa.society_id"+
	# 					" inner join mail_setting m on m.mail_setting_id=sa.mail_setting_id"+
	# 					" where sa.app_type_id=4  and s.society_id= :society_id and s.society_key= :society_key;")
	# 		results  = conn.execute(stmt.bindparams(society_id=society_id,society_key=society_key)).first()
	# 		if results :
	# 			return dict(results._mapping)
	# 		else:
	# 			return None 

	def get_mail_template(self,society_id,template_name):
		with engine.connect() as conn:
			stmt    = text("select * from mail_templates where society_id=:society_id and template_name=:template_name;")
			result  = conn.execute(stmt.bindparams(society_id=society_id,template_name=template_name)).first()
			conn.close()
			if result:
				return result
			else:
				return None


	def get_users_data(self,society_id,attch_type_id):
		with engine.connect() as conn:
			stmt   = text("select u.*,ua.attach_path,ua.attach_file_name,c.country_name,s.state_name,mcs.state_name as mc_state from users u left join user_attachments ua on u.user_id = ua.user_id and attach_type_id = {} left join countries c on u.country_id = c.country_id left join states s on u.state_id = s.state_id  left join states mcs on u.mc_state_id = mcs.state_id  where u.profile_updated_at != 'null' and society_id = {} order by u.profile_updated_at desc; ".format(attch_type_id,society_id))
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None


	def usp_get_profile_bo_data(self,society_id,status_id,attach_type_id,offset,limit):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_get_profile_bo_data",[society_id,status_id,attach_type_id,offset,limit])
			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 usp_district_list(self,society_id,val,in_profile_filter):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_district_list_v1",[society_id,val,in_profile_filter])
			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 usp_data_for_district_wise(self,society_id,attach_type_id,district):
	# 	sets = []
	# 	try:
	# 		connection = engine.raw_connection()
	# 		cursor = connection.cursor()
	# 		cursor.callproc("usp_data_for_district_wise",[society_id,attach_type_id,district])
	# 		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 usp_data_for_district_wise_v1(self,society_id,search_text,attach_type_id,district,in_filter,in_profile_filter,member_type_id,fromdate,todate,in_order,offset,limit):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_data_for_district_wise_v1",[society_id,search_text,attach_type_id,district,in_filter,in_profile_filter,member_type_id,fromdate,todate,in_order,offset,limit])
			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 get_bo_dashboard_count(self,society_id):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_get_bo_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()
		connection.close()
		if results :
			return results
		else :
			return None
		
	def get_bo_dashboard_count_v1(self,society_id,in_profile_filter):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_get_bo_dashboard_count_v1",[society_id,in_profile_filter])
		columns = [column[0] for column in cursor.description]
		results = []
		for row in cursor.fetchall():
			results.append(dict(zip(columns, row)))
		cursor.close()
		connection.commit()
		connection.close()
		if results :
			return results
		else :
			return None

	def get_total_member_data(self,society_id,attch_type_id):
		with engine.connect() as conn:
			stmt   = text("select u.*,ua.attach_path,ua.attach_file_name from users u left join user_attachments ua on u.user_id = ua.user_id and attach_type_id = {} where u.status_id = 14 and society_id = {} order by u.profile_updated_at desc; ".format(attch_type_id,society_id))
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

	def get_profile_updated_data(self,society_id,attch_type_id):
		with engine.connect() as conn:
			stmt   = text("select u.*,ua.attach_path,ua.attach_file_name from users u left join user_attachments ua on u.user_id = ua.user_id and attach_type_id = {} where u.profile_updated_at !='null' and society_id = {} order by u.profile_updated_at desc; ".format(attch_type_id,society_id))
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

	def get_incomplete_data(self,society_id,attch_type_id):
		with engine.connect() as conn:
			stmt   = text("select u.*,ua.attach_path,ua.attach_file_name from users u left join user_attachments ua on u.user_id = ua.user_id and attach_type_id = {} where u.profile_updated_at is null and u.status_id=14 and society_id = {} order by u.profile_updated_at desc; ".format(attch_type_id,society_id))
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None
			
	def usp_profile_analytics_v1(self,society_id,in_count,in_filter,in_from,in_to):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_profile_analytics_v1",[society_id,in_count,in_filter,in_from,in_to])
			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 usp_profile_analytics_v2(self,society_id,in_count,in_filter,in_from,in_to,in_profile_filter):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_profile_analytics_v2",[society_id,in_count,in_filter,in_from,in_to,in_profile_filter])
			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 get_profile_member_type_by_society_id(self,society_id):
		with engine.connect() as conn:
			stmt    = text("SELECT * FROM m_member_type m "
						+ " inner join society_map_member_type s on s.member_type_id=m.member_type_id "
						+ f" where society_id={society_id}")
			result  = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None
		
	def get_upload_details(self,society_id,attach_type_id):
		with engine.connect() as conn:
			stmt    = text(f"select * from m_attachment_type where society_id= {society_id}  and attach_type_id in ({attach_type_id});")
			result  = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None
	
	# def get_upload_details(self,society_id,attach_type_id):
	# 	with engine.connect() as conn:
	# 		stmt    = str(f"select * from m_attachment_type where society_id= {society_id}  and attach_type_id={attach_type_id};")
	# 		result = conn.execute(text(stmt)).first() 
	# 		return  dict(result._mapping) if result else None
		
	def get_fellowship_data(self,user_id,attach_type_id):
		with engine.connect() as conn: 
			stmt    = text(f"select m.*,u.attach_id,u.user_id,u.attach_file_name,u.attach_path,u.confirm_date,u.created_at,u.updated_at,u.deleted_at from m_attachment_type m left join user_attachments u on u.attach_type_id = m.attach_type_id and user_id={user_id} where m.attach_type_id in ({attach_type_id});")
			# stmt    = str(f"select * from user_attachments where user_id={user_id} and attach_type_id=11;")
			result  = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None
		
	def get_user_attach(self,attach_id,attach_type_id):
		with engine.connect() as conn:
			stmt = text("select * from user_attachments where attach_id = "+str(attach_id)+" and attach_type_id = " +str(attach_type_id)+";")
			result = conn.execute(stmt).first()
			conn.close()
			if result:
				return dict(result._mapping)
			else:
				return None
			
	def update_image(self,attach_type_id,data):
		with engine.connect() as conn:
			stmt = self.user_attachments.update().where(self.user_attachments.c.attach_id.in_([attach_type_id])).values(data)
			result = conn.execute(stmt)
			conn.commit()
			conn.close()
			if result:
				return 'success'
			else :
				return 'fail'
			
	def insert_image(self,data):
		with engine.connect() as conn:
			result = conn.execute(self.user_attachments.insert(), data)
			conn.commit()
			conn.close()
			pk_id = result.inserted_primary_key[0] if result.inserted_primary_key[0] else None
			return pk_id
		
	def delete_image(self,attach_id,attach_type_id):
		with engine.connect() as conn:
			stmt = text("delete from user_attachments WHERE attach_id = "+str(attach_id)+" and attach_type_id ="+str(attach_type_id)+";")
			results = conn.execute(stmt)
			conn.commit()
			conn.close()
			if results:
				return 'success'
			else :
				return 'fail'
			
	def update_confirm_date(self,confirm_date,attach_id):
		with engine.connect() as conn:
			stmt = text(f"update user_attachments set confirm_date = '{confirm_date}' where attach_id = {attach_id};")
			result = conn.execute(stmt)
			conn.commit()
			conn.close()
			return "success"
		
	def get_profile_member_type_by_society_id(self,society_id):
		with engine.connect() as conn:
			stmt    = text("SELECT * FROM m_member_type m "
						+ " inner join society_map_member_type s on s.member_type_id=m.member_type_id "
						+ f" where society_id={society_id}")
			result  = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None
		
	def get_profile_member_type(self):
		with engine.connect() as conn:
			stmt    = text("SELECT * from m_member_type where member_type_id in (1,2);")
			result  = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None
			
	def check_bo_access(self,email,app_type_id,society_id,conf_id):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_check_admin_access",[email,app_type_id,society_id,conf_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()
		connection.close()
		if results :
			return results[0]
		else :
			return None
		
	def update_or_insert_participation(self,user_id,participation_id,data2):
		with engine.connect() as conn:
			if (int(participation_id) > 0):
				
				stmt = self.user_participation.update().where(self.user_participation.c.participation_id.in_([participation_id])).values(data2)
				result = conn.execute(stmt)
			else :
				result = conn.execute(self.user_participation.insert(), data2)
			# conn.commit()
			# result = conn.execute(stmt)
			conn.commit()
			conn.close()
			return result
		
	def get_specialities(self,user_id):
		with engine.connect() as conn:
			stmt = text("select c.*,s.user_id from abs_categories c "
					+ " left join users_participate_sections s on c.category_id = s.category_id and user_id = :user_id "
					+ " where c.category_id in (1,61,111,81,340,31,404,405,370,348,371,171) "
					+ " order by display_name;")
			results = conn.execute(stmt.bindparams(user_id=user_id)).all()
			conn.close()
			return  [dict(r._mapping) for r in results] if results else None
		
	def get_longterm_specialities(self):
		with engine.connect() as conn:
			stmt = text("select c.* from abs_categories c "
					+ " where c.category_id in (31,1,407,386,371,181,61,346,111,408,407) "
					+ " order by display_name;")
			results = conn.execute(stmt).all()
			conn.close()
			return  [dict(r._mapping) for r in results] if results else None
		
	def get_old_specialities(self,user_id):
		with engine.connect() as conn:
			stmt = text(f"select * from users_participate_sections where user_id = {user_id};")
			results = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in results] if results else None
		
	def insert_specialities_db(self,insert_specialities,user_id,created_at):
		with engine.connect() as conn:
			row_val = []
			for i in insert_specialities:
				row_val.append({'category_id':i,'user_id':user_id,'created_at':created_at})

			stmt_1 = insert(self.users_participate_sections).values(row_val)
			result = conn.execute(stmt_1)
			conn.commit()
			conn.close()
			return result
		
	def delete_specialities_db(self,delete_specialities,user_id):
		with engine.connect() as conn:
			stmt = text(f"delete from users_participate_sections WHERE category_id IN({delete_specialities}) and user_id = {user_id}")
			result = conn.execute(stmt)
			conn.commit()
			conn.close()
			return result
		
	def get_specialites_data(self,user_id):
		with engine.connect() as conn: 
			stmt    = str(f"select group_concat(DISTINCT ac.display_name  separator ',') as parent_category  from users_participate_sections ups inner join abs_categories ac on ups.category_id=ac.category_id where user_id={user_id};")
			# stmt    = str(f"select * from user_attachments where user_id={user_id} and attach_type_id=11;")
			result = conn.execute(text(stmt)).first()
			conn.close()
			return  dict(result._mapping) if result else None
		
	def get_user_participation_data(self,user_id,society_id):
		with engine.connect() as conn: 
			stmt    = str(f"select * from user_participation where user_id={user_id} and society_id={society_id};")
			# stmt    = str(f"select * from user_attachments where user_id={user_id} and attach_type_id=11;")
			result = conn.execute(text(stmt)).first()
			conn.close()
			return  dict(result._mapping) if result else None
			
	def session_data_InsertToSqlite_1(self):
		sqlite_conn = sqlite3.connect('sqlite_database.db')
		tables = ['societies', 'm_attachment_type', 'society_applications', 'mail_setting']
		for table in tables:
			mysql_query = f"SELECT * FROM {table}"
			df = pd.read_sql(mysql_query, engine)
			df.to_sql(table, sqlite_conn, if_exists='replace', index=False)
		sqlite_conn.commit()
		engine.dispose()
		sqlite_conn.close()
		return "success"
		
	def get_abs_and_del_data(self,society_id):
		with engine.connect() as conn:
			stmt    = text("select sa.*,c.conf_key,c.conf_end_time from society_applications sa"
							+" inner join conference c on c.conf_id=sa.conf_id"
							+f" where sa.society_id={society_id} and sa.is_active=1 and sa.app_type_id in (2,3) and c.conf_end_time >= current_date() ;")
			result  = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

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