Matrices-Practice-Tool / src / report.py
report.py
Raw
'''
This program represents the report page
'''
import os
import csv
from QuizClasses.log import Log
from UserClasses.mail import Mail


class Report:
    ''' Report class '''

    def __init__(self, username: str, user_email: str):
        ''' Initialization method for the report generator. '''
        self.filename = 'report.csv'
        self.username = username
        self.user_email = user_email
        self.log = Log(self.username)

    def get_all(self):
        '''Gets all quiz attempts from the database through the log class.'''
        self.log.past_quizes = self.log.get_details(self.username)
        return self.log.past_quizes
        
    def get_last_5_shortened(self):
        short_past_quizes = []
        self.log.past_quizes = self.log.get_log(self.username)
        for quiz in self.log.past_quizes:
            quiz = list(quiz)
            quiz.pop(2)
            quiz.pop(0)
            short_past_quizes.append(quiz)
        return short_past_quizes

    def email_everything(self):
        ''' Emails the user about all quiz results. '''
        email = Mail()
        email.set_credentials(self.user_email, 'All Past Results')
        self.export_to_csv(self.get_all())
        email.set_body_and_file('''

As requested, here are the results of all of your completed attempts.

''', self.filename)
        email.email_person(self.user_email)

    def email_last_7(self):
        ''' Emails the user about which questions were correctly
        answered and which ones weren't out of the last 4 quizzes. '''
        email = Mail()
        email.set_credentials(self.user_email, 'Past 4 Results')
        self.log.past_quizes = self.log.get_log(self.username)
        self.export_to_csv(self.log.past_quizes)
        email.set_body_and_file('''

As requested, here are the results of your most recently completed attempts.

''', self.filename)
        email.email_person(self.user_email)

    def export_to_csv(self, questions_list: list):
        ''' Exports to a .csv file. '''
        with open(f'{os.getcwd()}/{self.filename}', 'w') as csv_file:
            file_writer = csv.writer(csv_file)
            titles = ["Date", "Score", "Time taken"]
            for count in range(1, 11):
                titles.insert(count, f'''Question {count}''')
            file_writer.writerow(titles)
            for questions in questions_list:
                questions = list(questions[1:])
                q_list = self.parse(questions[1])
                count = 1
                for question in q_list:
                    questions.insert(count, question)
                    count += 1
                questions.pop(count)
                file_writer.writerow(questions)

    @staticmethod
    def parse(question: str) -> list:
        '''Parses the question string into an appropriate list.'''
        q_list = question.replace('O', '-correct ').replace(
            'X', '-incorrect ')
        q_list = q_list.split(' ')
        q_list.pop()
        return q_list