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