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
BUGS.md
1 1 BUGS:
2 2  
  3 +- session management. close after some inactive time.
3 4 - guardar state cada vez que topico termina
4 5 - mostrar quantas perguntas faltam
5 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 7 - implementar xsrf. Ver [http://www.tornadoweb.org/en/stable/guide/security.html#cross-site-request-forgery-protection]()
10 8  
11 9 TODO:
... ... @@ -16,6 +14,9 @@ TODO:
16 14  
17 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 20 - não entra à primeira
20 21 - configuração e linha de comando.
21 22 - o browser é redireccionado para /question em vez de fazer um post?? quando se pressiona enter numa caixa text edit.
... ...
app.py
... ... @@ -2,7 +2,7 @@
2 2 # python standard library
3 3 from contextlib import contextmanager # `with` statement in db sessions
4 4 import logging
5   -from os import path
  5 +from os import path, sys
6 6  
7 7 # user installed libraries
8 8 try:
... ... @@ -32,12 +32,12 @@ class LearnApp(object):
32 32 # online students
33 33 self.online = {}
34 34  
35   - # connect to database and check registered students
36   - self.db_setup('students.db') # FIXME
37   -
38 35 # build dependency graph
39 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 41 # add topics from depgraph to the database
42 42 self.db_add_topics()
43 43  
... ... @@ -155,7 +155,7 @@ class LearnApp(object):
155 155 # Receives a set of topics (strings like "math/algebra"),
156 156 # and recursively adds dependencies to the dependency graph
157 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 160 # Load configuration file
161 161 try:
... ... @@ -164,11 +164,12 @@ class LearnApp(object):
164 164 config = yaml.load(f)
165 165 except FileNotFoundError as e:
166 166 logger.error(f'File not found: "{config_file}"')
167   - raise e
  167 + sys.exit(1)
168 168  
169 169 prefix = config['path'] # FIXME default if does not exist?
170 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 174 # Build dependency graph
174 175 deps = config.get('dependencies', {})
... ... @@ -202,14 +203,15 @@ class LearnApp(object):
202 203 # ------------------------------------------------------------------------
203 204 # setup and check database
204 205 def db_setup(self, db):
  206 + logger.debug(f'LearnApp.db_setup("{db}")')
205 207 engine = create_engine(f'sqlite:///{db}', echo=False)
206 208 self.Session = sessionmaker(bind=engine)
207 209 try:
208 210 with self.db_session() as s:
209 211 n = s.query(Student).count()
210 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 215 else:
214 216 logger.info(f'Database has {n} students registered.')
215 217  
... ...