From a4925cb4b9abc2d48d48ed17c45495b6ab83e6a2 Mon Sep 17 00:00:00 2001 From: Miguel Barão Date: Mon, 6 Mar 2017 13:58:19 +0000 Subject: [PATCH] - database configurable in the config.yaml file - replaced some initial critical exceptions by sys.exit(1) --- BUGS.md | 7 ++++--- app.py | 20 +++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/BUGS.md b/BUGS.md index 55fc9b3..3c507b5 100644 --- a/BUGS.md +++ b/BUGS.md @@ -1,11 +1,9 @@ BUGS: +- session management. close after some inactive time. - guardar state cada vez que topico termina - mostrar quantas perguntas faltam - logs mostram que está a gerar cada pergunta 2 vezes...?? -- mostra tópicos do lado esquerdo, indicando quais estão feitos e quantas perguntas contêm. -- se students.db não existe, rebenta. -- database hardcoded in LearnApp. - implementar xsrf. Ver [http://www.tornadoweb.org/en/stable/guide/security.html#cross-site-request-forgery-protection]() TODO: @@ -16,6 +14,9 @@ TODO: SOLVED: +- mostra tópicos do lado esquerdo, indicando quais estão feitos +- database hardcoded in LearnApp. +- se students.db não existe, rebenta. - não entra à primeira - configuração e linha de comando. - o browser é redireccionado para /question em vez de fazer um post?? quando se pressiona enter numa caixa text edit. diff --git a/app.py b/app.py index 21d80d6..ac9284c 100644 --- a/app.py +++ b/app.py @@ -2,7 +2,7 @@ # python standard library from contextlib import contextmanager # `with` statement in db sessions import logging -from os import path +from os import path, sys # user installed libraries try: @@ -32,12 +32,12 @@ class LearnApp(object): # online students self.online = {} - # connect to database and check registered students - self.db_setup('students.db') # FIXME - # build dependency graph self.build_dependency_graph(conffile) # FIXME + # connect to database and check registered students + self.db_setup(self.depgraph.graph['database']) # FIXME + # add topics from depgraph to the database self.db_add_topics() @@ -155,7 +155,7 @@ class LearnApp(object): # Receives a set of topics (strings like "math/algebra"), # and recursively adds dependencies to the dependency graph def build_dependency_graph(self, config_file): - logger.debug(f'build_dependency_graph("{config_file}")') + logger.debug(f'LearnApp.build_dependency_graph("{config_file}")') # Load configuration file try: @@ -164,11 +164,12 @@ class LearnApp(object): config = yaml.load(f) except FileNotFoundError as e: logger.error(f'File not found: "{config_file}"') - raise e + sys.exit(1) prefix = config['path'] # FIXME default if does not exist? title = config.get('title', '') - g = nx.DiGraph(path=prefix, title=title) + database = config.get('database', 'students.db') + g = nx.DiGraph(path=prefix, title=title, database=database) # Build dependency graph deps = config.get('dependencies', {}) @@ -202,14 +203,15 @@ class LearnApp(object): # ------------------------------------------------------------------------ # setup and check database def db_setup(self, db): + logger.debug(f'LearnApp.db_setup("{db}")') engine = create_engine(f'sqlite:///{db}', echo=False) self.Session = sessionmaker(bind=engine) try: with self.db_session() as s: n = s.query(Student).count() except Exception as e: - logger.critical('Database not usable.') - raise e + logger.critical(f'Database "{db}" not usable.') + sys.exit(1) else: logger.info(f'Database has {n} students registered.') -- libgit2 0.21.2