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