From 22878b53988a888f48f0c44b82d2ba0597ec0c99 Mon Sep 17 00:00:00 2001 From: viveksantayana Date: Sun, 12 Jun 2022 21:20:09 +0100 Subject: [PATCH] Added relationships between database models --- ref-test/app/models/entry.py | 6 ++---- ref-test/app/models/test.py | 3 ++- ref-test/app/models/user.py | 1 + 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ref-test/app/models/entry.py b/ref-test/app/models/entry.py index 28b5e70..0be0046 100644 --- a/ref-test/app/models/entry.py +++ b/ref-test/app/models/entry.py @@ -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: diff --git a/ref-test/app/models/test.py b/ref-test/app/models/test.py index cd28e79..9138a94 100644 --- a/ref-test/app/models/test.py +++ b/ref-test/app/models/test.py @@ -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'' diff --git a/ref-test/app/models/user.py b/ref-test/app/models/user.py index 4c217d9..9d45103 100644 --- a/ref-test/app/models/user.py +++ b/ref-test/app/models/user.py @@ -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' was added with .'