Sindbad~EG File Manager

Current Path : /home/numerotech/st.aios-scientificcommittee.org/aios-slots/core/model/
Upload File :
Current File : //home/numerotech/st.aios-scientificcommittee.org/aios-slots/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 .. import Cryptography,Auth
from core.library.helper import Helper
import json

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



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

        except Exception as e:
            print(e)


    def get_confe(self,conf_id,conf_key):
        with engine.connect() as conn:
            stmt = text("select c.*,sa.app_title,sa.app_url,app_host,sa.mail_setting_id,app_style,s.society_title,s.society_name,s.society_intial,sa.index_content as society_index_content,ms.* from conference c inner join societies s on s.society_id = c.society_id inner join society_applications sa on c.conf_id = sa.conf_id and sa.app_type_id = 3 inner join mail_setting ms on ms.mail_setting_id=sa.mail_setting_id  where c.conf_id = {} and c.conf_key = '{}'".format(conf_id,conf_key))
            result = conn.execute(stmt).first()
            # results = [dict(r) for r in result] if result else None
            return dict(result._mapping) if result else None

    def import_csv_users(self,workshop_id,rows,datatime,conf_id):
        with engine.connect() as conn:
            stmt = text('delete from temp_workshop_delegates where workshop_id = {} and conf_id = {}'.format(workshop_id,conf_id))
            stmt_1=('''INSERT INTO temp_workshop_delegates ( Workshop_id, delegate_no, membership_no, name, email, mobile, delegate_type, course, conf_id ) VALUES ''')
            row_val = []
            for data in rows:
                row_val.append(('(' + ''''{}',"{}",'{}','{}','{}','{}','{}','{}','{}' ''' +')').format(data['Workshop_id'],data['Reg_No'],data['membership_no'],data['name'],data['email'],data['mobile'],data['Delegate_type'],data['course'],conf_id))
            stmt_1 = stmt_1 +  ",".join(row_val)
            stmt_1 = stmt_1 + ';'	
            results = conn.execute(stmt)
            results = conn.execute(text(stmt_1))
            conn.commit()
            connection = engine.raw_connection()
            cursor = connection.cursor()
            cursor.callproc("Import_csv_data_to_db",[conf_id])
            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
            return "success"

    def get_users_email_data(self,email,conf_id):
        with engine.connect() as conn:
            stmt = text("select * from workshop_delegates where email = '{}' and conf_id = {}".format(email,conf_id))
            results = conn.execute(stmt).first()
            results = dict(results._mapping) if results else None
            return results
            
    def get_otp_random(self,user_id):
        with engine.connect() as conn:
            stmt = text("select otp from workshop_delegates where delegate_id = {} ".format(user_id))
            results = conn.execute(stmt).first()
            results = dict(results._mapping) if results else None
            return results
            
    def update_member(self,delegate_id,data):
        stmt = self.workshop_delegates.update().values(data).where(self.workshop_delegates.c.delegate_id.in_([delegate_id]))
        select_stmt = text("select *  from workshop_delegates where delegate_id = "+str(delegate_id)+" ")
        with engine.connect() as conn:
            result = conn.execute(stmt)
            select_result = conn.execute(select_stmt).first()
            conn.commit()
            result_2 = dict(select_result._mapping) if select_result else None
            return result_2
            
    def get_member(self,user_id):
        stmt = text("select *  from workshop_delegates where delegate_id = {} ".format(user_id))
        with engine.connect() as conn:
            results = conn.execute(stmt).first()
            results = dict(results._mapping) if results else None
            return results
            
    def get_user_workshop_data(self,delegate_id):
        stmt = text("select w.* from workshop_delegates d inner join delegatecourse c on d.delegate_no = c.delegate_no inner join m_workshop w on c.Workshop_id = w.workshopId where delegate_id = {} group by Workshop_id;".format(delegate_id))
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results
            
    def get_workshop_data(self,workshop_id):
        stmt = text("select * from m_workshop where workshopId = {}".format(workshop_id))
        with engine.connect() as conn:
            results = conn.execute(stmt).first()
            results = dict(results._mapping) if results else None
            return results
            
    def get_user_course_data(self,delegate_id):
        stmt = text("select w.*,ms.SlotId,ms.Date,ms.StartTime,ms.EndTime,ms.NoOfDelegates from workshop_delegates d inner join delegatecourse c on d.delegate_no collate utf8mb4_general_ci = c.delegate_no inner join workshopcourses w on c.course_id = w.WorkshopCourseId  left  join delegateslot s on s.course_id =  c.course_id  and s.DelegateNo collate utf8mb4_0900_ai_ci = c.delegate_no left  join mcourseslots ms on  ms.SlotId = s.SlotId where delegate_id = {} group by w.WorkshopCourseId;".format(delegate_id))
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results
            
    def get_slot_list(self,course_id,delegate_No):
        stmt = text("select m.*,(select count(*) from delegateslot where slotId = m.SlotId) as total_delegate_count,(select m.SlotId from delegateslot d inner join mcourseslots m on m.SlotId = d.SlotId inner join workshopcourses w on m.WorkshopCourseId = w.WorkshopCourseId where m.WorkshopCourseId = {} and DelegateNo = '{}') as is_sloted from mcourseslots m where WorkshopCourseId = {};".format(course_id,delegate_No,course_id))
        with engine.connect() as conn:
            results = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in results] if results else None
            return results
            
    def insert_delegate_Slot(self,slot_id,delegate_no,course_id):
        connection = engine.raw_connection()
        cursor = connection.cursor()
        cursor.callproc("delegate_sloting",[delegate_no,course_id,slot_id])
        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[0]
        else :
            return None

    def get_users(self,society_id):
        stmt    = text("select user_id,concat(ifnull(prefix,''),ifnull(full_name,'')) as full_name,email,mobile,membership_no,mc_number,mc.state_name as mc_state,s.state_name as state,city,pincode,address1,address2,address3,society_id from users u left join states mc on mc.state_id = u.mc_state_id left join states s on s.state_id = u.state_id where society_id = {} and full_name is not null".format(society_id))
        with engine.connect() as conn:
            result  = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            return results

    def importsqlite(self,society_id):
        stmt    = text("select user_id,concat(ifnull(prefix,''),ifnull(full_name,'')) as full_name,email,mobile,membership_no,mc_number,mc.state_name as mc_state,s.state_name as state,city,pincode,address1,address2,address3,society_id from users u left join states mc on mc.state_id = u.mc_state_id left join states s on s.state_id = u.state_id where society_id = {} and full_name is not null".format(society_id))
        with engine.connect() as conn:
            result  = conn.execute(stmt).all()
            results = [dict(r._mapping) for r in result] if result else None
            hline = list(results[0].keys())
            field_name = ','.join(str(e) for e in hline)
            connection = sqlite3.connect('numerote_primary_db.db')
            engine_sqlite = connection.cursor()
            stmt_2 = ('''CREATE TABLE IF NOT EXISTS `users`(ID INTEGER PRIMARY KEY AUTOINCREMENT,user_id int(45) ,full_name varchar(50),email varchar(255),mobile INTEGER,membership_no INTEGER,mc_number INTEGER,mc_state varchar(255),state varchar(255),city varchar(255),pincode varchar(25),address1 varchar(255),address2 varchar(255),address3 varchar(255),society_id integer)''')
            # print(stmt_2)
            result1 = engine_sqlite.execute(stmt_2)
            stmt_3 = (" insert into users ({}) values ".format(field_name))
            # print(stmt_1)
            row_val = []
            for data in results:
                row_val.append(("""('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')""".format(data['user_id'],data['full_name'],data['email'],data['mobile'],data['membership_no'],data['mc_number'],data['mc_state'],data['state'],data['city'],data['pincode'],data['address1'],data['address2'],data['address3'],data['society_id'])))
                # print(row_val)
            stmt_3 = stmt_3 +  ",".join(row_val)
            stmt_3 = stmt_3 + ';'	
            result = engine_sqlite.execute(stmt_3)
            connection.commit()
            connection.close()
            return None

    def get_sqlite_data(self,society_id):
        connection = sqlite3.connect('numerote_primary_db.db')
        connection.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r))
        engine_sqlite = connection.cursor()
        stmt_2 = ('select * from users where society_id = {}'.format(society_id))
        result = engine_sqlite.execute(stmt_2).fetchall()
        results = result
        connection.commit()
        connection.close()
        return results

app.jinja_env.globals.update(UserModel=UserModel)

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