Commit a4925cb4b9abc2d48d48ed17c45495b6ab83e6a2

Authored by Miguel Barão
1 parent e1ad9bc6
Exists in master and in 1 other branch dev

- database configurable in the config.yaml file

- replaced some initial critical exceptions by sys.exit(1)
Showing 2 changed files with 15 additions and 12 deletions   Show diff stats
1 BUGS: 1 BUGS:
2 2
  3 +- session management. close after some inactive time.
3 - guardar state cada vez que topico termina 4 - guardar state cada vez que topico termina
4 - mostrar quantas perguntas faltam 5 - mostrar quantas perguntas faltam
5 - logs mostram que está a gerar cada pergunta 2 vezes...?? 6 - logs mostram que está a gerar cada pergunta 2 vezes...??
6 -- mostra tópicos do lado esquerdo, indicando quais estão feitos e quantas perguntas contêm.  
7 -- se students.db não existe, rebenta.  
8 -- database hardcoded in LearnApp.  
9 - implementar xsrf. Ver [http://www.tornadoweb.org/en/stable/guide/security.html#cross-site-request-forgery-protection]() 7 - implementar xsrf. Ver [http://www.tornadoweb.org/en/stable/guide/security.html#cross-site-request-forgery-protection]()
10 8
11 TODO: 9 TODO:
@@ -16,6 +14,9 @@ TODO: @@ -16,6 +14,9 @@ TODO:
16 14
17 SOLVED: 15 SOLVED:
18 16
  17 +- mostra tópicos do lado esquerdo, indicando quais estão feitos
  18 +- database hardcoded in LearnApp.
  19 +- se students.db não existe, rebenta.
19 - não entra à primeira 20 - não entra à primeira
20 - configuração e linha de comando. 21 - configuração e linha de comando.
21 - o browser é redireccionado para /question em vez de fazer um post?? quando se pressiona enter numa caixa text edit. 22 - o browser é redireccionado para /question em vez de fazer um post?? quando se pressiona enter numa caixa text edit.
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 # python standard library 2 # python standard library
3 from contextlib import contextmanager # `with` statement in db sessions 3 from contextlib import contextmanager # `with` statement in db sessions
4 import logging 4 import logging
5 -from os import path 5 +from os import path, sys
6 6
7 # user installed libraries 7 # user installed libraries
8 try: 8 try:
@@ -32,12 +32,12 @@ class LearnApp(object): @@ -32,12 +32,12 @@ class LearnApp(object):
32 # online students 32 # online students
33 self.online = {} 33 self.online = {}
34 34
35 - # connect to database and check registered students  
36 - self.db_setup('students.db') # FIXME  
37 -  
38 # build dependency graph 35 # build dependency graph
39 self.build_dependency_graph(conffile) # FIXME 36 self.build_dependency_graph(conffile) # FIXME
40 37
  38 + # connect to database and check registered students
  39 + self.db_setup(self.depgraph.graph['database']) # FIXME
  40 +
41 # add topics from depgraph to the database 41 # add topics from depgraph to the database
42 self.db_add_topics() 42 self.db_add_topics()
43 43
@@ -155,7 +155,7 @@ class LearnApp(object): @@ -155,7 +155,7 @@ class LearnApp(object):
155 # Receives a set of topics (strings like "math/algebra"), 155 # Receives a set of topics (strings like "math/algebra"),
156 # and recursively adds dependencies to the dependency graph 156 # and recursively adds dependencies to the dependency graph
157 def build_dependency_graph(self, config_file): 157 def build_dependency_graph(self, config_file):
158 - logger.debug(f'build_dependency_graph("{config_file}")') 158 + logger.debug(f'LearnApp.build_dependency_graph("{config_file}")')
159 159
160 # Load configuration file 160 # Load configuration file
161 try: 161 try:
@@ -164,11 +164,12 @@ class LearnApp(object): @@ -164,11 +164,12 @@ class LearnApp(object):
164 config = yaml.load(f) 164 config = yaml.load(f)
165 except FileNotFoundError as e: 165 except FileNotFoundError as e:
166 logger.error(f'File not found: "{config_file}"') 166 logger.error(f'File not found: "{config_file}"')
167 - raise e 167 + sys.exit(1)
168 168
169 prefix = config['path'] # FIXME default if does not exist? 169 prefix = config['path'] # FIXME default if does not exist?
170 title = config.get('title', '') 170 title = config.get('title', '')
171 - g = nx.DiGraph(path=prefix, title=title) 171 + database = config.get('database', 'students.db')
  172 + g = nx.DiGraph(path=prefix, title=title, database=database)
172 173
173 # Build dependency graph 174 # Build dependency graph
174 deps = config.get('dependencies', {}) 175 deps = config.get('dependencies', {})
@@ -202,14 +203,15 @@ class LearnApp(object): @@ -202,14 +203,15 @@ class LearnApp(object):
202 # ------------------------------------------------------------------------ 203 # ------------------------------------------------------------------------
203 # setup and check database 204 # setup and check database
204 def db_setup(self, db): 205 def db_setup(self, db):
  206 + logger.debug(f'LearnApp.db_setup("{db}")')
205 engine = create_engine(f'sqlite:///{db}', echo=False) 207 engine = create_engine(f'sqlite:///{db}', echo=False)
206 self.Session = sessionmaker(bind=engine) 208 self.Session = sessionmaker(bind=engine)
207 try: 209 try:
208 with self.db_session() as s: 210 with self.db_session() as s:
209 n = s.query(Student).count() 211 n = s.query(Student).count()
210 except Exception as e: 212 except Exception as e:
211 - logger.critical('Database not usable.')  
212 - raise e 213 + logger.critical(f'Database "{db}" not usable.')
  214 + sys.exit(1)
213 else: 215 else:
214 logger.info(f'Database has {n} students registered.') 216 logger.info(f'Database has {n} students registered.')
215 217