Completed admin views
Corrected model method return values
This commit is contained in:
@@ -3,11 +3,9 @@ from ..tools.encryption import decrypt, encrypt
|
||||
from ..tools.forms import JsonEncodedDict
|
||||
from ..tools.logs import write
|
||||
|
||||
from flask import jsonify
|
||||
from flask.helpers import flash
|
||||
from flask_login import current_user
|
||||
|
||||
from datetime import datetime
|
||||
from datetime import date, datetime
|
||||
from json import dump, loads
|
||||
import os
|
||||
import secrets
|
||||
@@ -48,35 +46,45 @@ class Test(db.Model):
|
||||
self.generate_id()
|
||||
self.generate_code()
|
||||
self.creator = current_user
|
||||
errors = []
|
||||
if self.start_date.date() < date.today():
|
||||
errors.append('The start date cannot be in the past.')
|
||||
if self.end_date.date() < date.today():
|
||||
errors.append('The expiry date cannot be in the past.')
|
||||
if self.end_date < self.start_date:
|
||||
errors.append('The expiry date cannot be before the start date.')
|
||||
if errors:
|
||||
return False, errors
|
||||
db.session.add(self)
|
||||
db.session.commit()
|
||||
write('system.log', f'Test with code {self.code} created by {current_user.get_username()}.')
|
||||
write('system.log', f'Test with code {self.get_code()} created by {current_user.get_username()}.')
|
||||
return True, f'Test with code {self.get_code()} has been created.'
|
||||
|
||||
def delete(self):
|
||||
code = self.code
|
||||
if self.entries: return False, f'Cannot delete a test with submitted entries.'
|
||||
db.session.delete(self)
|
||||
db.session.commit()
|
||||
write('system.log', f'Test with code {code} deleted by {current_user.get_username()}.')
|
||||
write('system.log', f'Test with code {code} has been deleted by {current_user.get_username()}.')
|
||||
return True, f'Test with code {code} has been deleted.'
|
||||
|
||||
def start(self):
|
||||
now = datetime.now()
|
||||
if self.start_date > now:
|
||||
if self.start_date.date() > now.date():
|
||||
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.'})
|
||||
write('system.log', f'Test with code {self.code} has been started by {current_user.get_username()}.')
|
||||
return True, f'Test with code {self.code} has been started.'
|
||||
return False, f'Test with code {self.code} has already started.'
|
||||
|
||||
def end(self):
|
||||
now = datetime.now()
|
||||
if self.end_date > now:
|
||||
if self.end_date.date() > now.date():
|
||||
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.'})
|
||||
write('system.log', f'Test with code {self.code} ended by {current_user.get_username()}.')
|
||||
return True, f'Test with code {self.code} has been ended.'
|
||||
return False, f'Test with code {self.code} has already ended.'
|
||||
|
||||
def add_adjustment(self, time:int):
|
||||
adjustments = self.adjustments if self.adjustments is not None else {}
|
||||
@@ -85,23 +93,21 @@ class Test(db.Model):
|
||||
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()}.'})
|
||||
return True, 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()}.'})
|
||||
if not self.adjustments: return False, 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})
|
||||
write('system.log', f'Time adjustment for with code {code} has been removed from test {self.get_code()} by {current_user.get_username()}.')
|
||||
return True, f'Time adjustment for with code {code} has been removed from test {self.get_code()}.'
|
||||
|
||||
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 not start_date and not end_date and time_limit is None: return False, '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})
|
||||
write('system.log', f'Test with code {self.get_code()} has been updated by user {current_user.get_username()}.')
|
||||
return True, f'Test with code {self.get_code()} has been updated by.'
|
Reference in New Issue
Block a user