Commit 1cdce3ee0cbcb1564519246f10636adef3ebda90
1 parent
1a34901f
Exists in
master
and in
1 other branch
- get login state from database
Showing
2 changed files
with
14 additions
and
13 deletions
Show diff stats
app.py
| ... | ... | @@ -55,13 +55,15 @@ class LearnApp(object): |
| 55 | 55 | logger.info(f'User "{uid}" wrong password.') |
| 56 | 56 | return False # wrong password |
| 57 | 57 | |
| 58 | - student_state = s.query(StudentTopic).filter(StudentTopic.student_id == uid).all() | |
| 59 | - | |
| 60 | 58 | # success |
| 59 | + | |
| 60 | + tt = s.query(StudentTopic).filter(StudentTopic.student_id == uid) | |
| 61 | + state = {t.topic_id: t.level for t in tt} | |
| 62 | + | |
| 61 | 63 | self.online[uid] = { |
| 62 | 64 | 'name': student.name, |
| 63 | 65 | 'number': student.id, |
| 64 | - 'state': Knowledge(self.depgraph, student_state), | |
| 66 | + 'state': Knowledge(self.depgraph, state), | |
| 65 | 67 | } |
| 66 | 68 | logger.info(f'User "{uid}" logged in') |
| 67 | 69 | return True |
| ... | ... | @@ -70,16 +72,16 @@ class LearnApp(object): |
| 70 | 72 | # logout |
| 71 | 73 | def logout(self, uid): |
| 72 | 74 | state = self.online[uid]['state'].state # dict {node:level,...} |
| 73 | - print(state) | |
| 74 | 75 | |
| 76 | + # save state to database | |
| 75 | 77 | with self.db_session(autoflush=False) as s: |
| 76 | 78 | |
| 77 | 79 | # update existing associations and remove from state dict |
| 78 | - aa = s.query(StudentTopic).filter_by(student_id=uid).all() | |
| 79 | - for a in aa: | |
| 80 | - print('update ', a) | |
| 81 | - a.level = state.pop(a.topic_id) # update | |
| 82 | - s.add_all(aa) | |
| 80 | + for a in s.query(StudentTopic).filter_by(student_id=uid): | |
| 81 | + if a.topic_id in state: | |
| 82 | + a.level = state.pop(a.topic_id) # update | |
| 83 | + s.add(a) | |
| 84 | + # s.add_all(aa) | |
| 83 | 85 | |
| 84 | 86 | # insert the remaining ones |
| 85 | 87 | u = s.query(Student).get(uid) |
| ... | ... | @@ -87,7 +89,7 @@ class LearnApp(object): |
| 87 | 89 | a = StudentTopic(level=l) |
| 88 | 90 | t = s.query(Topic).get(n) |
| 89 | 91 | if t is None: # create if topic doesn't exist yet |
| 90 | - t = Topic(n) | |
| 92 | + t = Topic(id=n) | |
| 91 | 93 | a.topic = t |
| 92 | 94 | u.topics.append(a) |
| 93 | 95 | s.add(a) |
| ... | ... | @@ -185,14 +187,14 @@ class LearnApp(object): |
| 185 | 187 | # ------------------------------------------------------------------------ |
| 186 | 188 | def db_add_topics(self): |
| 187 | 189 | with self.db_session() as s: |
| 188 | - tt = [t[0] for t in s.query(Topic.id).all()] # db list of topics | |
| 190 | + tt = [t[0] for t in s.query(Topic.id)] # db list of topics | |
| 189 | 191 | nn = self.depgraph.nodes_iter() # topics in the graph |
| 190 | 192 | s.add_all([Topic(id=n) for n in nn if n not in tt]) |
| 191 | 193 | |
| 192 | 194 | # ------------------------------------------------------------------------ |
| 193 | 195 | # setup and check database |
| 194 | 196 | def db_setup(self, db): |
| 195 | - engine = create_engine(f'sqlite:///{db}', echo=True) | |
| 197 | + engine = create_engine(f'sqlite:///{db}', echo=False) | |
| 196 | 198 | self.Session = sessionmaker(bind=engine) |
| 197 | 199 | try: |
| 198 | 200 | with self.db_session() as s: | ... | ... |
knowledge.py