Commit 7f9b7abde22ceca857af4b74362f1485cf3dd104

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

update initdb.py and learnapp.py to sqlalchemy 2.0

aprendizations/initdb.py
... ... @@ -131,9 +131,9 @@ def main():
131 131  
132 132 # --- database stuff
133 133 print(f'Database: {args.db}')
134   - engine = create_engine(f'sqlite:///{args.db}', echo=False, future=True)
  134 + engine = create_engine(f'sqlite:///{args.db}')
135 135 Base.metadata.create_all(engine) # Creates schema if needed
136   - session = Session(engine, future=True)
  136 + session = Session(engine)
137 137  
138 138 # --- make list of students to insert/update
139 139 students = []
... ... @@ -183,7 +183,7 @@ def main():
183 183 count += 1
184 184 print(f' {sid}, {student.name}')
185 185 passwd = (args.pw or sid).encode('utf-8')
186   - student.password = bcrypt.hashpw(passwd, bcrypt.gensalt())
  186 + student.password = str(bcrypt.hashpw(passwd, bcrypt.gensalt()))
187 187  
188 188 session.commit()
189 189 print(f'Total {count} password(s) updated.')
... ...
aprendizations/learnapp.py
... ... @@ -164,7 +164,7 @@ class LearnApp():
164 164 await asyncio.sleep(random())
165 165  
166 166 query = select(Student).where(Student.id == uid)
167   - with Session(self._engine, future=True) as session:
  167 + with Session(self._engine) as session:
168 168 student = session.execute(query).scalar_one_or_none()
169 169 if student is None:
170 170 logger.info('User "%s" does not exist', uid)
... ... @@ -186,7 +186,7 @@ class LearnApp():
186 186  
187 187 # get topics for this student and set its current state
188 188 query = select(StudentTopic).where(StudentTopic.student_id == uid)
189   - with Session(self._engine, future=True) as session:
  189 + with Session(self._engine) as session:
190 190 student_topics = session.execute(query).scalars().all()
191 191  
192 192 state = {t.topic_id: {
... ... @@ -230,7 +230,7 @@ class LearnApp():
230 230 bcrypt.gensalt())
231 231  
232 232 query = select(Student).where(Student.id == uid)
233   - with Session(self._engine, future=True) as session:
  233 + with Session(self._engine) as session:
234 234 session.execute(query).scalar_one().password = hashed_pw
235 235 session.commit()
236 236  
... ... @@ -260,7 +260,7 @@ class LearnApp():
260 260 finishtime=str(question['finish_time']),
261 261 student_id=uid,
262 262 topic_id=topic_id)
263   - with Session(self._engine, future=True) as session:
  263 + with Session(self._engine) as session:
264 264 session.add(answer)
265 265 session.commit()
266 266  
... ... @@ -285,7 +285,7 @@ class LearnApp():
285 285 query = select(StudentTopic) \
286 286 .where(StudentTopic.student_id == uid) \
287 287 .where(StudentTopic.topic_id == tid)
288   - with Session(self._engine, future=True) as session:
  288 + with Session(self._engine) as session:
289 289 student_topic = session.execute(query).scalar_one_or_none()
290 290 if student_topic is None:
291 291 # insert new studenttopic into database
... ... @@ -343,7 +343,7 @@ class LearnApp():
343 343 '''
344 344 Fill table 'Topic' with topics from the graph, if new
345 345 '''
346   - with Session(self._engine, future=True) as session:
  346 + with Session(self._engine) as session:
347 347 db_topics = session.execute(select(Topic.id)).scalars().all()
348 348 new = [Topic(id=t) for t in topics if t not in db_topics]
349 349 if new:
... ... @@ -360,12 +360,13 @@ class LearnApp():
360 360 if not exists(database):
361 361 raise LearnException('Database does not exist')
362 362  
363   - self._engine = create_engine(f'sqlite:///{database}', future=True)
  363 + # echo=True enables logging of the SQL emitted by sqlalchemy
  364 + self._engine = create_engine(f'sqlite:///{database}', echo=False)
364 365 try:
365 366 query_students = select(func.count(Student.id))
366 367 query_topics = select(func.count(Topic.id))
367 368 query_answers = select(func.count(Answer.id))
368   - with Session(self._engine, future=True) as session:
  369 + with Session(self._engine) as session:
369 370 count_students = session.execute(query_students).scalar()
370 371 count_topics = session.execute(query_topics).scalar()
371 372 count_answers = session.execute(query_answers).scalar()
... ... @@ -584,7 +585,7 @@ class LearnApp():
584 585 StudentTopic.topic_id,
585 586 StudentTopic.level,
586 587 StudentTopic.date)
587   - with Session(self._engine, future=True) as session:
  588 + with Session(self._engine) as session:
588 589  
589 590 # all students in the database FIXME only with answers of this course
590 591 students = session.execute(query_students).all()
... ...