Sindbad~EG File Manager

Current Path : /home/numerotech/admin.numerotech.com/admin_app_v1/core/model/
Upload File :
Current File : //home/numerotech/admin.numerotech.com/admin_app_v1/core/model/DelegateAdminModel.py

from flask import session
from flask import request, Blueprint, jsonify
from sqlalchemy import create_engine, select, MetaData, Table,text
from sqlalchemy.sql import and_, or_

from core import app

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

class DelegateAdminModel():  
	def __init__(self):
		try: 
			# conference
			self.meta = MetaData()
			self.society_applications        = Table("society_applications", self.meta, autoload_with=engine)
			
		except Exception as e:
			print(e)


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

	def duplicate_app_config(self,conf_id,society_id,get_conf_id):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_create_duplicate_app_config",[conf_id,society_id,get_conf_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 get_conf_data_config(self,society_id):
		with engine.connect() as conn:
			stmt   = text("select c.conf_name,c.conf_id from conference c inner join society_applications sa on c.conf_id = sa.conf_id where sa.society_id = "+str(society_id)+" and  sa.app_type_id = 2 and sa.conf_id is not null group by sa.conf_id order by c.conf_id  ;")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None


	def get_societies_config(self):
		with engine.connect() as conn:
			stmt    = text("select s.society_id,s.society_name from societies s left join society_applications sa on sa.society_id = s.society_id where sa.app_type_id = 2  group by sa.society_id order by s.society_id;")
			result  = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None

	def get_societies_config_by_society_id(self,conf_id,society_id):
		with engine.connect() as conn:
			stmt    = text(" select * from society_applications  where conf_id = "+str(conf_id)+" and society_id = "+str(society_id)+" and app_type_id = 2  ;")
			result  = conn.execute(stmt).one_or_none()
			results = dict(result._mapping)  if result else None
			return results

	def insert_update_config_data(self,app_type_id,conf_id,society_id,societie_data,data):
		with engine.connect() as conn:
			try:
				if societie_data :
					stmt = self.society_applications.update().where(self.society_applications.c.app_type_id.in_([app_type_id]) & (self.society_applications.c.conf_id == conf_id)).values(data)
					results     = conn.execute(stmt)
					conn.commit()
					stmt    = text(" select * from society_applications  where conf_id = "+str(conf_id)+" and society_id = "+str(society_id)+" and  app_type_id = 2  ;")
					result  = conn.execute(stmt).one_or_none()
					results = dict(result._mapping)  if result else None
					return results,{"status":2}

				else:
					stmt = self.society_applications.insert().values(data)
					result   = conn.execute(stmt)
					conn.commit()
					pk_id = result.inserted_primary_key[0] if result.inserted_primary_key[0] else None
					if pk_id:
						stmt    = text(" select * from society_applications  where app_id = "+str(pk_id)+" and  app_type_id = 2  ;")
						result  = conn.execute(stmt).one_or_none()
						results = dict(result._mapping)  if result else None
						return results,{"status":1}
					else:
						return None
			except Exception as e:
				print("error -  ",str(e))
				return str(e)
				
	def get_delegate_applications(self,user_mail):
		with engine.connect() as conn:
			stmt    = text("SELECT sa.*,c.*,case"
						+" when sa.is_active = 1 and sa.app_host is not null then 'active' "
						+" when sa.is_active = 0 and sa.app_host is null then 'inactive' "
						+" when sa.is_active = 0 and sa.app_host is not null then 'inactive' "
						+" when  sa.app_host is null then 'incomplete'"
						+" end as del_status,u.user_id,u.user_uuid"
						+" FROM society_applications sa"
						+" left join conference c on c.conf_id = sa.conf_id"
						+" inner join users u on u.society_id = sa.society_id"
						+f" where sa.app_type_id=2 and u.email = '{user_mail}' group by sa.app_id order by c.conf_id;")
			result  = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None
	
	def get_delegate_status_count(self):
		with engine.connect() as conn:
			stmt = text("select count(*) as delegate_app,"
					+" sum(case when is_active = 1 and app_host is not null then 1 else 0 end) as active_count,"
					+" sum(case when is_active = 0 and app_host is not null then 1 else 0 end) as inactive_count,"
					+" sum(case when is_active is null and app_host is null then 1 else 0 end) as incomplete"
					+" from society_applications" 
					+" where app_type_id=2 ;")
			result = conn.execute(stmt).first()
			conn.close()
			return  dict(result._mapping) if result else None

	def get_conference(self,conf_id,conf_key):
		with engine.connect() as conn:
			stmt =text("select * from conference where conf_id ="+str(conf_id)+" and conf_key = '"+conf_key+"';")
			result = conn.execute(stmt).one_or_none()
			conn.close()
			if result :
				return dict(result._mapping)
			else:
				return None

	def get_addon_name(self,conf_id):
		with engine.connect() as conn:
			stmt =text("select ifnull(addon_name,display_name) as addon_name from addons where  is_visible = 1 and conference_id = "+str(conf_id)+"  group by ifnull(addon_name,display_name) ;")
			result = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			conn.close()

			if results : 
				return results
			else:
				return None

	def get_tariff_data(self,conf_id,addon_name):
		with engine.connect() as conn:
			if addon_name :
				where_con = " and ifnull(a.addon_name,a.display_name) = '"+addon_name+"' order by  a.start_by,  a.addon_id ASC"
			else :
				where_con = " order by a.start_by, a.addon_id ASC"
			sql_stmt = 	"select a.addon_id,a.addon_name,a.start_by as startby,a.end_by as endby,a.addon_reg_type,a.conference_id ,a.reg_type,a.amount,a.display_name,date_format(a.start_by , '%d-%m-%Y %H:%i:%s') as start_by ,date_format(a.end_by , '%d-%m-%Y %H:%i:%s') as end_by,ut.user_type from addons a left join user_types ut on ut.user_type_id = a.user_type_id where a.conference_id = "+str(conf_id)+" and a.is_visible =1 "
			stmt =text(sql_stmt + where_con)
			result = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			conn.close()
			
			if results : 
				return results
			else:
				return None


	def update_addon_data(self,conf_id,addon_id,data_1,curr_dt):
		with engine.connect() as conn:
			stmt = self.addons.update().where(self.addons.c.addon_id.in_([addon_id]),self.addons.c.conference_id.in_([conf_id])).values(data_1)
			result = conn.execute(stmt)
			conn.commit()
			stmt_1 =text("select * from addons where conference_id = '"+str(conf_id)+"' and updated_at = '"+curr_dt+"';")
			results = conn.execute(stmt_1).one_or_none()
			if results :
				return [dict(results._mapping)]
			else:
				return 'fail'
	
	def get_mail_settings(self):
		with engine.connect() as conn:
			stmt =text("select * from mail_setting")
			result = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			conn.close()
			if results : 
				return results
			else:
				return None
	# view tariff

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