Sindbad~EG File Manager

Current Path : /home/numerotech/conf-badge.numerotech.com/conference_badge/core/model/
Upload File :
Current File : //home/numerotech/conf-badge.numerotech.com/conference_badge/core/model/BadgeModel.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 sqlalchemy.exc import SQLAlchemyError

from sqlalchemy.dialects.mysql import insert


from core import app

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


class BadgeModel():  
	def __init__(self):
		try:
			self.meta = MetaData()
			self.badge_template       = Table("badge_template", self.meta, autoload_with=engine)
			self.badge_fields         = Table("badge_fields", self.meta, autoload_with=engine)
			self.badge_temp_role_pg   = Table("badge_temp_role_pg", self.meta, autoload_with=engine)
			self.badge_condition      = Table("badge_condition", self.meta, autoload_with=engine)


			
		except Exception as e:
			print(e)

	def GetConf(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+"'  limit 1;")
			result = conn.execute(stmt)
			result = result.first()
			conn.close()
			return dict(result._mapping) if result else None

	def get_m_badge_fields(self,conf_id):
		with engine.connect() as conn:
			stmt   = text("select * from m_badge_field  where   is_visible = 1")
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

	def get_badge_roles(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()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None


	def insert_badge_style(self,insert_query):
		with engine.connect() as conn:
			try:
				stmt        = text(insert_query)
				results     = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return str(e)

	def badge_template_data_save(self,conf_id,btemp_id,data):
		with engine.connect() as conn:
			try:
				if btemp_id > 0:
					stmt = self.badge_template.update().where(self.badge_template.c.btemp_id.in_([btemp_id]) & (self.badge_template.c.conf_id == conf_id)).values(data)
					results     = conn.execute(stmt)
					conn.commit()
					conn.close()
					return "success"
				else:
					stmt = self.badge_template.insert().values(data)
					result   = conn.execute(stmt)
					conn.commit()
					conn.close()
					pk_id = result.inserted_primary_key[0] if result.inserted_primary_key[0] else None
					return pk_id


			except Exception as e:
				print("error -  ",str(e))
				return str(e)
	
	def get_badge_temp_data(self,conf_id,btemp_id):
		with engine.connect() as conn:
			stmt = text("select * from badge_template where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+";")
			result = conn.execute(stmt).first()
			conn.close()
			results = dict(result._mapping)  if result else None
			return results

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


	def get_badge_style(self,conf_id,btemp_id):
		with engine.connect() as conn:
			stmt = text("select * from badge_template where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+";")
			result = conn.execute(stmt).first()
			conn.close()
			results = dict(result._mapping)  if result else None
			return results

	def update_image_file(self,conf_id,btemp_id,img_url,badge_type,curr_dt):
		with engine.connect() as conn:
			try:
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				if badge_type == 'front':
					stmt        = text("update badge_template set  b_background_img = '"+img_url+"',back_img_updated_at='"+curr_dt+"' where conf_id = "+str(conf_id)+" and btemp_id="+str(btemp_id)+";")
				else:
					stmt        = text("update badge_template set  comm_badge_url = '"+img_url+"',back_img_updated_at='"+curr_dt+"' where conf_id = "+str(conf_id)+" and btemp_id="+str(btemp_id)+";")
				restult_1 = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return str(e)

	def update_image_file_role_wise(self,conf_id,btemp_id,img_url,badge_type,btr_bg_id,curr_dt):
		with engine.connect() as conn:
			try:
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				if badge_type == 'front':
					stmt        = text("update badge_temp_role_pg set bg_url = '"+img_url+"',created_at = '"+curr_dt+"' where conf_id = "+str(conf_id)+" and btemp_id="+str(btemp_id)+" and btr_bg_id = "+str(btr_bg_id)+";")
				elif badge_type == 'commitment':
					stmt        = text("update badge_temp_role_pg set comm_bg_url = '"+img_url+"',created_at = '"+curr_dt+"' where conf_id = "+str(conf_id)+" and btemp_id="+str(btemp_id)+" and btr_bg_id = "+str(btr_bg_id)+";")
				else:
					stmt        = text("update badge_temp_role_pg set back_bg_url = '"+img_url+"',created_at = '"+curr_dt+"' where conf_id = "+str(conf_id)+" and btemp_id="+str(btemp_id)+" and btr_bg_id = "+str(btr_bg_id)+";")
				restult_1 = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return str(e)

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

	def check_badge_template_name(self,conf_id,template_name,btemp_id):
		with engine.connect() as conn:
		  stmt = text("select * from badge_template where conf_id = "+str(conf_id)+" and template_name = '"+(template_name)+"' and btemp_id not in ("+str(btemp_id)+");")
		  result = conn.execute(stmt).first()
		  conn.close()
		  results = dict(result._mapping)  if result else None
		  return results

	def delete_file_nameand_path(self,conf_id,btemp_id,badge_type):
		with engine.connect() as conn:
			try:
				if badge_type == 'front':

					stmt = text("Update badge_template SET b_background_img = null where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+"; ")
				else:
					stmt = text("Update badge_template SET comm_badge_url = null where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+"; ")

				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return str(e)

	def delete_file_nameand_path_role_wise(self,conf_id,btr_bg_id,badge_type):
		with engine.connect() as conn:
			try:
				if badge_type == 'front':
					stmt = text("Update badge_temp_role_pg SET bg_url = null where conf_id = "+str(conf_id)+" and btr_bg_id = "+str(btr_bg_id)+";")
				elif badge_type == 'commitment':
					stmt = text("Update badge_temp_role_pg SET comm_bg_url = null where conf_id = "+str(conf_id)+" and btr_bg_id = "+str(btr_bg_id)+";")
				else:
					stmt = text("Update badge_temp_role_pg SET back_bg_url = null where conf_id = "+str(conf_id)+" and btr_bg_id = "+str(btr_bg_id)+";")
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return str(e)

	def delete_badge_field_is_active(self,conf_id,bf_id,btemp_id,field_id):
		with engine.connect() as conn:
			try:
				stmt = text("delete from badge_fields where bf_id = "+str(bf_id)+" and conf_id = "+str(conf_id)+"");
				results   = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return str(e)

	def hide_badge_field_is_active(self,conf_id,bf_id,btemp_id,data):
		with engine.connect() as conn:
			try:
				stmt = self.badge_fields.update().where(self.badge_fields.c.bf_id.in_([bf_id])).values(data)
				results   = conn.execute(stmt)
				conn.commit()
				stmt = text("select * from badge_fields bf inner join m_badge_field mbf on mbf.field_id = bf.field_id where bf.conf_id = "+str(conf_id)+" and bf.btemp_id = "+str(btemp_id)+" and bf_id = "+str(bf_id)+"  ;")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
				
			except Exception as e:
				return str(e)

	def update_badge_field_data(self,conf_id,btemp_id,bf_id,data):
		with engine.connect() as conn:
			try:
				stmt = self.badge_fields.update().where(self.badge_fields.c.bf_id.in_([bf_id])).values(data)
				results   = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return "failed " + str(e)

	def update_template_contant(self,conf_id,btemp_id,badge_field_html,badge_field_html_pdf,badge_field_html_print):
		with engine.connect() as conn:
			try:
				badge_field_html 		= badge_field_html.replace("'","\\'")
				badge_field_html_pdf 	= badge_field_html_pdf.replace("'","\\'")
				badge_field_html_print 	= badge_field_html_print.replace("'","\\'")
				stmt = text("Update badge_template SET template_contant = '"+badge_field_html+"',badge_pdf_contant = '"+badge_field_html_pdf+"',pp_contant = '"+badge_field_html_print+"' where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+" ;")
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				print("Error - ",str(e))
				return str(e)

	def get_badge_field(self,conf_id,btemp_id,field_id):
		with engine.connect() as conn:
			stmt = text("select bf_id from badge_fields where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+" and field_id = '"+field_id+"';")
			result = conn.execute(stmt).first()
			conn.close()
			results = dict(result._mapping)  if result else None
			return results

	def get_role_wise_badge(self, conf_id, btemp_id, role_id):
		try:
			with engine.connect() as conn:
				
				stmt = text("select * from badge_temp_role_pg where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+" and role_id = "+str(role_id)+";")
				
				result = conn.execute(stmt).first()
				conn.close()
				results = dict(result._mapping)  if result else None
				return results
		except Exception as e:
			return f"Error: {str(e)}"


	def insert_role_wise_badge(self, conf_id, btemp_id, data):
		try:
			with engine.connect() as conn:
				result = conn.execute(self.badge_temp_role_pg.insert(), data)
				conn.commit()
				btr_bg_id = result.inserted_primary_key[0] if result.inserted_primary_key[0] else None
				stmt = text("select * from badge_temp_role_pg btr inner join badge_role br on btr.conf_id = br.conf_id and br.b_role_id = btr.role_id where btr.conf_id = "+str(conf_id)+" and btr.btemp_id = "+str(btemp_id)+" and btr_bg_id ="+str(btr_bg_id)+" order by btr.created_at  desc ; ")
				result = conn.execute(stmt).first()
				conn.close()
				return dict(result._mapping) if result else None
		except Exception as e:
				return str(e)

	def get_role_wise_badge_all(self, conf_id, btemp_id):
		try:
			with engine.connect() as conn:
				stmt = text("select * from badge_temp_role_pg btr inner join badge_role br on btr.conf_id = br.conf_id and br.b_role_id = btr.role_id where btr.conf_id = "+str(conf_id)+" and btr.btemp_id = "+str(btemp_id)+"; ")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
				# return "success"
		except Exception as e:
				return str(e)

	def get_condition(self,btemp_id):
		try:
			with engine.connect() as conn:
				stmt = text("select * from badge_condition  where btemp_id in  ("+str(btemp_id)+"); ")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
				# return "success"
		except Exception as e:
				return str(e)

	def get_role_wise_badge_all_v1(self, conf_id, btemp_id):
		try:
			with engine.connect() as conn:
				stmt = text("select * from badge_temp_role_pg btr inner join badge_role br on btr.conf_id = br.conf_id and br.b_role_id = btr.role_id where btr.conf_id = "+str(conf_id)+" and btr.btemp_id in ("+str(btemp_id)+"); ")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
				# return "success"
		except Exception as e:
				return str(e)

	def insert_b_field_name(self,conf_id,field_id,btemp_id,data):
		with engine.connect() as conn:
			try:
				stmt_3 = self.badge_fields.insert().values(data)
				result   = conn.execute(stmt_3)
				conn.commit()
				bf_id = result.inserted_primary_key[0] if result.inserted_primary_key[0] else None
				stmt_4 = text("select * from badge_fields bf inner join m_badge_field mbf on mbf.field_id = bf.field_id where bf.conf_id = "+str(conf_id)+" and bf.btemp_id = "+str(btemp_id)+" and bf.bf_id="+str(bf_id)+" order by  bf.created_at desc ;")
				result = conn.execute(stmt_4).first()
				conn.close()
				return dict(result._mapping) if result else None
			except Exception as e:
				return str(e)

	def get_badge_field_all(self,conf_id,btemp_id):
		with engine.connect() as conn:
			stmt = text("select * from badge_fields bf inner join m_badge_field mbf on mbf.field_id = bf.field_id  where bf.conf_id = "+str(conf_id)+" and bf.btemp_id = "+str(btemp_id)+"  ;")
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None


	def get_badge_template_with_role(self,conf_id,btemp_id):
		with engine.connect() as conn:
			stmt = text("select * from badge_role br inner join badge_template bt on bt.btemp_id = br.btemp_id inner join badge_fields bf on bf.btemp_id = br.btemp_id where br.conf_id = "+str(conf_id)+" and bt.btemp_id = "+str(btemp_id)+";")
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None


	def dataForBadgePrint(self,conf_id,role,counter,batch,del_no_from,del_no_to,del_table_name,role_ids,badge_limit,is_badge,badge_validation,sample_badge,plain_badge,is_commitment,btemp_ids):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_delegate_badge_data_v5",[conf_id,role,counter,batch,del_no_from,del_no_to,del_table_name,role_ids,badge_limit,is_badge,badge_validation,sample_badge,plain_badge,is_commitment,btemp_ids])
		
		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 update_horizontal_aline(self,conf_id,bf_id,btemp_id,horizontal_aline):
		with engine.connect() as conn:
			try:
				stmt = text("Update badge_fields SET horizontal_aline = '"+horizontal_aline+"' where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+" and bf_id = "+str(bf_id)+" ;")
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				conn.close()
			except Exception as e:
				return str(e)

	def update_vertical_aline(self,conf_id,bf_id,btemp_id,vertical_aline):
		with engine.connect() as conn:
			try:
				stmt = text("Update badge_fields SET vertical_aline = '"+vertical_aline+"' where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+" and bf_id = "+str(bf_id)+" ;")
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				conn.close()
			except Exception as e:
				return str(e)

	def update_is_qr_code(self,conf_id,field_id,bf_id,btemp_id,is_qrcode):		
		with engine.connect() as conn:
			try:
				stmt = text("Update badge_fields SET is_qrcode = "+str(is_qrcode)+" where conf_id = "+str(conf_id)+" and btemp_id ="+str(btemp_id)+" and bf_id = "+str(bf_id)+" ;")
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				stmt = text("select * from badge_fields bf inner join m_badge_field mbf on mbf.field_id = bf.field_id where bf.conf_id = "+str(conf_id)+" and bf.btemp_id = "+str(btemp_id)+" and bf.bf_id="+str(bf_id)+" ;")
				
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
			except Exception as e:
				return str(e)

	def update_role_in_temp(self,conf_id,btemp_id,role_ids):
		with engine.connect() as conn:
			try:
				stmt = text("Update badge_template SET role_id = '"+role_ids+"' where conf_id = "+str(conf_id)+" and btemp_id ="+str(btemp_id)+"  ;")
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				stmt = text("select * from badge_template bt inner join numerote_primary_db.badge_role br on br.conf_id = bt.conf_id  where br.conf_id = "+str(conf_id)+" and bt.btemp_id = "+str(btemp_id)+" and br.b_role_id in ('"+role_ids+"') ;")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
			except Exception as e:
				return str(e)


	def update_is_comm_badge(self,conf_id,btemp_id,is_comm):		
		with engine.connect() as conn:
			try:
				stmt = text("Update badge_template SET is_comm = "+str(is_comm)+" where conf_id = "+str(conf_id)+" and btemp_id ="+str(btemp_id)+" ;")
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				stmt = text("select * from badge_template  where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+"  ;")
				result = conn.execute(stmt).first()
				conn.close()
				results = dict(result._mapping)  if result else None
				return results
			except Exception as e:
				return str(e)


	def update_is_double_side_badge(self,conf_id,btemp_id,is_double_side):		
		with engine.connect() as conn:
			try:
				stmt = text("Update badge_template SET is_double_side = "+str(is_double_side)+" where conf_id = "+str(conf_id)+" and btemp_id ="+str(btemp_id)+" ;")
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				stmt = text("select * from badge_template  where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+"  ;")
				result = conn.execute(stmt).first()
				conn.close()
				results = dict(result._mapping)  if result else None
				return results
			except Exception as e:
				return str(e)

	def update_comm_margin_aline(self,conf_id,btemp_id,data):		
		with engine.connect() as conn:
			try:
				stmt = self.badge_template.update().where(self.badge_template.c.btemp_id.in_([btemp_id]) & (self.badge_template.c.conf_id == conf_id)).values(data)
				results     = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return str(e)

	
	def save_to_mysql_db_core(self):
		try:
			with engine.connect() as conn:
				data_list   = session['students']
				dynamic_table = Table('students', self.meta, autoload_with=engine)
				stmt        = insert(dynamic_table).values(data_list)
				update_dict = {key: stmt.inserted[key] for key in data_list[0] if key != 'id'}
				stmt = stmt.on_duplicate_key_update(**update_dict)
				conn.execute(stmt)
				conn.commit()
				conn.close()
			return 'Data inserted successfully'

		except SQLAlchemyError as e:
			return f'Error inserting data: {str(e)}'			

	def saveBadgeTemplateFromSession(self,badge_templates):
		try:
			with engine.connect() as conn:
				# data_list   = session['students']
				dynamic_table = Table('badge_template', self.meta, autoload_with=engine)
				stmt        = insert(dynamic_table).values(badge_templates)
				update_dict = {key: stmt.inserted[key] for key in badge_templates if key != 'btemp_id'}
	
				stmt = stmt.on_duplicate_key_update(**update_dict)
				
				conn.execute(stmt)
				
				conn.commit()
				conn.close()
				
			return 'success'

		except SQLAlchemyError as e:
			return f'Error inserting data: {str(e)}'	

	def saveBadgeFieldsFromSession(self,badge_fields):
		try:
			if badge_fields:
				with engine.connect() as conn:
					# data_list   = session['students']
					dynamic_table = Table('badge_fields', self.meta, autoload_with=engine)
					stmt        = insert(dynamic_table).values(badge_fields)
					update_dict = {key: stmt.inserted[key] for key in badge_fields[0] if key != 'bf_id'}
		
					stmt = stmt.on_duplicate_key_update(**update_dict)
					
					conn.execute(stmt)
					
					conn.commit()
					conn.close()
			return 'success'

		except SQLAlchemyError as e:
			print(f'Error inserting data: {str(e)}')
			return f'Error inserting data: {str(e)}'


	def get_badge_template_with_role_index(self,conf_id):
		with engine.connect() as conn:
			stmt = text("select * from badge_temp_role_pg btr inner join badge_role br on btr.conf_id = br.conf_id and br.b_role_id = btr.role_id where br.conf_id = "+str(conf_id)+";")
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

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

	# Multiple result set store proc function
	def GetBatchCounterRole(self,conf_id,del_table_name):
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_counter_batch_role_v1",[conf_id,del_table_name])
			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 delete_badge_role(self,conf_id,btemp_id,btr_bg_id):
		with engine.connect() as conn:
			try:
				stmt = text("delete from badge_temp_role_pg where  conf_id = "+str(conf_id)+" and  btemp_id = "+str(btemp_id)+" and btr_bg_id = "+str(btr_bg_id)+" ");
				results   = conn.execute(stmt)
				conn.commit()
				stmt = text("select * from badge_temp_role_pg btr inner join badge_role br on btr.conf_id = br.conf_id and br.b_role_id = btr.role_id where btr.conf_id = "+str(conf_id)+" and btr.btemp_id="+str(btemp_id)+"; ")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
				
			except Exception as e:
				return str(e)
				
	def saveGalleyColData(self,galley_data):
		try:
			if galley_data:
				with engine.connect() as conn:
					# data_list   = session['students']
					dynamic_table = Table('galley_sheet_headers', self.meta, autoload_with=engine)
					stmt        = insert(dynamic_table).values(galley_data)
					update_dict = {key: stmt.inserted[key] for key in galley_data[0] if key != 'gsh_id'}
		
					stmt = stmt.on_duplicate_key_update(**update_dict)
					
					conn.execute(stmt)
					
					conn.commit()
					conn.close()
			return 'success'

		except SQLAlchemyError as e:
			print(f'Error inserting data: {str(e)}')
			return f'Error inserting data: {str(e)}'


	def GetGalleydata(self,conf_id):
		with engine.connect() as conn:
			stmt = text("select * from galley_sheet_headers where conf_id = "+str(conf_id)+" ; ")

			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None


	def insert_font_family_to_db(self,insert_query):
		with engine.connect() as conn:
			try:
				stmt = text(insert_query);
				result   = conn.execute(stmt)
				conn.commit()
				stmt = text("select * from m_fonts ;")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
				# return "success"
			except Exception as e:
				return str(e)

	def get_font_family_data(self):
		with engine.connect() as conn:
			try:
				stmt = text("select * from m_fonts ;")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
			except Exception as e:
				return str(e)


	def check_file_name_file_type(self,main_name,font_type):
		with engine.connect() as conn:
			try:
				stmt = text("select * from m_fonts where font_name = '"+main_name+"' and font_type = '"+font_type+"';")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
				
			except Exception as e:
				return str(e)



	def delete_font_file(self,font_id):
		with engine.connect() as conn:
			try:
				stmt = text("delete from m_fonts where font_id = "+str(font_id)+" ");
				results   = conn.execute(stmt)
				conn.commit()
				stmt = text("select * from m_fonts ;")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
				# return "success"
			except Exception as e:
				return str(e)


	def get_font_family_data_single(self,font_id):
		with engine.connect() as conn:
			stmt = text("select * from m_fonts where font_id = "+str(font_id)+";")
			result = conn.execute(stmt)
			result = result.first()
			conn.close()
			return dict(result._mapping) if result else None
	

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

	def insert_and_update_badge_condition(self,btemp_id,data,bcon_id):
		with engine.connect() as conn:
			try:
				if bcon_id:
					stmt = self.badge_condition.update().where(self.badge_condition.c.btemp_id.in_([btemp_id]) & (self.badge_condition.c.bcon_id == bcon_id)).values(data)
					results     = conn.execute(stmt)
					conn.commit()
					conn.close()
					return "success"
				else:
					stmt = self.badge_condition.insert().values(data)
					result   = conn.execute(stmt)
					conn.commit()
					conn.close()
					bcon_id = result.inserted_primary_key[0] if result.inserted_primary_key[0] else None
					return bcon_id
			except Exception as e:
				print("error -  ",str(e))
				return str(e)
				
	def update_sticker_image(self,conf_id,bf_id,btemp_id,img_url):
		with engine.connect() as conn:
			try:
				stmt = text("Update badge_fields SET sticker_url = '"+img_url+"' where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+" and bf_id = "+str(bf_id)+" ;")
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				conn.close()
			except Exception as e:
				return str(e)


	def remove_sticker_data(self,conf_id,btemp_id,bf_id):
		with engine.connect() as conn:
			try:
				stmt = text("Update badge_fields SET sticker_url = null where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+" and bf_id = "+str(bf_id)+" ; ")
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				results     = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				return str(e)
	def get_del_addons(self,conf_id,conf_schema):
		with engine.connect() as conn:
			stmt   = text("select distinct(a.addon_id),a.display_name from "+conf_schema+".delegates d inner join delegates_addons da on d.delegate_id = da.delegate_id inner join addons a on  da.addon_id = a.addon_id where d.conference_id = "+str(conf_id)+" and a.addon_type_id != 1 and da.reg_status = 2 and d.del_status_id = 2;")
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

	def create_badge_template(self,conf_id,btemp_id):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_create_dupliacte_badge_template",[conf_id,btemp_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 delete_badge_template(self,conf_id,btemp_id):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_delete_badge_template",[conf_id,btemp_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 duplicate_badge_field(self,conf_id,btemp_id,bf_id):
		connection = engine.raw_connection()
		cursor = connection.cursor()
		cursor.callproc("usp_create_dupliacte_badge_field",[conf_id,btemp_id,bf_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_badge_field_data(self,conf_id,btemp_id,bf_id):
		with engine.connect() as conn:
			stmt = text("select * from badge_fields where conf_id = "+str(conf_id)+" and btemp_id = "+str(btemp_id)+" and bf_id = "+str(bf_id)+" ;")
			result = conn.execute(stmt).first()
			conn.close()
			results = dict(result._mapping)  if result else None
			return results

	# def insert_badge_condition(self,btemp_id,data):
	# 	with engine.connect() as conn:
	# 		try:
	# 			stmt = self.badge_condition.insert().values(data)
	# 			result   = conn.execute(stmt)
	# 			conn.commit()
	# 			conn.close()
	# 			bcon_id = result.inserted_primary_key[0] if result.inserted_primary_key[0] else None
	# 			# return bcon_id
	# 			stmt = text("select * from badge_condition where  btemp_id = "+str(btemp_id)+" and bcon_id = "+str(bcon_id)+" ;")
	# 			result = conn.execute(stmt).first()
				
	# 			results = dict(result._mapping)  if result else None
	# 			return results

	# 		except Exception as e:
	# 			print("error -  ",str(e))
	# 			return str(e)

	def insert_badge_condition(self, btemp_id, data):
		with engine.connect() as conn:
			try:
				stmt = self.badge_condition.insert().values(data)
				result = conn.execute(stmt)
				conn.commit()
				bcon_id = result.inserted_primary_key[0] if result.inserted_primary_key else None
				if not bcon_id:
					return None
				stmt = text("select * from badge_condition where btemp_id = "+str(btemp_id)+";")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
			except Exception as e:
				print("error - ", str(e))
				return str(e)

	def update_condition(self,conf_id,bcon_id,btemp_id,condition_value):
		with engine.connect() as conn:
			try:
				safe_update = text("SET SQL_SAFE_UPDATES = 0 ")
				result      = conn.execute(safe_update)
				stmt = text("update badge_condition set `condition` = '"+str(condition_value)+"' where btemp_id = "+str(btemp_id)+" and bcon_id = "+str(bcon_id)+" ;")
				print("--------stmt",stmt)
				restult = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
			except Exception as e:
				print("error - ", str(e))
				return str(e)

	def check_insert_condition(self,btemp_id,condition_value):
		with engine.connect() as conn:
			try:
				stmt = text("select * from badge_condition where btemp_id = "+str(btemp_id)+" and column_name = '"+str(condition_value)+"';")
				print("--------stmt",stmt)
				result = conn.execute(stmt).first()
				conn.close()
				results = dict(result._mapping)  if result else None
				return results
			except Exception as e:
				print("error - ", str(e))
				return str(e)

	def delete_condition(self, conf_id, bcon_id, btemp_id):
		try:
			with engine.connect() as conn:
				stmt = text("delete from badge_condition where btemp_id = "+str(btemp_id)+" and bcon_id = "+str(bcon_id)+";")
				results   = conn.execute(stmt)
				conn.commit()
				conn.close()
				return "success"
		except Exception as e:
			print("Error -", str(e))
			return str(e)

	def after_delete_get_condition(self,conf_id,btemp_id):
		with engine.connect() as conn:
			try:
				stmt = text("select * from badge_condition where btemp_id = "+str(btemp_id)+";")
				result = conn.execute(stmt).all()
				conn.close()
				return [dict(r._mapping) for r in result] if result else None
			except Exception as e:
				print("error - ", str(e))
				return str(e)
				

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