Sindbad~EG File Manager

Current Path : /home/numerotech/mcinfo.numerotech.com/mc_info_update/core/model/
Upload File :
Current File : //home/numerotech/mcinfo.numerotech.com/mc_info_update/core/model/DelegateModel.py

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

engine = create_engine(app.config['DATABASE_URI'])


class DelegateModel():
	def __init__(self):
		try:
			self.meta = MetaData()
			self.delegates = Table("delegates", self.meta,  autoload_with=engine)
			self.adddelegate = Table("delegateadd", self.meta,  autoload_with=engine)
			self.states = Table("states", self.meta,  autoload_with=engine)            
		except Exception as e:
			print(e)

	def insert_delegates(self,data):
		result = engine.execute(self.delegates.insert(), data)
		return result

	def insert_delegate(self,data):
		result = engine.execute(self.adddelegate.insert(), data)
		if result:
			return 'success'
		else :
		   return 'fail'

	def get_states(self):
		stmt = select([self.states])
		stmt_2 = text("select * from states;")
		conn = engine.connect()

		result = conn.execute(stmt_2)
		conn.close()
		return result

	def get_delegates(self):
		print('in Get Function')
		stmt = select([self.delegates])
		result = engine.execute(stmt)
		return result

	def get_data(self):
		print('in Get Function')
		stmt1=text("select * from delegateadd ORDER BY id DESC;")
		#stmt = select.order_by(desc[self.adddelegate])
		result = engine.execute(stmt1)
		return result

	def get_delegates_email(self,email):
		stmt = self.delegates.select().where(self.delegates.c.email.in_([email]))
		result = engine.execute(stmt)
		result = result.fetchone()
		if result :
			return result
		else:
			return "fail"   


	def get_login_email(self,email):
		stmt1=text("SELECT * from delegateadd where email like '{}%';".format(email))
		stmt = self.adddelegate.select().where(self.adddelegate.c.email.in_([email]))
		result = engine.execute(stmt1)
		result = result.fetchone()
		print(result)
		return result 		

	def get_delegates_mobile(self,mobile):
		stmt = self.delegates.select().where(self.delegates.c.mobile.in_([mobile]))
		result = engine.execute(stmt)
		result = result.fetchone()
		if result :
			return "success"
		else:
			return "fail" 


	# get_delegates_email_data 
	def get_delegates_email_data(self,email):
		# stmt_2 = text("select * from delegates where email  = "+email +";")
		stmt = self.delegates.select().where(self.delegates.c.email.in_([email]))
		results = engine.execute(stmt)
		results = [dict(r) for r in results] if results else None
		if results : 
			return results[0]
		else:
			return None

			

	def update_last_login(self,delegate_id,last_login):
		stmt = self.delegates.update().values({"last_login":last_login}).where(self.delegates.c.delegate_id.in_([delegate_id]))
		result = engine.execute(stmt)
		return result

	def delete_delegate(self,id):
		print('in Delete Function')
		print(id)
		stmt = self.delegates.delete().where(self.delegates.c.delegate_id.in_([id]))
		result = engine.execute(stmt)
		return result		

	def edit_delegate(self,id):
		print('in Edit Function')
		print(id)
		stmt = select([self.delegates]).where(self.delegates.c.delegate_id.in_([id]))
		result = engine.execute(stmt)
		output = result.fetchone()
		return output  

	def update_delegate(self,id,data):
		stmt = self.delegates.update().where(self.delegates.c.delegate_id.in_([id])).values(data)
		result = engine.execute(stmt)
		if result:
			return 'success'
		else :
		   return 'fail'


	def check_delegates_email_for_update(self,email,delegate_id):
		stmt = text("select count(*) as email_count from delegates where email = "+"'"+email+"' "+"and delegate_id != "+str(delegate_id)  +";")
		result = engine.execute(stmt)
		results = [dict(r) for r in result] if result else None
		print(results[0])
		if results :
			return results[0]
		else:
			return None 	  


			

	def check_delegates_mobile_for_update(self,mobile,delegate_id):
		stmt = text("select count(*) as mobile_count from delegates where mobile = "+"'"+mobile+"' "+"and delegate_id != "+ str(delegate_id)  +";")
		result = engine.execute(stmt)
		results = [dict(r) for r in result] if result else None
		print(results)
		if results :
			return results[0]
		else:
			return None		


	def get_username(self,delegate_id):
		stmt = text("select name  from delegates where delegate_id = "+str(delegate_id)+";")
		result = engine.execute(stmt)
		results = [dict(r) for r in result] if result else None
		print(results)
		if results :
			return results[0]
		else:
			return None		 




	def delete_select(self,ids):
		stmt = text("DELETE FROM delegates WHERE delegate_id IN (" + ",".join(ids) + ")")
		print('in Delete Function')
		print(ids)
		result = engine.execute(stmt)
		return result


	def pagination_view(self,offset,limit):
		stmt=text("SELECT * FROM delegates LIMIT "+ str(offset) +","+str(limit))
		result = engine.execute(stmt)  
		output = result.fetchall()
		return output
			
			


	def total_row(self):
		stmt=text("SELECT count( id ) as total from delegateadd;")
		result = engine.execute(stmt)
		results = result.fetchall()
		return results


	def edit_delegates(self):
		print('in View Function')
		stmt1=text("select * from delegates")
		#stmt = select([self.delegates]).where(self.delegates.c.delegate_id.in_([id]))
		result = engine.execute(stmt1)
		output = result.fetchall()
		return output  

	def ajax_all_data(self):
		print('in View Function')
		stmt1=text("select * from delegateadd")
		#stmt = select([self.delegates]).where(self.delegates.c.delegate_id.in_([id]))
		result = engine.execute(stmt1)
		output = result.fetchall()
		return output  



	def delete(self,id):
		print('in Delete Function')
		print(id)
		stmt = self.adddelegate.delete().where(self.adddelegate.c.id.in_([id]))
		result = engine.execute(stmt)
		if result:
			return 'success'
		else :
		   return 'fail'	


	def edit(self,id):
		print('in Edit Function')
		print(id)
		stmt = select([self.adddelegate]).where(self.adddelegate.c.id.in_([id]))
		result = engine.execute(stmt)
		output = result.fetchone()
		if output:
			return dict(output)
		else:
			return none  




	def update(self,id,data):
		stmt = self.adddelegate.update().where(self.adddelegate.c.id.in_([id])).values(data)
		result = engine.execute(stmt)
		if result:
			return 'success'
		else :
		   return 'fail'


	def get_email(self,email):
		#stmt = self.adddelegate.select().where(self.adddelegate.c.email.like([email]))
		stmt1=text("SELECT * from delegateadd where email like '{}%'  ORDER BY id ASC;".format(email))
		result = engine.execute(stmt1)
		result = result.fetchall()
		print(result)
		if result:
			return 'success'
		else :
		   return 'fail'		

	def get_filter_data(self,name,email,mobile):
		#stmt = self.adddelegate.select().where(self.adddelegate.c.name.like([name]))
		#stmt1=text("SELECT * from delegateadd where name like '{}%'  ORDER BY id ASC;".format(name))
		stmt = text("SELECT id,name,email,mobile,password FROM delegateadd where name like "+"'%"+name+"%' "+" and email like "+"'%"+email+"%' "+" and mobile like "+"'%"+mobile+"%' "+" ; ")
		print("STATEMENT----")
		print(stmt)
		result = engine.execute(stmt)
		result = result.fetchall()
		return result   
			

	def get_mobile(self,mobile):
		stmt = self.adddelegate.select().where(self.adddelegate.c.mobile.like([mobile]))
		stmt1=text("SELECT * from delegateadd where mobile like '{}%'  ORDER BY id ASC;".format(mobile))
		result = engine.execute(stmt1)
		result = result.fetchall()
		if result:
			return 'success'
		else :
		   return 'fail'



	def get(self):
		print('in Get Function')
		stmt1=text("SELECT id,name,email,mobile from delegateadd ;")
		#stmt = select.order_by(desc[self.adddelegate])
		result = engine.execute(stmt1)
		result = result.fetchall()
		return result

	def total_filter_data(self,name,email,mobile):
		print('total name')
		#stmt1=text("SELECT count(*) from delegateadd where name like '{}%'  ORDER BY id ASC;".format(name))
		stmt = text("SELECT count(id)as total  FROM delegateadd where name like "+"'%"+name+"%' "+" and email like "+"'%"+email+"%' "+" and mobile like "+"'%"+mobile+"%' "+" ; ")
		#stmt = self.delegates.select([func.count(id)]).where(self.delegateadd.c.name.like(['(name)%']))
		print("STATEMENT----")
		print(stmt)
		result = engine.execute(stmt)
		results = result.fetchall()
		return results

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