2022-06-12 21:03:51 +01:00
from . . modules import db
from . . tools . encryption import decrypt , encrypt
from . . tools . forms import JsonEncodedDict
from . . tools . logs import write
2022-06-11 18:26:39 +01:00
2022-06-12 21:03:51 +01:00
from flask import jsonify
from flask . helpers import flash
from flask_login import current_user
2022-06-11 18:26:39 +01:00
2022-06-12 21:03:51 +01:00
from datetime import datetime
from json import dump , loads
import os
import secrets
2022-06-11 18:26:39 +01:00
2022-06-12 21:03:51 +01:00
class Test ( db . Model ) :
2022-06-11 18:26:39 +01:00
2022-06-12 21:03:51 +01:00
id = db . Column ( db . String ( 36 ) , primary_key = True )
code = db . Column ( db . String ( 36 ) , nullable = False )
start_date = db . Column ( db . DateTime , nullable = True )
end_date = db . Column ( db . DateTime , nullable = True )
time_limit = db . Column ( db . Integer , nullable = True )
2022-06-12 21:20:09 +01:00
creator_id = db . Column ( db . String ( 36 ) , db . ForeignKey ( ' user.id ' ) )
2022-06-12 21:03:51 +01:00
data = db . Column ( db . String ( 36 ) , nullable = False )
adjustments = db . Column ( JsonEncodedDict , nullable = True )
2022-06-12 21:20:09 +01:00
entries = db . relationship ( ' Entry ' , backref = ' test ' )
2022-06-12 21:03:51 +01:00
def __repr__ ( self ) :
return f ' <test with code { self . code } was created by { current_user . get_username ( ) } .> '
def get_code ( self ) :
code = self . code . upper ( )
return ' — ' . join ( [ code [ : 4 ] , code [ 4 : 8 ] , code [ 8 : ] ] )
def create ( self ) :
db . session . add ( self )
db . session . commit ( )
write ( ' system.log ' , f ' Test with code { self . code } created by { current_user . get_username ( ) } . ' )
2022-06-11 18:26:39 +01:00
def delete ( self ) :
2022-06-12 21:03:51 +01:00
code = self . code
db . session . delete ( self )
db . session . commit ( )
write ( ' system.log ' , f ' Test with code { code } deleted by { current_user . get_username ( ) } . ' )
def start ( self ) :
now = datetime . now ( )
if self . start_date > now :
self . start_date = now
db . session . commit ( )
message = f ' Test with code { self . code } started by { current_user . get_username ( ) } . '
write ( ' system.log ' , message )
return True , jsonify ( { ' success ' : message } )
return False , jsonify ( { ' error ' : f ' Test with code { self . code } has already started. ' } )
def end ( self ) :
now = datetime . now ( )
if self . end_date > now :
self . end_date = now
db . session . commit ( )
message = f ' Test with code { self . code } ended by { current_user . get_username ( ) } . '
write ( ' system.log ' , message )
return True , jsonify ( { ' success ' : message } )
return False , jsonify ( { ' error ' : f ' Test with code { self . code } has already started. ' } )
def add_adjustment ( self , time : int ) :
adjustments = self . adjustments if self . adjustments is not None else { }
code = secrets . token_hex ( 3 ) . lower ( )
adjustments [ code ] = time
self . adjustments = adjustments
db . session . commit ( )
write ( ' system.log ' , f ' Time adjustment for { time } minutes with code { code } added to test { self . get_code ( ) } by { current_user . get_username ( ) } . ' )
return True , jsonify ( { ' success ' : f ' Time adjustment for { time } minutes added to test { self . get_code ( ) } . This can be accessed using the user code { code . upper ( ) } . ' } )
def remove_adjustment ( self , code : str ) :
if not self . adjustments : return False , jsonify ( { ' error ' : f ' There are no adjustments configured for test { self . get_code ( ) } . ' } )
self . adjustments . pop ( code )
if not self . adjustments : self . adjustments = None
db . session . commit ( )
message = f ' Time adjustment for with code { code } removed from test { self . get_code ( ) } by { current_user . get_username ( ) } . '
write ( ' system.log ' , message )
return True , jsonify ( { ' success ' : message } )
2022-06-11 18:26:39 +01:00
2022-06-12 21:03:51 +01:00
def update ( self , start_date : datetime = None , end_date : datetime = None , time_limit : int = None ) :
if not start_date and not end_date and time_limit is None : return False , jsonify ( { ' error ' : ' There were no changes requested. ' } )
if start_date : self . start_date = start_date
if end_date : self . end_date = end_date
if time_limit is not None : self . time_limit = time_limit
db . session . commit ( )
message = f ' Test with code { self . get_code ( ) } has been updated by user { current_user . get_username ( ) } '
write ( ' system.log ' , message )
return True , jsonify ( { ' success ' : message } )