Commit 5c8b68ea1f60f0507fafff696b94ffe084757f57

Authored by Miguel Barão
1 parent a702aecd
Exists in dev

remove "future" flag from engine and session creation

Showing 2 changed files with 22 additions and 19 deletions   Show diff stats
perguntations/app.py
@@ -94,9 +94,9 @@ class App: @@ -94,9 +94,9 @@ class App:
94 raise AppException('No database, use "initdb" to create') 94 raise AppException('No database, use "initdb" to create')
95 95
96 # connect to database and check for admin & registered students 96 # connect to database and check for admin & registered students
97 - self._engine = create_engine(f"sqlite:///{dbfile}", future=True) 97 + self._engine = create_engine(f"sqlite:///{dbfile}")
98 try: 98 try:
99 - with Session(self._engine, future=True) as session: 99 + with Session(self._engine) as session:
100 query = select(Student.id, Student.name).where(Student.id != "0") 100 query = select(Student.id, Student.name).where(Student.id != "0")
101 dbstudents = session.execute(query).all() 101 dbstudents = session.execute(query).all()
102 session.execute(select(Student).where(Student.id == "0")).one() 102 session.execute(select(Student).where(Student.id == "0")).one()
@@ -135,7 +135,7 @@ class App: @@ -135,7 +135,7 @@ class App:
135 If successful returns None, else returns an error message 135 If successful returns None, else returns an error message
136 """ 136 """
137 try: 137 try:
138 - with Session(self._engine, future=True) as session: 138 + with Session(self._engine) as session:
139 query = select(Student.password).where(Student.id == uid) 139 query = select(Student.password).where(Student.id == uid)
140 hashed = session.execute(query).scalar_one() 140 hashed = session.execute(query).scalar_one()
141 except NoResultFound: 141 except NoResultFound:
@@ -168,7 +168,7 @@ class App: @@ -168,7 +168,7 @@ class App:
168 # ------------------------------------------------------------------------ 168 # ------------------------------------------------------------------------
169 async def set_password(self, uid: str, password: str) -> None: 169 async def set_password(self, uid: str, password: str) -> None:
170 """change password in the database""" 170 """change password in the database"""
171 - with Session(self._engine, future=True) as session: 171 + with Session(self._engine) as session:
172 query = select(Student).where(Student.id == uid) 172 query = select(Student).where(Student.id == uid)
173 student = session.execute(query).scalar_one() 173 student = session.execute(query).scalar_one()
174 student.password = await hash_password(password) if password else "" 174 student.password = await hash_password(password) if password else ""
@@ -265,14 +265,14 @@ class App: @@ -265,14 +265,14 @@ class App:
265 for n, q in enumerate(test["questions"]) 265 for n, q in enumerate(test["questions"])
266 ] 266 ]
267 267
268 - with Session(self._engine, future=True) as session: 268 + with Session(self._engine) as session:
269 session.add(test_row) 269 session.add(test_row)
270 session.commit() 270 session.commit()
271 logger.info('"%s" database updated', uid) 271 logger.info('"%s" database updated', uid)
272 272
273 # ------------------------------------------------------------------------ 273 # ------------------------------------------------------------------------
274 def _correct_tests(self) -> None: 274 def _correct_tests(self) -> None:
275 - with Session(self._engine, future=True) as session: 275 + with Session(self._engine) as session:
276 # Find which tests have to be corrected 276 # Find which tests have to be corrected
277 query = ( 277 query = (
278 select(Test) 278 select(Test)
@@ -395,7 +395,7 @@ class App: @@ -395,7 +395,7 @@ class App:
395 def get_grades_csv(self): 395 def get_grades_csv(self):
396 """generates a CSV with the grades of the test currently running""" 396 """generates a CSV with the grades of the test currently running"""
397 test_ref = self._testfactory["ref"] 397 test_ref = self._testfactory["ref"]
398 - with Session(self._engine, future=True) as session: 398 + with Session(self._engine) as session:
399 query = ( 399 query = (
400 select(Test.student_id, Test.grade, Test.starttime, Test.finishtime) 400 select(Test.student_id, Test.grade, Test.starttime, Test.finishtime)
401 .where(Test.ref == test_ref) 401 .where(Test.ref == test_ref)
@@ -416,7 +416,7 @@ class App: @@ -416,7 +416,7 @@ class App:
416 def get_detailed_grades_csv(self): 416 def get_detailed_grades_csv(self):
417 """generates a CSV with the grades of the test""" 417 """generates a CSV with the grades of the test"""
418 test_ref = self._testfactory["ref"] 418 test_ref = self._testfactory["ref"]
419 - with Session(self._engine, future=True) as session: 419 + with Session(self._engine) as session:
420 query = ( 420 query = (
421 select( 421 select(
422 Test.id, 422 Test.id,
@@ -453,14 +453,14 @@ class App: @@ -453,14 +453,14 @@ class App:
453 # ------------------------------------------------------------------------ 453 # ------------------------------------------------------------------------
454 def get_json_filename_of_test(self, test_id): 454 def get_json_filename_of_test(self, test_id):
455 """get JSON filename from database given the test_id""" 455 """get JSON filename from database given the test_id"""
456 - with Session(self._engine, future=True) as session: 456 + with Session(self._engine) as session:
457 query = select(Test.filename).where(Test.id == test_id) 457 query = select(Test.filename).where(Test.id == test_id)
458 return session.execute(query).scalar() 458 return session.execute(query).scalar()
459 459
460 # ------------------------------------------------------------------------ 460 # ------------------------------------------------------------------------
461 def get_grades(self, uid, ref): 461 def get_grades(self, uid, ref):
462 """get grades of student for a given testid""" 462 """get grades of student for a given testid"""
463 - with Session(self._engine, future=True) as session: 463 + with Session(self._engine) as session:
464 query = ( 464 query = (
465 select(Test.grade, Test.finishtime, Test.id) 465 select(Test.grade, Test.finishtime, Test.id)
466 .where(Test.student_id == uid) 466 .where(Test.student_id == uid)
@@ -520,7 +520,7 @@ class App: @@ -520,7 +520,7 @@ class App:
520 # ------------------------------------------------------------------------ 520 # ------------------------------------------------------------------------
521 async def insert_new_student(self, uid: str, name: str) -> None: 521 async def insert_new_student(self, uid: str, name: str) -> None:
522 """insert new student into the database""" 522 """insert new student into the database"""
523 - with Session(self._engine, future=True) as session: 523 + with Session(self._engine) as session:
524 try: 524 try:
525 session.add(Student(id=uid, name=name, password="")) 525 session.add(Student(id=uid, name=name, password=""))
526 session.commit() 526 session.commit()
perguntations/initdb.py
@@ -155,31 +155,31 @@ def main(): @@ -155,31 +155,31 @@ def main():
155 args = parse_commandline_arguments() 155 args = parse_commandline_arguments()
156 156
157 # --- database 157 # --- database
158 - print(f"Database: {args.db}")  
159 - engine = create_engine(f"sqlite:///{args.db}", echo=False, future=True) 158 + print(f"Database '{args.db}'")
  159 + engine = create_engine(f"sqlite:///{args.db}", echo=False) # no logging
160 Base.metadata.create_all(engine) # Criates schema if needed 160 Base.metadata.create_all(engine) # Criates schema if needed
161 - session = Session(engine, future=True) # FIXME: future? 161 + session = Session(engine)
162 162
163 # --- build list of new students to insert 163 # --- build list of new students to insert
164 students = [] 164 students = []
165 165
166 if args.admin: 166 if args.admin:
167 - print("Adding user: 0, Admin.") 167 + print("Adding user (0, Admin)")
168 students.append({"uid": "0", "name": "Admin"}) 168 students.append({"uid": "0", "name": "Admin"})
169 169
170 for csvfile in args.csvfile: 170 for csvfile in args.csvfile:
171 - print("Adding users from:", csvfile) 171 + print(f"Adding users from '{csvfile}'")
172 students.extend(get_students_from_csv(csvfile)) 172 students.extend(get_students_from_csv(csvfile))
173 173
174 if args.add: 174 if args.add:
175 for uid, name in args.add: 175 for uid, name in args.add:
176 - print(f"Adding user: {uid}, {name}.") 176 + print(f"Adding user ({uid}, {name})")
177 students.append({"uid": uid, "name": name}) 177 students.append({"uid": uid, "name": name})
178 178
179 # --- insert new students 179 # --- insert new students
180 if students: 180 if students:
181 if args.pw is None: 181 if args.pw is None:
182 - print("Set passwords to empty") 182 + print("Passwords set to empty")
183 for s in students: 183 for s in students:
184 s["pw"] = "" 184 s["pw"] = ""
185 else: 185 else:
@@ -187,7 +187,8 @@ def main(): @@ -187,7 +187,8 @@ def main():
187 for s in students: 187 for s in students:
188 s["pw"] = ph.hash(args.pw) 188 s["pw"] = ph.hash(args.pw)
189 print(".", end="", flush=True) 189 print(".", end="", flush=True)
190 - print(f"\nInserting {len(students)}") 190 + print()
  191 + print(f"Inserting {len(students)}")
191 insert_students_into_db(session, students) 192 insert_students_into_db(session, students)
192 193
193 # --- update all students 194 # --- update all students
@@ -219,6 +220,8 @@ def main(): @@ -219,6 +220,8 @@ def main():
219 student.password = ph.hash(args.pw) 220 student.password = ph.hash(args.pw)
220 session.commit() 221 session.commit()
221 222
  223 + print("Done!\n")
  224 +
222 show_students_in_database(session, args.verbose) 225 show_students_in_database(session, args.verbose)
223 226
224 session.close() 227 session.close()