Added relationships between database models

This commit is contained in:
Vivek Santayana 2022-06-12 21:20:09 +01:00
parent 52b44128fa
commit 22878b5398
3 changed files with 5 additions and 5 deletions

View File

@ -17,7 +17,6 @@ class Entry(db.Model):
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)
@ -67,9 +66,8 @@ class Entry(db.Model):
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:
delta = timedelta(minutes=self.test.time_limit)
if not self.test.time_limit or self.end_time <= self.start_time + delta:
self.status = 'finished'
self.valid = True
else:

View File

@ -20,9 +20,10 @@ class Test(db.Model):
start_date = db.Column(db.DateTime, nullable=True)
end_date = db.Column(db.DateTime, nullable=True)
time_limit = db.Column(db.Integer, nullable=True)
creator = db.Column(db.String(36), db.ForeignKey('user.id'))
creator_id = db.Column(db.String(36), db.ForeignKey('user.id'))
data = db.Column(db.String(36), nullable=False)
adjustments = db.Column(JsonEncodedDict, nullable=True)
entries = db.relationship('Entry', backref='test')
def __repr__(self):
return f'<test with code {self.code} was created by {current_user.get_username()}.>'

View File

@ -16,6 +16,7 @@ class User(UserMixin, db.Model):
email = db.Column(db.String(128), nullable=False)
reset_token = db.Column(db.String(20), nullable=True)
verification_token = db.Column(db.String(20), nullable=True)
tests = db.relationship('Test', backref='creator')
def __repr__(self):
return f'<user {self.username}> was added with <id {self.id}>.'