Matrices-Practice-Tool / src / QuizClasses / log.py
log.py
Raw
'''Quiz objects'''
import sqlite3


class Log:
    '''Class to extract and hold info about the past 7 quizes completed'''
    past_quizes = []
    log_check = '''SELECT *
                FROM QuizDetails
                WHERE Username = (?)
                ORDER BY Date DESC'''

    def __init__(self, username: str):
        '''Initialises'''
        self.past_quizes = self.get_log(username)

    def get_log(self, username: str) -> list:
        '''Get past 5 quizes from database'''
        connection = sqlite3.connect("QuizDatabase.db")
        cursor = connection.cursor()
        log_check = self.log_check + ''' LIMIT 4;'''
        cursor.execute(log_check, (username,))
        data = list(cursor.fetchall())
        connection.commit()
        connection.close()
        return data

    def get_details(self, username: str) -> list:
        '''Get all quizes from the database.'''
        connection = sqlite3.connect('''QuizDatabase.db''')
        cursor = connection.cursor()
        log_check = self.log_check + ''';'''
        cursor.execute(log_check, (username,))
        data = list(cursor.fetchall())
        connection.commit()
        connection.close()
        return data