2022-06-12 21:03:51 +01:00
|
|
|
from ..modules import db
|
|
|
|
from ..tools.forms import JsonEncodedDict
|
|
|
|
from ..tools.encryption import decrypt, encrypt
|
|
|
|
from ..tools.logs import write
|
|
|
|
from .test import Test
|
|
|
|
|
|
|
|
from flask_login import current_user
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
2022-06-14 22:55:11 +01:00
|
|
|
from uuid import uuid4
|
2022-06-11 18:26:39 +01:00
|
|
|
|
|
|
|
class Entry(db.Model):
|
|
|
|
|
|
|
|
id = db.Column(db.String(36), primary_key=True)
|
|
|
|
first_name = db.Column(db.String(128), nullable=False)
|
|
|
|
surname = db.Column(db.String(128), nullable=False)
|
|
|
|
email = db.Column(db.String(128), nullable=False)
|
|
|
|
club = db.Column(db.String(128), nullable=True)
|
|
|
|
test_id = db.Column(db.String(36), db.ForeignKey('test.id'))
|
|
|
|
user_code = db.Column(db.String(6), nullable=True)
|
2022-06-16 10:44:48 +01:00
|
|
|
start_time = db.Column(db.DateTime, nullable=True)
|
2022-06-11 18:26:39 +01:00
|
|
|
end_time = db.Column(db.DateTime, nullable=True)
|
|
|
|
status = db.Column(db.String(16), nullable=True)
|
2022-06-12 21:03:51 +01:00
|
|
|
valid = db.Column(db.Boolean, default=True, nullable=True)
|
2022-06-11 18:26:39 +01:00
|
|
|
answers = db.Column(JsonEncodedDict, nullable=True)
|
2022-06-12 21:03:51 +01:00
|
|
|
result = db.Column(JsonEncodedDict, nullable=True)
|
2022-06-11 18:26:39 +01:00
|
|
|
|
2022-06-14 22:55:11 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return f'<New entry by {self.first_name} {self.surname}> was added with <id {self.id}>.'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def generate_id(self): raise AttributeError('generate_id is not a readable attribute.')
|
|
|
|
|
|
|
|
generate_id.setter
|
2022-06-15 23:54:44 +01:00
|
|
|
def generate_id(self): self.id = uuid4().hex
|
2022-06-14 22:55:11 +01:00
|
|
|
|
2022-06-11 18:26:39 +01:00
|
|
|
@property
|
|
|
|
def set_first_name(self): raise AttributeError('set_first_name is not a readable attribute.')
|
|
|
|
|
|
|
|
set_first_name.setter
|
|
|
|
def set_first_name(self, name:str): self.first_name = encrypt(name)
|
|
|
|
|
|
|
|
def get_first_name(self): return decrypt(self.first_name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def set_surname(self): raise AttributeError('set_first_name is not a readable attribute.')
|
|
|
|
|
|
|
|
set_surname.setter
|
|
|
|
def set_surname(self, name:str): self.surname = encrypt(name)
|
|
|
|
|
|
|
|
def get_surname(self): return decrypt(self.surname)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def set_email(self): raise AttributeError('set_email is not a readable attribute.')
|
|
|
|
|
2022-06-12 21:03:51 +01:00
|
|
|
set_email.setter
|
2022-06-11 18:26:39 +01:00
|
|
|
def set_email(self, email:str): self.email = encrypt(email)
|
|
|
|
|
|
|
|
def get_email(self): return decrypt(self.email)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def set_club(self): raise AttributeError('set_club is not a readable attribute.')
|
|
|
|
|
2022-06-12 21:03:51 +01:00
|
|
|
set_club.setter
|
2022-06-11 18:26:39 +01:00
|
|
|
def set_club(self, club:str): self.club = encrypt(club)
|
|
|
|
|
2022-06-12 21:03:51 +01:00
|
|
|
def get_club(self): return decrypt(self.club)
|
|
|
|
|
2022-06-16 10:44:48 +01:00
|
|
|
def ready(self):
|
2022-06-16 01:03:06 +01:00
|
|
|
self.generate_id()
|
2022-06-16 10:44:48 +01:00
|
|
|
db.session.add(self)
|
|
|
|
db.session.commit()
|
|
|
|
write('tests.log', f'New test ready for {self.get_first_name()} {self.get_surname()}.')
|
|
|
|
return True, f'Test ready.'
|
|
|
|
|
|
|
|
def start(self):
|
2022-06-12 21:03:51 +01:00
|
|
|
self.start_time = datetime.now()
|
|
|
|
self.status = 'started'
|
2022-06-16 10:44:48 +01:00
|
|
|
write('tests.log', f'Test started by {self.get_first_name()} {self.get_surname()}.')
|
2022-06-12 21:03:51 +01:00
|
|
|
db.session.commit()
|
2022-06-16 01:03:06 +01:00
|
|
|
return True, f'New test started with id {self.id}.'
|
2022-06-12 21:03:51 +01:00
|
|
|
|
2022-06-14 22:55:11 +01:00
|
|
|
def complete(self, answers:dict=None, result:dict=None):
|
2022-06-12 21:03:51 +01:00
|
|
|
self.end_time = datetime.now()
|
2022-06-14 22:55:11 +01:00
|
|
|
self.answers = answers
|
2022-06-16 10:44:48 +01:00
|
|
|
self.result = result
|
2022-06-12 21:03:51 +01:00
|
|
|
write('tests.log', f'Test completed by {self.get_first_name()} {self.get_surname()}.')
|
2022-06-14 22:55:11 +01:00
|
|
|
delta = timedelta(minutes=self.test.time_limit+1)
|
2022-06-12 21:20:09 +01:00
|
|
|
if not self.test.time_limit or self.end_time <= self.start_time + delta:
|
2022-06-15 11:23:38 +01:00
|
|
|
self.status = 'completed'
|
2022-06-12 21:03:51 +01:00
|
|
|
self.valid = True
|
|
|
|
else:
|
|
|
|
self.status = 'late'
|
|
|
|
self.valid = False
|
|
|
|
db.session.commit()
|
2022-06-16 01:03:06 +01:00
|
|
|
return True, f'Test entry completed for id {self.id}.'
|
2022-06-12 21:03:51 +01:00
|
|
|
|
|
|
|
def validate(self):
|
2022-06-15 11:23:38 +01:00
|
|
|
if self.valid: return False, f'The entry is already valid.'
|
|
|
|
if self.status == 'started': return False, 'The entry is still pending.'
|
2022-06-12 21:03:51 +01:00
|
|
|
self.valid = True
|
|
|
|
self.status = 'completed'
|
|
|
|
db.session.commit()
|
2022-06-15 11:23:38 +01:00
|
|
|
write('system.log', f'The entry {self.id} has been validated by {current_user.get_username()}.')
|
|
|
|
return True, f'The entry {self.id} has been validated.'
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
id = self.id
|
|
|
|
name = f'{self.get_first_name()} {self.get_surname()}'
|
|
|
|
db.session.delete(self)
|
|
|
|
db.session.commit()
|
|
|
|
write('system.log', f'The entry {id} by {name} has been deleted by {current_user.get_username()}.')
|
|
|
|
return True, 'Entry deleted.'
|