Sindbad~EG File Manager

Current Path : /home/numerotech/conf-certificates.numerotech.com/certificates/core/model/
Upload File :
Current File : //home/numerotech/conf-certificates.numerotech.com/certificates/core/model/CertificateModel.py

from sqlalchemy import create_engine, MetaData, Table, insert, select,update,delete,text
from sqlalchemy.sql import and_, or_
from core import app
from flask import session
import json
from sqlalchemy import text
import sqlite3
engine = create_engine(app.config['DATABASE_URI'],pool_recycle=3600,future=True)

class CertificateModel():
	def __init__(self):
		try:
			self.meta = MetaData()
			self.certificates       = Table("certificates",self.meta, autoload_with= engine)

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

	def getNameAndRole(self,del_no,conf_schema):
		with engine.connect() as conn:
			stmt = text("select delegate_no,full_name,role,is_present,d.badge_role from "+conf_schema+".delegates where delegate_no= "+str(del_no)+";")
			result  = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result else None	


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

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

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

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

	def update_cerf_and_getdata(self,data_for_update,cerf_id):
		with engine.connect() as conn:
			stmt = self.certificates.update().where(self.certificates.c.cerf_id.in_([cerf_id])).values(data_for_update)
			restult_1 = conn.execute(stmt)
			conn.commit()

			stmt_2 = text("SELECT * from certificates;")
			result = conn.execute(stmt_2).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

	def delete_cerf_index(self,cerf_id):
		with engine.connect() as conn:
			stmt = self.certificates.delete().where(self.certificates.c.cerf_id.in_([cerf_id]))
			restult_1 = conn.execute(stmt)
			conn.commit()
			stmt_2 = text("select * from certificates;")
			result = conn.execute(stmt_2).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

	def checkCertAlreadyPrint(self,conf_id,cert_id,delegate_no , conf_schema):
		with engine.connect() as conn:
			if cert_id :
				where_con = " and cert_id = "+str(cert_id)+" "
			else :
				# where_con = " and cert_id = "+str(cert_id)+" "
				where_con = "  "

			stmt   = text("select date_format(created_at , '%d-%m-%Y %H:%i:%s') as created_at  from "+conf_schema+".cert_download_logs where delegate_no = "+ str(delegate_no)+" and conf_id= "+str(conf_id)+"  "+where_con+" ;")
			# print(stmt)
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None		


	def saveCertDownloadLogs(self,insert_stmt,update_stmt):
		with engine.connect() as conn:
			if insert_stmt :
				stmt   = text(insert_stmt)
				result = conn.execute(stmt)
				conn.commit()

			if update_stmt :
				stmt2   = text("set sql_safe_updates  = 0 ;")
				conn.execute(stmt2)
				conn.commit()

				stmt_1 = text(update_stmt)
				result 	= conn.execute(stmt_1)
				conn.commit()
			conn.close()
			return "success"
			
	# check singed on or not
	def checkSignedOn(self,conf_id,delegate_no,conf_schema):
		with engine.connect() as conn:
			stmt = text("select signed_on from " + conf_schema + ".delegates where delegate_no = " + str(delegate_no) + " and  conference_id = "+ str(conf_id) + " and signed_on is not null  limit 1 " )
			result  = conn.execute(stmt).first()
			conn.close()
			return dict(result._mapping) if result else None					
		
	def getCertPringLogs(self,conf_schema,conf_id):	
		with engine.connect() as conn:
			stmt = text("select d.delegate_id,d.delegate_no,d.prefix,d.full_name,d.email,d.mobile,d.membership_no, "
			+ "  group_concat(date_format(dl.created_at , '%d-%m-%Y %H:%i:%s') separator ',')   as cert_print_on , "
			+ " date_format(d.signed_on , '%d-%m-%Y %H:%i:%s') as signed_on, count(dl.delegate_no) as no_of_times,d.badge_role "
			+ " from "+ conf_schema + ".delegates d inner join  "+ conf_schema + ".cert_download_logs dl on dl.delegate_no = d.delegate_no "
			+ "where d.email not like '%numerot%' and d.conference_id = "+ str(conf_id)+" group by dl.delegate_no ;")	
			# print(stmt)
			result = conn.execute(stmt).all()
			conn.close()
			return [dict(r._mapping) for r in result] if result else None

	def usp_save_signed_data(self,conf_id,delegate_no,signed_by,comments,conf_schema,user_id):
		connection  = engine.raw_connection()
		cursor      = connection.cursor()
		cursor.callproc("usp_conf_save_signed_data",[conf_id,delegate_no,signed_by,comments,conf_schema,user_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()
			if results :
				return results
			else :
				return None 
		else :
			cursor.close()
			connection.commit()
			connection.close()
			return None		




	def get_cert_data(self,conf_id,delegate_no,cert_id,is_set_signup,is_name_update,admin_user_id,full_name):
		print("call usp_conf_get_del_certificate")
		print(conf_id,delegate_no,cert_id,is_set_signup,is_name_update,admin_user_id,full_name)
		sets = []
		try:
			connection = engine.raw_connection()
			cursor = connection.cursor()
			cursor.callproc("usp_conf_get_del_certificate",[(conf_id or None),(delegate_no or None),(cert_id or None),(is_set_signup or 0),(is_name_update or 0),(admin_user_id or None),(full_name or None)])
			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



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