Sindbad~EG File Manager

Current Path : /home/numerotech/conference.aios-scientificcommittee.org/aios_conf_app/core/model/
Upload File :
Current File : //home/numerotech/conference.aios-scientificcommittee.org/aios_conf_app/core/model/UserModel.py

from sqlalchemy import create_engine, MetaData, Table, insert, select,update,delete,text
from flask import Flask,flash
from core import app
from sqlalchemy.sql import and_, or_
from sqlalchemy import asc, desc


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

class UserModel():
	def __init__(self):
		try:
			self.meta = MetaData()
			self.users           = Table("users", self.meta, autoload_with=engine)
			self.m_payment_type  = Table("m_payment_type", self.meta, autoload_with=engine)
			self.societies       = Table("societies", self.meta, autoload_with=engine)
			self.m_member_type   = Table("m_member_type", self.meta, autoload_with=engine)
			self.delegates       = Table("delegates", self.meta, autoload_with=engine)
			self.delegates_addons= Table("delegates_addons", self.meta, autoload_with=engine)
			self.user_payment    = Table("user_payment", self.meta, autoload_with=engine)
		except Exception as e:
			print("table not found",e)

	def get_search_values(self,search_data):
		with engine.connect() as conn:
			stmt   = text("select distinct s.society_id,s.society_name,u.* from societies s left join users u on s.society_id =u.society_id  where full_name like '%"+search_data+"%' or email like '%"+search_data+"%' or mobile like '%"+search_data+"%' order by society_id asc;")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None

	def view_search(self,user_id):
		with engine.connect() as conn:
			stmt   = text("SELECT s.society_intial,s.society_name,u.*,m.member_type from users u left join societies s on s.society_id=u.society_id left join m_member_type m on m.member_type_id = u.member_type_id WHERE u.user_id="+str(user_id)+" ;")
			result = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else 0

	def usp_get_all_society_members(self,search_name,search_email,search_mobile,society_id):
		connection  = engine_conf.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("usp_get_all_society_members",[search_name,search_email,search_mobile,society_id])
		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 check_email(self,email,society_id):
		with engine.connect() as conn:
			stmt   = self.users.select().where(self.users.c.email.in_([email])).where(self.users.c.society_id.in_([society_id]))
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None

	def insert_user_data(self,data):
		with engine.connect() as conn:
			result = conn.execute(self.users.insert(),data)
			conn.commit()
			return result

	def view_email_list(self,user_id):
		with engine.connect() as conn:
			stmt_1   = text("SELECT * from users  WHERE user_id= "+str(user_id)+" ;")
			result_1 = conn.execute(stmt_1).one_or_none()

			result_1 = dict(result_1._mapping)  if result_1 else None
			stmt_2   = text("select u.*,s.*,st.state_name as mc_state_name,concat(ifnull(u.address1,''),'',case when u.address2 is null then '' else  concat(',',u.address2) end ,'',case when u.address3 is null then '' else  concat(',',u.address3) end) as address from users  u inner join societies s on s.society_id=u.society_id  left join states st on st.state_id = u.mc_state_id where email='"+str(result_1['email'])+"';")
			result_2 = conn.execute(stmt_2).all()
			return [dict(r._mapping) for r in result_2] if result_2 else None

	def view_mobile_list(self,user_id):
		with engine.connect() as conn:
			stmt_1   = text("SELECT * from users  WHERE user_id= "+str(user_id)+" ;")
			result_1 = conn.execute(stmt_1).one_or_none()
			result_1 = dict(result_1._mapping)  if result_1 else None

			stmt_2   = text("select * from users  u inner join societies s on s.society_id=u.society_id  where mobile='"+str(result_1['mobile'])+"';")
			result_2 = conn.execute(stmt_2).all()
			return [dict(r._mapping) for r in result_2] if result_2 else None

	def update_member(self,user_id,data,society_id):
		with engine.connect() as conn:
			stmt      = self.users.update().where(self.users.c.user_id.in_([user_id])).values(data)
			restult_1 = conn.execute(stmt)
			conn.commit()

			stmt_2    = text("select s.society_intial,u.* from users u inner join societies s on s.society_id=u.society_id WHERE u.user_id="+str(user_id)+";")
			result_2  = conn.execute(stmt_2).all()
			return [dict(r._mapping) for r in result_2] if result_2 else ''

	def check_email_update(self,email,society_id):
		with engine.connect() as conn:
			stmt = text("select * from users where (email= "+str(email)+" and society_id="+str(society_id)+";")
			result 	= conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None

	def view_particular_email_list(self,user_id):
		with engine.connect() as conn:
			stmt_1   = text("SELECT * from users  WHERE user_id="+str(user_id)+" ;")
			result_1 = conn.execute(stmt).one_or_none()

			stmt_2   = text("select u.*,s.*,st.state_name as mc_state_name,concat(ifnull(u.address1,''),'',case when u.address2 is null then '' else  concat(',',u.address2) end ,'',case when u.address3 is null then '' else  concat(',',u.address3) end) as address from users  u inner join societies s on s.society_id=u.society_id  left join states st on st.state_id = u.mc_state_id where u.email="+str(email)+" and u.society_id ="+str(society_id)+";")
			result_2 = conn.execute(stmt_2).all()
			return [dict(r._mapping) for r in result_2] if result_2 else None

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

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

	def get_search_user_id_in_delagetes(self,user_id):
		with engine.connect() as conn:
			stmt   = text("select c.conf_name,u.user_id,concat(ifnull(concat(d.prefix,' '),''),d.full_name) as d_full_name, d.delegate_no,d.delegate_id,d.email as d_email,d.mobile as d_mobile,a.display_name,da.addon_id,d.society_id,up.receipt_no,up.payment_method,up.payment_method_from_gateway,up.api_payment_id,up.utr_number  from users u  inner join delegates d on d.parent_user_id=u.user_id  inner join societies s on u.society_id = s.society_id inner join conference c on c.conf_id = d.conference_id inner join delegates_addons da on da.delegate_id = d.delegate_id inner join addons a on a.addon_id = da.addon_id inner join user_payment up on up.unique_id = da.unique_id  where u.user_id = "+str(user_id)+";")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None

	def delete_delegate_user_id(self,delegate_id):
		with engine.connect() as conn:
			stmt   = self.delegates.delete().where(self.delegates.c.delegate_id.in_([delegate_id]))
			result = conn.execute(stmt)
			conn.commit()
			return 'success' if result else 'fail'

	def delete_user_id(self,user_id):
		with engine.connect() as conn:
			stmt   = self.users.delete().where(self.users.c.user_id.in_([user_id]))
			result = conn.execute(stmt)
			conn.commit()
			return 'success' if result else 'fail'

	def get_mcstates_of_india(self):
		with engine.connect() as conn:
			stmt   = text("select * from states where country_id =101 order by state_name asc ;")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None
		
	def get_delegate_data(self,conf_id,search_data,before_time,delegate_id):
		# print("call usp_del_search_delegate_record(",conf_id,",'",search_data,"',",before_time,",",delegate_id,")")
		sets = []
		try:
			connection = engine_conf.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_del_search_delegate_record",[conf_id,search_data,before_time,delegate_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 update_delegate_record(self,delegate_id,data):
		with engine.connect() as conn:
			stmt = self.delegates.update().where(self.delegates.c.delegate_id.in_([delegate_id])).values(data)
			result = conn.execute(stmt)
			conn.commit()
			return 'success' if result else 'fail'

	def update_delegate_addon_record(self,delegate_addon_id,data):
		with engine.connect() as conn:
			stmt = self.delegates_addons.update().where(self.delegates_addons.c.delegate_addon_id.in_([delegate_addon_id])).values(data)
			result = conn.execute(stmt)
			conn.commit()
			return 'success' if result else 'fail'

	def update_user_payment_record(self,payment_id,data):
		with engine.connect() as conn:
			stmt = self.user_payment.update().where(self.user_payment.c.payment_id.in_([payment_id])).values(data)
			result = conn.execute(stmt)
			conn.commit()
			return 'success' if result else 'fail'	

	def delete_delegate_record(self,delegate_id,delegate_addon_id):
		connection = engine_conf.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_del_delete_record",[delegate_id,delegate_addon_id])
		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()
			return results
		else :
			cursor.close()
			connection.commit()
			return None
			
	def get_logger_name(self):
		with engine.connect() as conn:
			stmt   = text("select logger_name from logs where logger_name is not null group by logger_name ;")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None

	def get_host_url(self):
		with engine.connect() as conn:
			stmt   = text("select host_url from logs where host_url is not null group by host_url ;")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None

	def get_app_type(self):
		with engine.connect() as conn:
			stmt   = text("select app_type from logs where app_type is not null group by app_type ;")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None

	def get_del_error_logs(self,logger_name,host_url,before_time,limit,app_type,db_name):
		connection  = engine_conf.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("usp_del_error_logs",[logger_name,host_url,before_time,limit,app_type,db_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
			
	# Usage : Search Pending Delegate Record
	# Date  : 2023-07-03 12:00
	# Created by : Ramya

	def get_pending_delegate_record(self,conf_id,from_date,to_date,status,limit):
		connection  = engine_conf.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("usp_del_search_pending_list",[conf_id,from_date,to_date,status,limit])
		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
			
			
	# Usage : View BO Admin Access Users
	# Date  : 2023-07-07 15:02
	# Created by : Ramya


	def get_boadmin_access_record(self,society_id):
		with engine.connect() as conn:
			stmt   = text("select concat(ifnull(concat(prefix,' '),''),full_name) as full_name,mobile,email,password,is_admin from users where is_admin = 1 and society_id = "+str(society_id)+" ;")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None

	def get_society_name(self,society_id):
		with engine.connect() as conn:
			stmt   = text("select society_name from societies where  society_id = "+str(society_id)+" ;")
			result = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else 0

	# Usage : View Payment type for conference
	# Date  : 2023-07-11 12:27
	# Created by : Ramya


	def get_payment_type(self,conf_id):
		with engine.connect() as conn:
			stmt     = text("select * from m_payment_type where is_delegate = 1  and is_visible = 1  and conf_id  = "+str(conf_id)+" ;")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None

	def update_payment_type(self,m_payment_id,data):
		with engine.connect() as conn:
			stmt   = self.m_payment_type.update().where(self.m_payment_type.c.m_payment_id.in_([m_payment_id])).values(data)
			result = conn.execute(stmt)
			conn.commit()
			return 'success' if result else 'fail'
			
	def get_users_data_for_whatsapp(self,user_id):
		with engine.connect() as conn:
			stmt    = text("select full_name,email,whatsapp_number,password,prefix from users where user_id ="+str(user_id)+" ;")
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else 0
			
	def get_conf_data_by_society(self,society_id):
		with engine.connect() as conn:
			stmt   = text("select * from conference where society_id = "+str(society_id)+";")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None
			
	def update_user_and_delegate(self,user_id,parent_user_id,conference_id,prefix,full_name,email,mobile,current_dt):
		with engine.connect() as conn:
			stmt   = text("update delegates set prefix= '"+str(prefix)+"' ,full_name='"+str(full_name)+"',email='"+str(email)+"',mobile="+str(mobile)+",updated_at ='"+str(current_dt)+"' where conference_id ="+str(conference_id)+" and user_id="+str(user_id)+" and parent_user_id="+str(parent_user_id)+";")
			result = conn.execute(stmt)
			conn.commit()
			return 'success' if result else 'fail'
			
	def update_in_delegate(self,user_id,parent_user_id,conference_id,membership_no,mc_number,mc_state_id,current_dt):
		with engine.connect() as conn:					
			stmt   = text("update delegates set membership_no='"+str(membership_no)+"',mc_number="+str(mc_number)+",mc_state_id="+str(mc_state_id)+",updated_at ='"+str(current_dt)+"' where conference_id ="+str(conference_id)+" and user_id="+str(user_id)+" and parent_user_id="+str(parent_user_id)+";")
			result = conn.execute(stmt)
			conn.commit()
			return 'success' if result else 'fail'
			
	# Usage : Bulk Delete error logs
	# Date  : 2023-09-25 12:12
	# Created by : Ramya

	def delete_error_logs(self,db_name,ids):
		with engine_conf.connect() as conn:
			try:
				stmt_delete = text("delete FROM "+db_name+".logs WHERE id in ("+ids+");")
				result = conn.execute(stmt_delete)
				conn.commit()
				return 'success' if result else 'fail'
			except Exception as e:
				return str(e)		

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