Sindbad~EG File Manager

Current Path : /home/numerotech/live.numerotech.com/common_user_dashboard/core/model/
Upload File :
Current File : //home/numerotech/live.numerotech.com/common_user_dashboard/core/model/UserModel.py

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

# engine = create_engine(app.config['DATABASE_URI'],pool_size=5000,pool_pre_ping=True,pool_recycle=3600)
engine = create_engine(app.config['DATABASE_URI'],
					pool_size=10,        # Maximum number of connections in the pool
					max_overflow=5,      # Maximum number of additional connections to be created if pool is exhausted
					pool_timeout=30,     # Seconds to wait before giving up when trying to connect to the pool
					pool_recycle=3600,   # Time in seconds after which a connection will be recycled
				)



class UserModel():
	def __init__(self):
		try:
			self.meta                   = MetaData()
			self.users                  = Table("users", self.meta,  autoload_with=engine)
			self.societies              = Table("societies", self.meta,  autoload_with=engine)
			self.m_attachment_type      = Table("m_attachment_type", self.meta,  autoload_with=engine)
			self.ex_comm_members        = Table("ex_comm_members", self.meta,  autoload_with=engine)
			self.peer_support_communication     = Table("peer_support_communication", self.meta,  autoload_with=engine)
		except Exception as e:
			print(e)
	
	def get_data(self,email,pwd_difference):
		with engine.connect() as conn:
			stmt = text(f"select * from users where email= '{email}' and pwd_difference='{pwd_difference}';")       
			result = conn.execute(stmt)
			results = result.first()
			conn.close()
			return results if results else None
			
			
	def get_society_by_host(self,host):
		with engine.connect() as conn:
			stmt = text(f"select * from societies where society_host = '{host}' ;")       
			results = conn.execute(stmt).first()
			results = dict(results._mapping) if results else None  
			conn.close()
			return results

	def get_attach_type_id(self,society_id,attach_type):
		with engine.connect() as conn:
			stmt = text(f"select * from m_attachment_type where society_id= {society_id} and attach_type='{attach_type}';")       
			result = conn.execute(stmt).first()
			conn.close()
			return result
			
	def get_dashboard_content(self,society_id,user_id):
		with engine.connect() as conn:
	# 		stmt = text("select * from society_applications sa inner join m_app_type mat on sa.app_type_id = mat.app_type_id where society_id = {}  order by mat.app_type_id".format(society_id))
			stmt = text("SELECT * FROM society_applications sa "+
						" INNER JOIN m_app_type mat ON sa.app_type_id = mat.app_type_id "+
						f" Left JOIN users u on u.user_id = {user_id} "+
						f" WHERE sa.society_id = {society_id} and conf_id is null and is_active = 1  "+
						" ORDER BY mat.app_type_id")
			results = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in results] if results else None
			conn.close()
			return results
			
	def get_dashboard_content_conference(self,society_id):
		with engine.connect() as conn:
			stmt = text(f"select * from conference where society_id = {society_id} and is_active = 1 order by conf_start_dt desc;")
			results = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in results] if results else None
			conn.close()
			return results
			
	def get_dashboard_conference_application(self,society_id):
		with engine.connect() as conn:
			stmt = text(f"select * from society_applications sa INNER JOIN m_app_type mat ON sa.app_type_id = mat.app_type_id  where society_id =  {society_id} and conf_id is not null and is_active = 1 order by mat.app_type_id desc;")
			results = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in results] if results else None
			conn.close()
			if results : 
				return results
			else:
				return None

	def get_userdata(self,email,society_id):
		with engine.connect() as conn:
			stmt = text(f"select * from users where email= '{email}' and society_id={society_id};")       
			result = conn.execute(stmt).first()
			results = dict(result._mapping)  if result else None
			conn.close()
			return results 
			
	def get_otp_random(self,user_id):
		with engine.connect() as conn:
			stmt = text("select otp from users where user_id = {} ".format(user_id))
			results = conn.execute(stmt).first()
			results = dict(results._mapping)  if results else None
			conn.close()
			return results
			
	def update_member(self,user_id,data):
		with engine.connect() as conn:
			stmt = self.users.update().values(data).where(self.users.c.user_id.in_([user_id]))
			select_stmt = text("select *  from users where user_id = "+str(user_id)+" ")
			result = conn.execute(stmt)
			conn.commit()
			select_result = conn.execute(select_stmt).first()
			result_2 = dict(select_result._mapping) if select_result else None
			conn.close()
			return result_2
			
   
	def insert_users(self,data):
		with engine.connect() as conn:
			result = conn.execute(self.users.insert(), data)
			conn.commit()
			pk_id = [r for r in result.inserted_primary_key] if result.inserted_primary_key else None
			conn.close()
			return pk_id

	def get_email_count(self,email,society_id):
		with engine.connect() as conn:
			stmt = text("select count(*) as email_count from users where email = '{}' and society_id ={}".format(email,society_id))
			result = conn.execute(stmt).first()
			conn.close()
			return result.email_count


	def update_user(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

	
	def check_password(self,user_id):
		with engine.connect() as conn:
			stmt = self.users.select().where(self.users.c.user_id.in_([user_id]))
			result = conn.execute(stmt).first()
			conn.close()
			return result if result else None
			
	def get_society(self,society_id):
		with engine.connect() as conn:
			stmt = text(f"select * from societies where society_id= {society_id};")       
			results = conn.execute(stmt).first()
			result=  dict(results._mapping) if results else None
			conn.close()
			return result
			
	def get_member(self,user_id):
		with engine.connect() as conn:
			stmt = text(f'select * from users where user_id = {user_id}')
			result = conn.execute(stmt).first()
			results = dict(result._mapping)
			conn.close()
			return results if results else None
	
	def get_autologin_data(self,user_uuid):
		with engine.connect() as conn:     
			stmt = text("select * from users where user_uuid=:user_uuid")
			result = conn.execute(stmt.bindparams(user_uuid=user_uuid)).first()
			if result :
				return dict(result._mapping)
			else:
				return None  
	
	def get_ex_comm_members(self):
		with engine.connect() as conn:
			stmt = text("select * from ex_comm_members")
			results = conn.execute(stmt).all()
			result=  [dict(r._mapping) for r in results] if results else None
			conn.close()
			return result		
	
	def get_user(self,user_id,society_id):
		with engine.connect() as conn:
			stmt = text(f"select * from users u left join m_member_type m on u.member_type_id = m.member_type_id left join user_attachments ua on u.user_id=ua.user_id and attach_type_id = (select attach_type_id from m_attachment_type where attach_type='Photograph' and society_id = {society_id}) where u.user_id={user_id} ")
			result = conn.execute(stmt).first()
			conn.close()
			return result if result else None
			
	def get_member_display_name(self,member_type_id,society_id):
		with engine.connect() as conn:
			stmt = text(f"select mt.member_type,smt.display_name,smt.amount from m_member_type as mt inner join society_map_member_type as smt on mt.member_type_id=smt.member_type_id where smt.member_type_id={member_type_id}  and smt.society_id={society_id}")
			results = conn.execute(stmt).first()
			conn.close()
			return results if results 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_country(self):
		with engine.connect() as conn:
			stmt = text("select * from countries")
			results = conn.execute(stmt).all()
			result=  [dict(r._mapping) for r in results] if results else None
			conn.close()
			return result
			

	def get_state(self,country_id):
		with engine.connect() as conn:
			stmt = text("select * from states where country_id = {}".format(country_id))
			results = conn.execute(stmt).all()
			result=  [dict(r._mapping) for r in results] if results else None
			conn.close()
			return result
			
	def getSociety(self,society_id,society_key):
		with engine.connect() as conn:
			stmt = text(f"select * from societies where society_id = {society_id} and society_key= '{society_key}';")
			results = conn.execute(stmt).first()
			results = dict(results._mapping)  if results else None
			conn.close()
			return results
			
	def setSociety(self,society_host):
		with engine.connect() as conn:
			stmt = text(f"select * from societies where society_host = '{society_host}';")
			results = conn.execute(stmt).first()
			results=  dict(results._mapping) if results else None
			conn.close()
			return results
			
	def get_data_mail(self,user_id):
		with engine.connect() as conn:
			stmt = text("SELECT  u.* ,s.state_name,c.country_name from users u "+
						" left join states s  on u.state_id = s.state_id "+
						" left join countries  c  on c.country_id = s.country_id "+ 
						f" where u.user_id = {user_id};")
			result = conn.execute(stmt).first()
			result=  dict(result._mapping) if result else None
			conn.close()
			return result if result else None
			
	def count_mobile_num(self,user_id,mobile,society_id):
		with engine.connect() as conn:
			stmt = text(f"select count(*) from users where user_id <> {user_id} and (mobile= '{mobile}' or whatsapp_number='{mobile}') and society_id={society_id};")
			result = conn.execute(stmt)
			conn.close()
			results = result.fetchone()
			conn.close()
			if results :
				return results
			else:
				return None

	def count_whatsapp_num(self,user_id,whatsapp_number,society_id):
		with engine.connect() as conn:
			stmt = text(f"select count(*) from users where user_id <> {user_id} and (mobile= '{whatsapp_number}' or whatsapp_number='{whatsapp_number}') and society_id={society_id};")
			result = conn.execute(stmt).first()
			conn.close()
			return result if result else None
			
	def get_allstates(self):
		with engine.connect() as conn:
			stmt = text("select * from states")
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None
	
	def get_states(self,country_id):
		with engine.connect() as conn:
			stmt = text("select * from states where country_id   = "+str(country_id)+" order by state_name asc;")
			results = conn.execute(stmt).all()
			conn.close()
			return  [dict(r._mapping) for r in results] if results else None
	
	def get_app_list(self):
		with engine.connect() as conn:
			stmt = text("select * from m_app_type;")
			results = conn.execute(stmt).all()
			conn.close()
			return  [dict(r._mapping) for r in results] if results else None
		
	def get_backoffice_access(self,email,society_id):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_get_backoffice_access",[email,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 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 get_peer_support_office_members(self):
		with engine.connect() as conn:
			stmt = text("select * from peer_support_office_members where is_active=1")
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

	def insert_peer_support_communication(self,data):
		with engine.connect() as conn:
			result = conn.execute(self.peer_support_communication.insert(), data)
			conn.commit()
			pk_id = result.inserted_primary_key[0] if result.inserted_primary_key[0] else None
			conn.close()
			return pk_id

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