Sindbad~EG File Manager

Current Path : /home/numerotech/mcinfo.numerotech.com/mc_info_update/core/model/
Upload File :
Current File : //home/numerotech/mcinfo.numerotech.com/mc_info_update/core/model/MCDetailModel.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

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


class MCDetailModel():
	def __init__(self):
		try:
			self.meta = MetaData()
			# self.delegates = Table("delegates", self.meta,  autoload_with=engine)
			# self.mc_delegates = Table("mc_delegates", self.meta,  autoload_with=engine)
			
				
		except Exception as e:
			print(e)

	def checkConfData(self,conf_id,conf_key):
		with engine.connect() as conn:
			stmt = text("select * from conference c inner join society_applications s on s.conf_id = c.conf_id and app_type_id = 2  inner join mail_setting  m on s.mail_setting_id = m.mail_setting_id where c.conf_id = "+str(conf_id)+" and c.conf_key = '"+conf_key+"' ;")
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None


	def delegateDataByUUID(self,conf_id,conf_key,user_uuid,conf_schema):
		with engine.connect() as conn:
			stmt = text("select d.*, s.state_name as mc_state_name from "+str(conf_schema)+".delegates d left join states s on s.state_id = d.mc_state_id  and s.country_id = 101 " 
			+ " where d.conference_id = "+str(conf_id)+" and d.user_uuid = '"+user_uuid+"'  and d.delegate_no is not null and d.delegate_no > 0 ;")
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None		

	def getStates(self,country_id):
		with engine.connect() as conn:
			stmt = text("select * from states s where s.country_id = "+str(country_id)+" ;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			conn.close()
			return results if results else None					



	def updateData(self,data,user_uuid,email,conf_id,conf_key,conf_schema):
		self.delegates = Table("delegates", self.meta,schema=conf_schema,  autoload_with=engine)
		with engine.connect() as conn:
			try:
				update_stmt = self.delegates.update().where(self.delegates.c.user_uuid.in_([user_uuid])).values(data) 
				conn.execute(update_stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				conn.close()
				return str(e)

	# ------------------------ March 24, 2023 --------
	def delegateDataByDelegateNo(self,conf_id,conf_key,delegate_no,conf_schema):
		with engine.connect() as conn:
			stmt = text("select d.*, s.state_name as mc_state_name from "+str(conf_schema)+".delegates d left join states s on s.state_id = d.mc_state_id  and s.country_id = 101 " 
			+ " where d.conference_id = "+str(conf_id)+" and d.delegate_no = "+str(delegate_no)+"  and d.delegate_no is not null and d.delegate_no > 0 limit 1 ;")
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None



	def updateMCData(self,data,delegate_no,email,conf_id,conf_key,mc_number,state_name,curr_dt,del_table_name,conf_schema):
		self.delegates = Table("delegates", self.meta,schema=conf_schema,  autoload_with=engine)
		with engine.connect() as conn:
			try:
				update_stmt = self.delegates.update().where(
					and_(
						self.delegates.c.delegate_no == delegate_no,
						self.delegates.c.conference_id == conf_id
				)
				).values(data) 
				conn.execute(update_stmt)
				conn.commit()

				# update_del_conf_table = text("UPDATE  "+del_table_name+" set mc_number= '" + mc_number+ "' , state = '"+ state_name+"' ,updated_at = '"+curr_dt+"' where delegate_no =  " +str(delegate_no)+ "  and conf_id = "+str(conf_id)+" ;" )
				# conn.execute(update_del_conf_table)
				# conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return str(e)
				
	def updateMCDataNew(self,data_for_update,delegate_no,conf_id,update_stmt,conf_schema):
		self.delegates = Table("delegates", self.meta,schema=conf_schema,  autoload_with=engine)
		with engine.connect() as conn:
			try:
				stmt = self.delegates.update().where(
					and_(
						self.delegates.c.delegate_no == delegate_no,
						self.delegates.c.conference_id == conf_id
				)
				).values(data_for_update) 
				conn.execute(stmt)
				conn.commit()
				conn.close()

				# update_del_conf_table = text(update_stmt)
				# conn.execute(update_del_conf_table)
				# conn.commit()
				return "success"
			except Exception as e:
				conn.close()
				return str(e)			

	def getMCState(self,mc_state_id):	
		print("getMCState === ",mc_state_id)
		with engine.connect() as conn:
			stmt = text("SELECT state_name from states where state_id = "+str(mc_state_id)+" ; ")
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None
		
	def insert_delegate_in_mc_delegates(self,conf_id,delegate_no,curr_dt,conf_schema):
		with engine.connect() as conn:
			try:
				stmt = text("insert into mc_delegates (delegate_no,prefix,full_name,email,mobile,mc_number,mc_state_id,role,reg_type_id,conference_id,created_at) select delegate_no,prefix,full_name,email,mobile,mc_number,mc_state_id,role,reg_type_id,conference_id,'"+curr_dt+"' from "+str(conf_schema)+".delegates where conference_id ="+str(conf_id)+" and delegate_no = "+str(delegate_no)+" and delegate_no is not null;")
				conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				conn.close()
				return str(e)	

	def check_delegates_email(self,conf_id,email,delegate_no,conf_schema):
		with engine.connect() as conn:
			stmt_2 = text("select distinct u.user_type_id from user_types u "
					+" inner join addons  a on a.user_type_id = u.user_type_id "
					+" where a.conference_id = "+str(conf_id)+" and u.is_spouse = 1  limit 1;")
			result_1 = conn.execute(stmt_2).fetchone()
			result_2 = dict(result_1._mapping) if result_1  else None
			spouse   = result_2['user_type_id'] if result_2  else None
			print("spouse = ",spouse)
			if spouse :
				stmt = text("select * from "+str(conf_schema)+".delegates where conference_id = "+str(conf_id)+" and email = '"+str(email)+"' and delegate_no <> "+str(delegate_no)+"  and reg_type_id not in ("+str(spouse)+") and  delegate_no is not null limit 1 ;")
			else :
				stmt = text("select * from "+str(conf_schema)+".delegates where conference_id = "+str(conf_id)+" and email = '"+str(email)+"' and delegate_no <> "+str(delegate_no)+"  and  delegate_no is not null  limit 1;")
			print(stmt)
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None
			

	def check_delegates_mobile(self,conf_id,mobile,delegate_no,conf_schema):
		with engine.connect() as conn:
			stmt_2 = text("select distinct u.user_type_id from user_types u "
					+" inner join addons  a on a.user_type_id = u.user_type_id "
					+" where a.conference_id = "+str(conf_id)+" and u.is_spouse = 1  limit 1;")
			result_1 = conn.execute(stmt_2).fetchone()
			result_2 = dict(result_1._mapping)  if result_1  else None
			spouse   = result_2['user_type_id']  if result_2  else None
			if spouse :
				stmt = text("select * from "+str(conf_schema)+".delegates where conference_id = "+str(conf_id)+" and mobile = '"+str(mobile)+"' and delegate_no <> "+str(delegate_no)+"  and reg_type_id not in ("+str(spouse)+") and  delegate_no is not null  limit 1 ;")
			else :
				stmt = text("select * from "+str(conf_schema)+".delegates where conference_id = "+str(conf_id)+" and mobile = '"+str(mobile)+"' and delegate_no <> "+str(delegate_no)+"  and delegate_no is not null  limit 1;")
			
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None

	def delegateDataByEmail(self,conf_id,conf_key,email,conf_schema):
		with engine.connect() as conn:
			stmt = text("select d.*, s.state_name as mc_state_name from "+str(conf_schema)+".delegates d left join states s on s.state_id = d.mc_state_id  and s.country_id = 101 " 
			+ " where d.conference_id = "+str(conf_id)+" and d.email = '"+str(email)+"'  and d.delegate_no is not null and d.delegate_no > 0 and d.del_status_id = 2 limit 1  ;")
			result = conn.execute(stmt).first()
			print(stmt)
			conn.close()
			return dict(result._mapping) if result  else None


	def delegateDataByMobile(self,conf_id,conf_key,mobile,conf_schema):
		with engine.connect() as conn:
			stmt = text("select d.*, s.state_name as mc_state_name from "+str(conf_schema)+".delegates d left join states s on s.state_id = d.mc_state_id  and s.country_id = 101 " 
			+ " where d.conference_id = "+str(conf_id)+" and d.mobile = '"+str(mobile)+"'  and d.delegate_no is not null and d.delegate_no > 0 and d.del_status_id = 2 limit 1 ;")
			result = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result  else None

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