Sindbad~EG File Manager

Current Path : /home/numerotech/conf-certificates.numerotech.com/certificates/core_old/model/
Upload File :
Current File : //home/numerotech/conf-certificates.numerotech.com/certificates/core_old/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 from "+conf_schema+".delegates where delegate_no= "+str(del_no)+";")
			result  = conn.execute(stmt).one_or_none()
			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()
			return [dict(r._mapping) for r in result] if result else None

	def getTemplate(self,conf_id,cerf_id):
		with engine.connect() as conn:
			stmt = text("select  * from certificates where conf_id = "+str(conf_id)+" and cerf_id="+ str(cerf_id) +";")
			# print(stmt)
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None

	def insert_Tempdata(self,data):
		with engine.connect() as conn:
			result = conn.execute(self.certificates.insert(), data)
			conn.commit()
			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).one_or_none()
			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()
			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()
			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:
			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)+"  and cert_id = "+str(cert_id)+"  ;")
			# print(stmt)
			result = conn.execute(stmt).all()
			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()

			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).one_or_none()
			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 "
			+ " 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()
			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()
			if results :
				return results
			else :
				return None 
		else :
			cursor.close()
			connection.commit()
			return None		




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