ska-referee-test/ref-test/app/models/entry.py

88 lines
3.2 KiB
Python

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 import jsonify
from flask_login import current_user
from datetime import datetime, timedelta
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'))
test_code = db.Column(db.String(36), db.ForeignKey('test.code'))
user_code = db.Column(db.String(6), nullable=True)
start_time = db.Column(db.DateTime, nullable=False)
end_time = db.Column(db.DateTime, nullable=True)
status = db.Column(db.String(16), nullable=True)
valid = db.Column(db.Boolean, default=True, nullable=True)
answers = db.Column(JsonEncodedDict, nullable=True)
result = db.Column(JsonEncodedDict, nullable=True)
@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.')
set_email.setter
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.')
set_club.setter
def set_club(self, club:str): self.club = encrypt(club)
def get_club(self): return decrypt(self.club)
def start(self):
self.start_time = datetime.now()
self.status = 'started'
write('tests.log', f'New test started by {self.get_first_name()} {self.get_surname()}.')
db.session.commit()
def complete(self):
self.end_time = datetime.now()
write('tests.log', f'Test completed by {self.get_first_name()} {self.get_surname()}.')
test = Test.query.filter_by(code=self.test_code).first()
delta = timedelta(minutes=test.time_limit)
if not test.time_limit or self.end_time <= self.start_time + delta:
self.status = 'finished'
self.valid = True
else:
self.status = 'late'
self.valid = False
db.session.commit()
def validate(self):
if self.valid: return False, jsonify({'error':f'The entry is already valid.'})
if self.status == 'started': return False, jsonify({'error':f'The entry is still pending.'})
self.valid = True
self.status = 'completed'
db.session.commit()
message = f'The entry {self.id} has been validated by {current_user.get_username()}.'
return True, jsonify({'success': message})