Sindbad~EG File Manager

Current Path : /home/numerotech/neurologysimplified.urbanedge.co.in/neurology_video_app/core/model/
Upload File :
Current File : //home/numerotech/neurologysimplified.urbanedge.co.in/neurology_video_app/core/model/UserModel.py

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

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

class UserModel():
	def __init__(self):
		try:
			self.meta = MetaData()
			self.users    = Table("users", self.meta,  autoload_with=engine)
			self.payments = Table("payments", self.meta, autoload_with=engine)
			self.user_session_map = Table("user_session_map", self.meta, autoload_with=engine)
		except Exception as e:
			print(e)

	# insert user
	def insertUser(self,data):
		with engine.connect() as conn:
			result   = conn.execute(self.users.insert(), data)
			# get pk
			# user_id = [r for r in result.inserted_primary_key] if result.inserted_primary_key else None
			user_id  = result.lastrowid
			conn.commit()
			stmt_2   = text("select * from users where user_id = "+str(user_id)+" ;")
			result_2 = conn.execute(stmt_2).one_or_none()
			result_2 = dict(result_2._mapping) if result_2 else None
			if result_2 : 
				return result_2
			else:
				return None
			# return result.lastrowid

	def updateUser(self,user_id,data):
		with engine.connect() as conn:
			stmt = self.users.update().values(data).where(self.users.c.user_id.in_([user_id]))
			result = conn.execute(stmt)
			conn.commit()
			return result
		
	# Jan 10, 2024	
	def updatePaymentsBONew(self,p_id,payment_data):
		with engine.connect() as conn:
			stmt = self.payments.update().values(payment_data).where(self.payments.c.p_id.in_([p_id]))
			result = conn.execute(stmt)
			conn.commit()
			return result


	def getUserByUserID(self,user_id):
		with engine.connect() as conn:
			stmt = text("select * from users u   where u.user_id = "+str(user_id)+" ; ")
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None	
			
	
	def getPaymentsByUserID(self,user_id):
		with engine.connect() as conn:
			stmt = text("select * from payments p   where p.user_id = "+str(user_id)+"   order by p.p_id desc limit 1; ")
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None			
				


	def savePayRecord(self,user_id,data_for_user,data_for_payment) :
		with engine.connect() as conn:
			stmt    = self.users.update().values(data_for_user).where(self.users.c.user_id.in_([user_id]))
			result_1 = conn.execute(stmt)
			conn.commit()
			result_2 = conn.execute(self.payments.insert(), data_for_payment) 
			conn.commit()
			return "success"

	def getDataforPayments(self,user_id,unique_id):
		with engine.connect() as conn:
			stmt  = text("select u.user_id,u.prefix,u.full_name,u.email,u.mobile ,p.payment_status,p.unique_id ,p.amount,p.updated_at from users u "
			+ " inner join payments p on p.user_id = u.user_id where u.user_id = " +str(user_id)+ "  and p.unique_id = '"+unique_id+"'  ;")	
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None	

	def getPaymentByUniqueId(self,email,unique_id):
		with engine.connect() as conn:
			stmt  = text("select u.user_id,u.prefix,u.full_name,u.email,u.mobile ,p.payment_status,p.unique_id ,p.amount,p.updated_at from users u "
			+ " inner join payments p on p.user_id = u.user_id where u.email = '" +email+ "'  and p.unique_id = '"+unique_id+"'  ;")	
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None	

	def checkAlreadyPaid(self,user_id) :
		with engine.connect() as conn:
			stmt = text("select u.user_id,u.prefix,u.full_name,u.email,u.mobile ,p.payment_status,p.unique_id ,p.amount,p.updated_at from users u "
			+ " inner join payments p on p.user_id = u.user_id where u.user_id = "+str(user_id)+" and p.payment_status = 'success'  ; ")
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None

	def updatePayments(self,unique_id,data):
		with engine.connect() as conn:
			stmt = self.payments.update().values(data).where(self.payments.c.unique_id.in_([unique_id]))
			result = conn.execute(stmt)
			conn.commit()
			return result

	def updateOtpAndGetUser(self,user_id,data):
		with engine.connect() as conn:
			stmt_1 = self.users.update().values(data).where(self.users.c.user_id.in_([user_id]))
			result = conn.execute(stmt_1)
			conn.commit()
			print(" user id ----")
			print(user_id)
			stmt = text("select u.*  from users u where u.user_id = "+str(user_id)+" ; ")
			print(stmt)
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None			

	########## insert payment for backoffice #######

	def insert_payment(self,data):
		with engine.connect() as conn:
			result = conn.execute(self.payments.insert(), data)
			conn.commit()
			pk_id = [r for r in result.inserted_primary_key] if result.inserted_primary_key else None
			return pk_id

	###################

	def get_users_email_data(self,email):
		with engine.connect() as conn:
	# 		stmt = self.users.select().where(self.users.c.email.in_([email]))
			stmt  = text("select * from users where email = '"+email+"' ;")
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None
			
			
	def check_email(self,user_id,email):
		with engine.connect() as conn:
			stmt = text("select * from users where user_id <> "+str(user_id)+" and email= '"+email+"' ;")
			print(stmt)
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None 

	def update_member(self,user_id,data):
		with engine.connect() as conn:
			stmt = self.users.update().values(data).where(self.users.c.user_id.in_([user_id]))
			result = conn.execute(stmt)
			conn.commit()
			return result



	def get_otp_random(self,user_id):
		with engine.connect() as conn:
			stmt = text("select otp from users where user_id = {} ".format(user_id))
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None

	def get_member(self,user_id):
		with engine.connect() as conn:
			stmt = text("select u.*  from users u where u.user_id = "+str(user_id)+" ; ")
			result  = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None

	def get_video_link(self):
		with engine.connect() as conn:
			stmt = text("select * from video_table where is_active = 1 order by order_by asc ; ")
			result = conn.execute(stmt).one_or_none()
			return dict(result._mapping) if result else None 

	def get_video_linkByVideoId(self,video_id):
		with engine.connect() as conn:
			# stmt = text("select * from video_table where video_id = "+str(video_id)+" and is_active = 1 order by order_by asc ; ")
			stmt = text("select * ,(select ifnull(min(video_id),0) from video_table where  is_active = 1) as min_video_id,(select ifnull(max(video_id), 0) from video_table where  is_active = 1) as max_video_id,"
				+ "(select video_id from video_table where is_active = 1 and video_id > "+str(video_id)+" order by order_by asc limit 1) as next_video_id,"
				+ "(select video_id from video_table where is_active = 1 and video_id < "+str(video_id)+" order by order_by desc limit 1)  as previous_video_id "
				+ "from video_table where video_id = "+str(video_id)+" and is_active = 1 order by order_by asc ; ")
			# print(stmt)
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None 		

	# Get video index by session id Feb 03, 2023
	def get_video_linkBySessionId(self,session_id):
		with engine.connect() as conn:
			stmt = text("select v.*,s.session_name,s.feedback_url from video_table v left join sessions s on s.session_id = v.session_id where v.is_active = 1 and v.session_id = "+str(session_id)+"   order by order_by asc ; ")
			result = conn.execute(stmt).all()
			return  [dict(r._mapping) for r in result] if result else None 

	def get_video_linkByVideoIdAndSessionId(self,video_id,session_id):
		with engine.connect() as conn:
			stmt = text("select * ,(select ifnull(min(video_id),0) from video_table where  is_active = 1 and session_id = "+str(session_id)+" ) as min_video_id,(select ifnull(max(video_id), 0) from video_table where  is_active = 1 and session_id = "+str(session_id)+" ) as max_video_id,"
				+ "(select video_id from video_table where is_active = 1 and video_id > "+str(video_id)+" and session_id = "+str(session_id)+" order by order_by asc limit 1) as next_video_id,"
				+ "(select video_id from video_table where is_active = 1 and video_id < "+str(video_id)+" and session_id = "+str(session_id)+" order by order_by desc limit 1)  as previous_video_id "
				+ "from video_table where video_id = "+str(video_id)+" and session_id = "+str(session_id)+" and is_active = 1 order by order_by asc ; ")
			# print(stmt)
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None 
	# Feb 10, 2022
	def getUserWithSessions(self,user_id):
		with engine.connect() as conn:
			stmt = text("select u.*,us.session_id,s.session_name  from users u left join user_session_map us on us.user_id = u.user_id left join sessions s on s.session_id = us.session_id where u.user_id = "+str(user_id)+" ; ")
			result = conn.execute(stmt).all()
			return [dict(r._mapping) for r in result ] if result else None 


	def getSessionsData(self):
		with engine.connect() as conn:
			stmt = text("select session_id,session_name from sessions order by session_id asc ; ")
			stmt_2 = text("SET SQL_MODE = '' ;")
			conn.execute(stmt_2)
			result = conn.excute(stmt).all()
			return [dict(r._mapping) for r in result] if result else None

	def getUserSessionList(self,user_id):
		with engine.connect() as conn:
			stmt = text("select * from  sessions s left join user_session_map m on s.session_id = m.session_id  and m.user_id = "+str(user_id)+" ")							
			results = conn.execute(stmt).all()
			return [dict(r._mapping) for r in results] if results else None


	def insert_session(self,data):
		with engine.connect() as conn:
			result = conn.execute(self.user_session_map.insert(), data)
			conn.commit()
			pk_id = [r for r in result.inserted_primary_key] if result.inserted_primary_key else None
			return pk_id

	
	def delete_session(self,user_id):
		with engine.connect() as conn:
			safe_update   = text("SET SQL_SAFE_UPDATES = 0 ;")
			stmt          = text("delete from user_session_map where user_id ="+str(user_id)+";")
			print(stmt)
			result_1      = conn.execute(safe_update)
			conn.execute(stmt)
			conn.commit()
			return "success"
	
	def getUser_BO(self,user_id):
		with engine.connect() as conn:
			stmt = text("select u.*,p.p_id,p.txn_id,p.amount,date(p.paid_at) as paid_at from users u  left join user_session_map us on us.user_id=u.user_id left join payments p  on p.user_id = u.user_id and p.payment_status = 'success' where u.user_id = "+str(user_id)+"  ; ")
			results = conn.execute(stmt)
			return [dict(r._mapping) for r in results] if results else None
		
			
app.jinja_env.globals.update(UserModel=UserModel)	

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