Sindbad~EG File Manager

Current Path : /home/numerotech/conf.numerotech.com/conference_dashboard/core_old/model/
Upload File :
Current File : //home/numerotech/conf.numerotech.com/conference_dashboard/core_old/model/BadgeMatrixModel.py

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

class BadgeMatrixModel():
	def __init__(self):
		try:
			self.meta = MetaData()
			self.conf_badge_matrix = Table("conf_badge_matrix", self.meta,  autoload_with=engine)
			self.badge_role        = Table("badge_role", self.meta,  autoload_with=engine)

		except Exception as e:
			print("table not found",e)

	def get_conf(self,conf_id,conf_key):
		with engine.connect() as conn:
			stmt = text("select c.conf_id,c.conf_schema,c.conf_name,c.conf_title,c.conf_key,c.username,c.badge_login_password,c.conf_uuid,c.regdesk_pin,c.conf_name_full_form,c.conf_domain,c.society_id,c.is_active,c.conf_start_time,c.conf_end_time,c.reg_start_on,c.reg_end_on,c.mail_header_logo,c.header_logo,c.header_text,c.index_content,c.backup_email_2,c.pg_appmaster_id,c.pg_is_test,c.payment_api_url,c.call_payment_url,c.stylesheet,c.favicon_url,c.tariff_url,c.is_email_enable,is_requried_fields,c.kc_del_session_table_name,c.kc_session_entry_table_name,c.del_table_name,c.fc_del_session_table_name,fc_session_entry_table_name,c.is_regdesk_open,c.signup_table,c.e_support_email,c.e_backup_emails,c.e_from_email,c.e_reply_name,c.e_reply_to,c.pg_details,s.society_id,sa.is_gen_number,sa.paymentgateway_api_url,sa.paymentgateway_appmaster_id,sa.paymentgateway_is_test,sa.app_url,s.clarity_script,ms.driver,ms.domain,ms.secret_key,c.addons_member_list,sa.del_subheader_text,dc.dt_text_color,dc.dt_background_color,dc.background_color,dc.background_tariff_color,dc.header_text_color,dc.header_background_color,dc.tr_background_color,dc.td_text_color,dc.footer_text_color,dc.footer_background_color from conference c  inner join societies s on s.society_id = c.society_id  left join society_applications sa on sa.conf_id = c.conf_id  and sa.app_type_id = 2  left join mail_setting ms on ms.mail_setting_id = sa.mail_setting_id left join daily_digest_mail_color_template dc on dc.conf_id = c.conf_id   where c.conf_id = "+str(conf_id)+" and c.conf_key='"+str(conf_key)+"'  ;")
			result = conn.execute(stmt)
			result = result.fetchone()
			conn.close()
			return dict(result._mapping) if result else None    


	def get_conf_badge_matrix(self,conf_id):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_conf_del_cbm_get_data",[(conf_id or None)])
			while 1:
				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 insert_conf_badge_matrix(self,data):
		with engine.connect() as conn:
			result = conn.execute(self.conf_badge_matrix.insert(),data)
			conn.commit()
			conn.close()
			return result.lastrowid

	def update_conf_badge_matrix(self,b_id,update_data):
		with engine.connect() as conn:
			stmt    = self.conf_badge_matrix.update().where(self.conf_badge_matrix.c.b_id.in_([b_id])).values(update_data)
			result = conn.execute(stmt)
			conn.commit()
			conn.close()
			return 'success' if result else 'fail'

	def get_role_wise_reg_count(self,conf_id,roles,conf_schema):
		with engine.connect() as conn:
			stmt 	= text("select ifnull(count(d.delegate_id),0) as reg_count FROM "+str(conf_schema)+".delegates d inner join badge_role b on b.b_role = d.role where d.conference_id = "+str(conf_id)+" and d.del_status_id = 2 and b.b_role_id in ("+roles+");")
			result 	= conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result else 0

	def get_del_reg_count(self,conf_id,conf_schema):
		with engine.connect() as conn:
			stmt = text("select (group_concat(distinct b.b_role_id)) as role_ids,(group_concat(distinct d.role)) as roles,min(d.delegate_no) as start_no,max(d.delegate_no) as end_no	from "+str(conf_schema)+".delegates d inner join badge_role b on b.b_role = d.role and b.conf_id = d.conference_id  where d.del_status_id=2 and d.conference_id ="+str(conf_id)+";")
			result = conn.execute(stmt).fetchone()
			conn.close()
			return dict(result._mapping) if result  else None

	def update_del_badge_type(self,conf_id,badge_type_id,conf_schema):
		with engine.connect() as conn:
			stmt2   = text("set sql_safe_updates  = 0 ;")
			conn.execute(stmt2)
			conn.commit()

			stmt    = text("update "+str(conf_schema)+".delegates set badge_type_id = "+str(badge_type_id)+" where conference_id = "+str(conf_id)+" and del_status_id = 2 ;" )
			result = conn.execute(stmt)
			conn.commit()
			conn.close()
			return 'success' if result else 'fail'

	def check_start_end_del_number(self,conf_id,start_no,end_no,b_id):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_conf_del_cbm_check_delegate_number_series",[(conf_id or None),(start_no or None),(end_no or None),(b_id or None)])
			while 1:
				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
				if cursor.description is None:
					break
		finally:
			connection.close()		
		return sets



	def update_role_order_by(self,conf_id,roles):
		with engine.connect() as conn:
			stmt    = text("update badge_role , (SELECT @n := 0) m set order_by =  @n := @n + 1 where conf_id = "+str(conf_id)+" and  b_role_id in ("+str(roles)+") order by field(b_role_id , "+str(roles)+" );")
			result = conn.execute(stmt)
			conn.commit()
			conn.close()
			return 'success' if result else 'fail'



	def get_usp_del_delno_generate_v2(self,conf_id,is_update,b_id,view_type):
		# print(conf_id,is_update,b_id,view_type)
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_conf_del_cbm_delno_generate_v2",[(conf_id or None),(is_update or None),(b_id or None),(view_type or None)])
			while 1:
				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 check_max_del_no(self,conf_id,conf_schema):
		with engine.connect() as conn:
			# stmt = text("select case when d.delegate_no is null then ifnull(max(c.end_no),0)+1 else max(d.delegate_no)+1 end as max_del_no from delegates d inner join conf_badge_matrix c on c.conf_id = d.conference_id where c.conf_id ="+str(conf_id)+" and d.del_status_id in (2,10)  ;")
			stmt = text("select ifnull(max(c.end_no),0)+1 as max_conf_del_no ,ifnull(max(d.delegate_no),0)+1 as max_del_no from "+str(conf_schema)+".delegates d inner join conf_badge_matrix c on c.conf_id = d.conference_id where c.conf_id ="+str(conf_id)+" and d.del_status_id in (2,10)  ;")
			result = conn.execute(stmt).fetchone()
			conn.close()
			return dict(result._mapping) if result  else None

	def get_badge_data_by_id(self,conf_id,b_id):
		with engine.connect() as conn:
			stmt = text("select * from conf_badge_matrix c inner join badge_role b on  find_in_set(b.b_role_id,c.roles) and c.conf_id ="+str(conf_id)+" and c.b_id in ("+ str(b_id) +");")
			result = conn.execute(stmt).fetchone()
			conn.close()
			return dict(result._mapping) if result  else None

	def get_matrix_badge_data(self,conf_id):
		with engine.connect() as conn:
			stmt = text("select * from conf_badge_matrix  where conf_id ="+str(conf_id)+" and del_no_gen_on is not null;")
			result  = conn.execute(stmt).all()
			conn.close()
			results = [dict(r._mapping) for r in result] if result else None
			return results


	def get_matrix_badge_data_by_ids(self,conf_id,b_ids):
		with engine.connect() as conn:
			if b_ids :
				where_con = " and b_id in ("+str(b_ids)+") "
			else:
				where_con = " "

			stmt    = text("select * from conf_badge_matrix  where conf_id ="+str(conf_id)+" and del_no_gen_on is not null "+str(where_con)+";")
			result  = conn.execute(stmt).all()
			conn.close()
			results = [dict(r._mapping) for r in result] if result else None
			return results


	def delete_conf_badge_matrix(self,b_id,conf_id):
		try:
			with engine.connect() as conn:
				stmt   = self.conf_badge_matrix.delete().where(self.conf_badge_matrix.c.b_id.in_([b_id]))
				result = conn.execute(stmt)
				conn.commit()
				stmt2  = text("select * from conf_badge_matrix  where conf_id ="+str(conf_id)+" limit 1;")
				result1 = conn.execute(stmt2)
				results = result1.first()
				conn.close()
				return dict(results._mapping) if results else None

		except Exception as e:
			raise e

	def get_delegate_already_exists(self,start_no,end_no,conf_id,conf_schema):
		with engine.connect() as conn:
			stmt= text("select * FROM "+str(conf_schema)+".delegates WHERE conference_id ="+str(conf_id)+" and  delegate_no BETWEEN "+str(start_no)+" AND "+str(end_no)+" limit 1;")
			result = conn.execute(stmt)
			result = result.first()
			conn.close()
			return dict(result._mapping) if result else None

	def insert_update_delete_records(self,insert_query):
		with engine.connect() as conn:
			try:
				stmt2   = text("set sql_safe_updates  = 0 ;")
				conn.execute(stmt2)
				conn.commit()

				stmt        = text(insert_query)
				results     = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return str(e)

	def get_max_del_no(self,conf_id,conf_schema):
		with engine.connect() as conn:
			stmt = text("select cbm.b_id,ifnull(count(d.delegate_no),0) as count_del_no,ifnull(max(d.delegate_no),0) as max_del_no,cbm.end_no from "+str(conf_schema)+".delegates d inner join conf_badge_matrix cbm on cbm.conf_id=d.conference_id where d.conference_id ="+str(conf_id)+" and d.del_status_id in (2) and cbm.is_existing_del_number = 1 ;")
			result = conn.execute(stmt).fetchone()
			conn.close()
			return dict(result._mapping) if result  else None



	def getRole(self,conf_id,conf_schema):
		with engine.connect() as conn:
			stmt    = text("select b.*,count(d.role) as role_count from badge_role b left join "+str(conf_schema)+".delegates d on d.role = b.b_role and d.conference_id = b.conf_id where b.conf_id = "+str(conf_id)+" group by b.b_role;")
			result  = conn.execute(stmt).all()
			conn.close()
			results = [dict(r._mapping) for r in result] if result else None
			return results


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

	def deleteBadgeRole(self,b_role_id):
		with engine.connect() as conn:
			stmt   = self.badge_role.delete().where(self.badge_role.c.b_role_id.in_([b_role_id]))
			result = conn.execute(stmt)
			conn.commit()
			conn.close()
			return result

	def insertBadgeRole(self,data):
		with engine.connect() as conn:
			result = conn.execute(self.badge_role.insert(), data)
			conn.commit()
			conn.close()
			return result.lastrowid


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

	def get_delegate_details_by_del_nos(self,delegate_nos,from_del_no,to_del_no,conf_id):
		print("usp_conf_del_delegate_no_search")
		print(delegate_nos,from_del_no,to_del_no,conf_id)
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_conf_del_delegate_no_search",[(delegate_nos or None),(from_del_no or None),(to_del_no or None),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()
			connection.close()
			return results
		else :
			cursor.close()
			connection.commit()
			connection.close()
			return None

	def update_delegate_role(self,conf_id,role,delegate_nos,updated_at,is_gen_number,b_role_id,conf_schema):
		print("update_delegate_role")
		print(conf_id,role,delegate_nos,updated_at,is_gen_number,b_role_id,conf_schema)
		with engine.connect() as conn:
			if is_gen_number == 'ref_number' :
				where_con = " and ref_no in ('"+str(delegate_nos)+"') "
			else :
				where_con = " and delegate_no in ('"+str(delegate_nos)+"') "

			conn    = engine.connect()
			stmt 	= text("update "+str(conf_schema)+".delegates set role = '"+str(role)+"',updated_at = '"+str(updated_at)+"',is_bo_role_update = 1,role_id = "+str(b_role_id)+" where conference_id = "+str(conf_id)+" and del_status_id = 2 "+where_con+" ; ")
			print(stmt)
			result  = conn.execute(stmt)
			conn.commit()
			conn.close()
			return 'success' if result else 'fail'
			


	def get_rolewise_count(self,conf_id,conf_schema):
		with engine.connect() as conn:
			conn    = engine.connect()
			stmt 	= text(" select del_status_id,role,case when del_status_id = 10 then 1 else 0 end  as is_spot_delegate,"
						+ " case when del_status_id = 6 then 1 else 0 end  as is_hidden_delegate,"
						+ " case when del_status_id in (3,8) then 1 else 0 end  as is_waiting_delegate,"
						+ " count(delegate_id) as total_count,count(delegate_no) as gen_count,count(delegate_id) - count(delegate_no) as not_gen_count "
						+ " from "+str(conf_schema)+".delegates where conference_id = "+str(conf_id)+" and del_status_id in (2,6,10,3,8) "
						+ " group by del_status_id ,role;")
			result  = conn.execute(stmt).all()
			conn.close()
			results = [dict(r._mapping) for r in result] if result else None
			return results


	def get_role_ids(self,conf_id):
		with engine.connect() as conn:
			stmt = text("select  b_role_id,b_role from badge_role where conf_id ="+str(conf_id)+" and is_visible = 1 ;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			conn.close()
			return results
		
	def get_badge_spot_data(self,conf_id,b_id,conf_schema):
		with engine.connect() as conn:
			stmt = text("select max(delegate_no) as max_del_no from "+str(conf_schema)+".delegates where delegate_no is not null and badge_type_id = "+str(b_id) +" and conference_id = "+str(conf_id) +" and is_spot = 1 limit 1 ;")
			result = conn.execute(stmt).fetchone()
			conn.close()
			return dict(result._mapping) if result  else None

	def get_cbm_data(self,conf_id,b_id):
		with engine.connect() as conn:
			stmt = text("select * from conf_badge_matrix where b_id = "+str(b_id) +" and conf_id = "+str(conf_id) +" limit 1 ;")
			result = conn.execute(stmt).fetchone()
			conn.close()
			return dict(result._mapping) if result  else None
	
	def check_role_id_mapped_or_not(self,conf_id):
		with engine.connect() as conn:
			stmt = text("select * from badge_role where conf_id = "+str(conf_id)+";")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			conn.close()
			return results

	def get_delegate_roles(self,conf_id,conf_schema):
		with engine.connect() as conn:
			stmt = text("select group_concat(delegate_no) as delegate_nos,role from "+str(conf_schema)+".delegates where conference_id = "+str(conf_id)+" and role_id is null and del_status_id=2 group by role ;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			conn.close()
			return results


	def update_delegate_role_id(self,conf_id,delegate_nos,updated_at,is_gen_number,b_role_id,conf_schema,role):
		with engine.connect() as conn:
			
			if is_gen_number != 'ref_number' :
				# where_con = " and delegate_no in ("+str(delegate_nos)+") "
				where_con = " and role ='"+str(role)+"' "

			conn    = engine.connect()
			stmt2   = text("set sql_safe_updates  = 0 ;")
			conn.execute(stmt2)
			conn.commit()

			stmt 	= text("update "+str(conf_schema)+".delegates set updated_at = '"+str(updated_at)+"',is_bo_role_update = 1,role_id = "+str(b_role_id)+" where conference_id = "+str(conf_id)+" and del_status_id = 2 "+where_con+" ; ")			
			print(stmt)
			result  = conn.execute(stmt)
			conn.commit()
			conn.close()
			return 'success' if result else 'fail'

	def get_role_by_name(self,conf_id,role):
		with engine.connect() as conn:
			stmt = text("select * from badge_role where conf_id = "+str(conf_id)+" and b_role = '"+str(role)+"';")
			result = conn.execute(stmt).fetchone()
			conn.close()
			return dict(result._mapping) if result  else None

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