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,13 +55,15 @@ class LearnApp(object): | ||
| 55 | logger.info(f'User "{uid}" wrong password.') | 55 | logger.info(f'User "{uid}" wrong password.') |
| 56 | return False # wrong password | 56 | return False # wrong password |
| 57 | 57 | ||
| 58 | - student_state = s.query(StudentTopic).filter(StudentTopic.student_id == uid).all() | ||
| 59 | - | ||
| 60 | # success | 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 | self.online[uid] = { | 63 | self.online[uid] = { |
| 62 | 'name': student.name, | 64 | 'name': student.name, |
| 63 | 'number': student.id, | 65 | 'number': student.id, |
| 64 | - 'state': Knowledge(self.depgraph, student_state), | 66 | + 'state': Knowledge(self.depgraph, state), |
| 65 | } | 67 | } |
| 66 | logger.info(f'User "{uid}" logged in') | 68 | logger.info(f'User "{uid}" logged in') |
| 67 | return True | 69 | return True |
| @@ -70,16 +72,16 @@ class LearnApp(object): | @@ -70,16 +72,16 @@ class LearnApp(object): | ||
| 70 | # logout | 72 | # logout |
| 71 | def logout(self, uid): | 73 | def logout(self, uid): |
| 72 | state = self.online[uid]['state'].state # dict {node:level,...} | 74 | state = self.online[uid]['state'].state # dict {node:level,...} |
| 73 | - print(state) | ||
| 74 | 75 | ||
| 76 | + # save state to database | ||
| 75 | with self.db_session(autoflush=False) as s: | 77 | with self.db_session(autoflush=False) as s: |
| 76 | 78 | ||
| 77 | # update existing associations and remove from state dict | 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 | # insert the remaining ones | 86 | # insert the remaining ones |
| 85 | u = s.query(Student).get(uid) | 87 | u = s.query(Student).get(uid) |
| @@ -87,7 +89,7 @@ class LearnApp(object): | @@ -87,7 +89,7 @@ class LearnApp(object): | ||
| 87 | a = StudentTopic(level=l) | 89 | a = StudentTopic(level=l) |
| 88 | t = s.query(Topic).get(n) | 90 | t = s.query(Topic).get(n) |
| 89 | if t is None: # create if topic doesn't exist yet | 91 | if t is None: # create if topic doesn't exist yet |
| 90 | - t = Topic(n) | 92 | + t = Topic(id=n) |
| 91 | a.topic = t | 93 | a.topic = t |
| 92 | u.topics.append(a) | 94 | u.topics.append(a) |
| 93 | s.add(a) | 95 | s.add(a) |
| @@ -185,14 +187,14 @@ class LearnApp(object): | @@ -185,14 +187,14 @@ class LearnApp(object): | ||
| 185 | # ------------------------------------------------------------------------ | 187 | # ------------------------------------------------------------------------ |
| 186 | def db_add_topics(self): | 188 | def db_add_topics(self): |
| 187 | with self.db_session() as s: | 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 | nn = self.depgraph.nodes_iter() # topics in the graph | 191 | nn = self.depgraph.nodes_iter() # topics in the graph |
| 190 | s.add_all([Topic(id=n) for n in nn if n not in tt]) | 192 | s.add_all([Topic(id=n) for n in nn if n not in tt]) |
| 191 | 193 | ||
| 192 | # ------------------------------------------------------------------------ | 194 | # ------------------------------------------------------------------------ |
| 193 | # setup and check database | 195 | # setup and check database |
| 194 | def db_setup(self, db): | 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 | self.Session = sessionmaker(bind=engine) | 198 | self.Session = sessionmaker(bind=engine) |
| 197 | try: | 199 | try: |
| 198 | with self.db_session() as s: | 200 | with self.db_session() as s: |
knowledge.py
| @@ -24,7 +24,6 @@ class Knowledge(object): | @@ -24,7 +24,6 @@ class Knowledge(object): | ||
| 24 | self.seq = nx.topological_sort(self.depgraph) | 24 | self.seq = nx.topological_sort(self.depgraph) |
| 25 | self.topic = None | 25 | self.topic = None |
| 26 | 26 | ||
| 27 | - self.state = {'a': 3, 'b': 4} | ||
| 28 | 27 | ||
| 29 | def get_current_question(self): | 28 | def get_current_question(self): |
| 30 | return self.current_question | 29 | return self.current_question |