Sindbad~EG File Manager
from sqlalchemy import create_engine, MetaData, Table, insert, select,update,delete,text
from sqlalchemy.sql import and_, or_
from core import app
from .. import Cryptography
from core.library.helper import Helper
import json
import datetime
from datetime import timedelta,date,datetime
from .. import engine,user_engine
import sqlite3
import pandas as pd
# engine = create_engine(app.config['DATABASE_URI'],pool_size=5000,pool_pre_ping=True,pool_recycle=3600)
# engine = create_engine(app.config['DATABASE_URI'],pool_size=20, max_overflow=0)
class UserModel():
def __init__(self):
try:
self.meta = MetaData()
self.users = Table("users", self.meta, autoload_with=user_engine)
self.q_options = Table("q_options", self.meta, autoload_with=engine)
self.questions = Table("questions", self.meta, autoload_with=engine)
self.user_answers = Table("user_answers", self.meta, autoload_with=engine)
self.mcq_category = Table("mcq_category", self.meta, autoload_with=engine)
except Exception as e:
print(e)
def get_society(self,society_id):
with engine.connect() as conn:
stmt = text("select * from societies where society_id = {}".format(society_id))
result = conn.execute(stmt).one_or_none()
conn.close()
return dict(result._mapping) if result else None
def insert_new_user(self,email,society_id):
with engine.connect() as conn:
stmt = text("insert into users (email,society_id,created_at) values('{}',{},'{}')".format(email,society_id,datetime.now()))
result = conn.execute(stmt)
conn.commit()
stmt = text("select * from users where email = '{}' and society_id = {} limit 1".format(email,society_id))
result = conn.execute(stmt).one_or_none()
conn.close()
return dict(result._mapping) if result else None
def get_mcq_category(self):
with engine.connect() as conn:
stmt = text(f'select * from mcq_category')
result = conn.execute(stmt).all()
conn.close()
return [dict(r._mapping) for r in result] if result else []
def get_incomplete_user_questions(self,user_id):
with engine.connect() as conn:
stmt = text(f'select * from questions where user_id = {user_id} and status_id = 0')
result = conn.execute(stmt).all()
conn.close()
return [dict(r._mapping) for r in result] if result else []
def get_complete_user_questions(self,user_id):
with engine.connect() as conn:
stmt = text(f'select * from questions where user_id = {user_id} and status_id = 1')
result = conn.execute(stmt).all()
conn.close()
return [dict(r._mapping) for r in result] if result else []
def get_selected_user_questions(self,user_id):
with engine.connect() as conn:
stmt = text(f'select * from questions where user_id = {user_id} and status_id = 2')
result = conn.execute(stmt).all()
conn.close()
return [dict(r._mapping) for r in result] if result else []
def insert_new_question(self,user_id):
with engine.connect() as conn:
stmt = text(f"insert into questions (user_id) values({user_id})")
result = conn.execute(stmt)
conn.commit()
stmt = text(f"select * from questions where user_id = {user_id} order by q_id desc limit 1")
result = conn.execute(stmt).one_or_none()
q_data = dict(result._mapping) if result else None
q_id = q_data["q_id"]
conn.close()
return q_id
def get_question(self,q_id):
with engine.connect() as conn:
stmt = text(f'select *,group_concat(display_name) as category_name FROM questions q left join mcq_category mc on find_in_set(mc.mcq_category_id,q.category_ids) where q_id = {q_id}')
result = conn.execute(stmt).one_or_none()
conn.close()
return dict(result._mapping) if result else None
def get_question_option(self,q_id):
with engine.connect() as conn:
stmt = text(f'select * from q_options where q_id = {q_id} order by opt_id')
result = conn.execute(stmt).all()
conn.close()
return [dict(i._mapping) for i in result] if result else []
def get_user_by_password(self,email,password):
with user_engine.connect() as conn:
stmt = text(f'select * from users where email = "{email}" and password = "{password}" and society_id = 18')
result = conn.execute(stmt).one_or_none()
conn.close()
return dict(result._mapping) if result else None
def get_user_by_email(self,email):
with user_engine.connect() as conn:
stmt = text(f'select * from users where email = "{email}" and society_id = 18')
result = conn.execute(stmt).one_or_none()
conn.close()
return dict(result._mapping) if result else None
def update_member(self,user_id,data):
stmt = self.users.update().values(data).where(self.users.c.user_id.in_([user_id]))
select_stmt = text("select * from users where user_id = "+str(user_id)+" ")
with user_engine.connect() as conn:
result = conn.execute(stmt)
conn.commit()
select_result = conn.execute(select_stmt).first()
result_2 = dict(select_result._mapping) if select_result else None
conn.close()
return result_2
def get_member(self,user_id):
stmt = text(f'select * from users where user_id = {user_id} and society_id = 18')
with user_engine.connect() as conn:
result = conn.execute(stmt).one_or_none()
conn.close()
return dict(result._mapping) if result else None
def get_all_questions(self,user_id,status_id):
with engine.connect() as conn:
stmt = text(f"select *,group_concat( distinct display_name) as category_name from questions q left join mcq_category mc on find_in_set(mc.mcq_category_id,q.category_ids) where user_id = {user_id} and status_id = {status_id} group by q.q_id")
result = conn.execute(stmt).all()
conn.close()
return [dict(row._mapping) for row in result] if result else None
def insert_update_opt(self,opt_data):
with engine.connect() as conn:
for i in opt_data:
if not i["opt_id"]:
result = conn.execute(self.q_options.insert(), i)
else:
stmt = self.q_options.update().where(self.q_options.c.opt_id.in_([i["opt_id"]])).values(i)
result = conn.execute(stmt)
conn.commit()
conn.close()
return "success"
def update_question(self,q_data,q_id):
with engine.connect() as conn:
print(q_data)
stmt = self.questions.update().where(self.questions.c.q_id.in_([q_id])).values(q_data)
result = conn.execute(stmt)
conn.commit()
conn.close()
return "success"
def get_img_upload_by_q_id(self,q_id):
with engine.connect() as conn:
stmt = text(f'select * from questions where q_id = {q_id}')
result = conn.execute(stmt).one_or_none()
conn.close()
return dict(result._mapping) if result else None
def get_completed_sessions(self,user_id):
with engine.connect() as conn:
stmt = text(f'select count(distinct session_id) as session_count from user_answers ua where ua.user_id = {user_id} ;')
result = conn.execute(stmt).fetchone()
conn.close()
return dict(result._mapping) if result else None
def get_upcoming_sessions(self,now):
with engine.connect() as conn:
stmt = text(f"select * from m_session where startdate > STR_TO_DATE('{now}', '%Y-%m-%d %T') ;")
result = conn.execute(stmt).all()
conn.close()
return [dict(row._mapping) for row in result] if result else None
def get_active_session(self,now):
with engine.connect() as conn:
stmt = text(f"select * from m_session where STR_TO_DATE('{now}', '%Y-%m-%d %T') between startdate and enddate;")
result = conn.execute(stmt).all()
conn.close()
return [dict(row._mapping) for row in result] if result else None
def get_question_data(self,session_id,q_id,user_id):
sets = []
try:
connection = engine.raw_connection()
cursor = connection.cursor()
cursor.callproc("usp_get_question_options",[session_id,q_id,user_id])
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
if cursor.description is None:
break
finally:
connection.close()
return sets
def insert_user_option(self,data):
with engine.connect() as conn:
result = conn.execute(self.user_answers.insert(), data)
conn.commit()
conn.close()
return result.inserted_primary_key
app.jinja_env.globals.update(UserModel=UserModel)
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists