diff --git a/perguntations/app.py b/perguntations/app.py index 3d4f561..66f6a71 100644 --- a/perguntations/app.py +++ b/perguntations/app.py @@ -94,9 +94,9 @@ class App: raise AppException('No database, use "initdb" to create') # connect to database and check for admin & registered students - self._engine = create_engine(f"sqlite:///{dbfile}", future=True) + self._engine = create_engine(f"sqlite:///{dbfile}") try: - with Session(self._engine, future=True) as session: + with Session(self._engine) as session: query = select(Student.id, Student.name).where(Student.id != "0") dbstudents = session.execute(query).all() session.execute(select(Student).where(Student.id == "0")).one() @@ -135,7 +135,7 @@ class App: If successful returns None, else returns an error message """ try: - with Session(self._engine, future=True) as session: + with Session(self._engine) as session: query = select(Student.password).where(Student.id == uid) hashed = session.execute(query).scalar_one() except NoResultFound: @@ -168,7 +168,7 @@ class App: # ------------------------------------------------------------------------ async def set_password(self, uid: str, password: str) -> None: """change password in the database""" - with Session(self._engine, future=True) as session: + with Session(self._engine) as session: query = select(Student).where(Student.id == uid) student = session.execute(query).scalar_one() student.password = await hash_password(password) if password else "" @@ -265,14 +265,14 @@ class App: for n, q in enumerate(test["questions"]) ] - with Session(self._engine, future=True) as session: + with Session(self._engine) as session: session.add(test_row) session.commit() logger.info('"%s" database updated', uid) # ------------------------------------------------------------------------ def _correct_tests(self) -> None: - with Session(self._engine, future=True) as session: + with Session(self._engine) as session: # Find which tests have to be corrected query = ( select(Test) @@ -395,7 +395,7 @@ class App: def get_grades_csv(self): """generates a CSV with the grades of the test currently running""" test_ref = self._testfactory["ref"] - with Session(self._engine, future=True) as session: + with Session(self._engine) as session: query = ( select(Test.student_id, Test.grade, Test.starttime, Test.finishtime) .where(Test.ref == test_ref) @@ -416,7 +416,7 @@ class App: def get_detailed_grades_csv(self): """generates a CSV with the grades of the test""" test_ref = self._testfactory["ref"] - with Session(self._engine, future=True) as session: + with Session(self._engine) as session: query = ( select( Test.id, @@ -453,14 +453,14 @@ class App: # ------------------------------------------------------------------------ def get_json_filename_of_test(self, test_id): """get JSON filename from database given the test_id""" - with Session(self._engine, future=True) as session: + with Session(self._engine) as session: query = select(Test.filename).where(Test.id == test_id) return session.execute(query).scalar() # ------------------------------------------------------------------------ def get_grades(self, uid, ref): """get grades of student for a given testid""" - with Session(self._engine, future=True) as session: + with Session(self._engine) as session: query = ( select(Test.grade, Test.finishtime, Test.id) .where(Test.student_id == uid) @@ -520,7 +520,7 @@ class App: # ------------------------------------------------------------------------ async def insert_new_student(self, uid: str, name: str) -> None: """insert new student into the database""" - with Session(self._engine, future=True) as session: + with Session(self._engine) as session: try: session.add(Student(id=uid, name=name, password="")) session.commit() diff --git a/perguntations/initdb.py b/perguntations/initdb.py index 53971a7..19d40ec 100644 --- a/perguntations/initdb.py +++ b/perguntations/initdb.py @@ -155,31 +155,31 @@ def main(): args = parse_commandline_arguments() # --- database - print(f"Database: {args.db}") - engine = create_engine(f"sqlite:///{args.db}", echo=False, future=True) + print(f"Database '{args.db}'") + engine = create_engine(f"sqlite:///{args.db}", echo=False) # no logging Base.metadata.create_all(engine) # Criates schema if needed - session = Session(engine, future=True) # FIXME: future? + session = Session(engine) # --- build list of new students to insert students = [] if args.admin: - print("Adding user: 0, Admin.") + print("Adding user (0, Admin)") students.append({"uid": "0", "name": "Admin"}) for csvfile in args.csvfile: - print("Adding users from:", csvfile) + print(f"Adding users from '{csvfile}'") students.extend(get_students_from_csv(csvfile)) if args.add: for uid, name in args.add: - print(f"Adding user: {uid}, {name}.") + print(f"Adding user ({uid}, {name})") students.append({"uid": uid, "name": name}) # --- insert new students if students: if args.pw is None: - print("Set passwords to empty") + print("Passwords set to empty") for s in students: s["pw"] = "" else: @@ -187,7 +187,8 @@ def main(): for s in students: s["pw"] = ph.hash(args.pw) print(".", end="", flush=True) - print(f"\nInserting {len(students)}") + print() + print(f"Inserting {len(students)}") insert_students_into_db(session, students) # --- update all students @@ -219,6 +220,8 @@ def main(): student.password = ph.hash(args.pw) session.commit() + print("Done!\n") + show_students_in_database(session, args.verbose) session.close() -- libgit2 0.21.2