From b724b70f2c9f19f82a8ea9113e6f41d008b2b008 Mon Sep 17 00:00:00 2001 From: Miguel Barão Date: Fri, 29 Jan 2021 10:39:48 +0000 Subject: [PATCH] initdb: add option -U, --update-all to update password of all students other minor changes --- BUGS.md | 12 +++++++----- perguntations/__init__.py | 2 +- perguntations/initdb.py | 74 ++++++++++++++++++++++++++++++++++++++++++++------------------------------ perguntations/serve.py | 6 +++--- perguntations/templates/question-checkbox.html | 4 ++-- perguntations/templates/question-radio.html | 4 ++-- perguntations/templates/question-text.html | 2 +- perguntations/templates/question-textarea.html | 2 +- perguntations/templates/test.html | 16 +++++++++++----- perguntations/testfactory.py | 12 ++++++------ update.sh | 6 ++++++ 11 files changed, 84 insertions(+), 56 deletions(-) create mode 100755 update.sh diff --git a/BUGS.md b/BUGS.md index cc4fe1d..49e3913 100644 --- a/BUGS.md +++ b/BUGS.md @@ -1,12 +1,11 @@ # BUGS -- cookies existe um perguntations_user e um user. De onde vem o user? +- correct devia poder ser corrido mais que uma vez (por exemplo para alterar cotacoes, corrigir perguntas) - nao esta a mostrar imagens?? internal server error? -- JOBE correct async -- esta a corrigir código JOBE mesmo que nao tenha respondido??? +- guardar testes em JSON assim que sao atribuidos aos alunos (ou guardados inicialmente com um certo nome, e atribuidos posteriormente ao aluno). +- cookies existe um perguntations_user e um user. De onde vem o user? - QuestionCode falta reportar nos comments os vários erros que podem ocorrer (timeout, etc) - - algumas vezes a base de dados guarda o mesmo teste em duplicado. ver se dois submits dao origem a duas correcções. talvez a base de dados devesse ter como chave do teste um id que fosse único desse teste particular (não um auto counter, nem ref do teste) - em caso de timeout na submissão (e.g. JOBE ou script nao responde) a correcção não termina e o teste não é guardado. @@ -19,11 +18,12 @@ talvez a base de dados devesse ter como chave do teste um id que fosse único de - a revisao do teste não mostra as imagens. - Test.reset_answers() unused. - teste nao esta a mostrar imagens de vez em quando.??? -- testar as perguntas todas no início do teste como o aprendizations. - show-ref nao esta a funcionar na correccao (pelo menos) # TODO +- JOBE correct async +- esta a corrigir código JOBE mesmo que nao tenha respondido??? - permitir remover alunos que estão online para poderem comecar de novo. - guardar nota final grade truncado em zero e sem ser truncado (quando é necessário fazer correcções à mão às perguntas, é necessário o valor não truncado) - stress tests. use https://locust.io @@ -74,6 +74,8 @@ ou usar push (websockets?) # FIXED +- testar as perguntas todas no início do teste como o aprendizations. +- adicionar identificacao do aluno no jumbotron inicial do teste, para que ao imprimir para pdf a identificacao do aluno venha escrita no documento. - internal server error quando em --review, download csv detalhado. - perguntas repetidas (mesma ref) dao asneira, porque a referencia é usada como chave em varios sitios e as chaves nao podem ser dupplicadas. da asneira pelo menos na funcao get_questions_csv. na base de dados tem de estar registado tb o numero da pergunta, caso contrario é impossível saber a qual corresponde. diff --git a/perguntations/__init__.py b/perguntations/__init__.py index ae58bdf..1ed54dc 100644 --- a/perguntations/__init__.py +++ b/perguntations/__init__.py @@ -32,7 +32,7 @@ proof of submission and for review. ''' APP_NAME = 'perguntations' -APP_VERSION = '2020.12.dev1' +APP_VERSION = '2021.01.dev1' APP_DESCRIPTION = __doc__ __author__ = 'Miguel Barão' diff --git a/perguntations/initdb.py b/perguntations/initdb.py index b1c316a..5ff5c72 100644 --- a/perguntations/initdb.py +++ b/perguntations/initdb.py @@ -42,24 +42,28 @@ def parse_commandline_arguments(): parser.add_argument('-A', '--admin', action='store_true', - help='insert the admin user') + help='insert admin user 0 "Admin"') parser.add_argument('-a', '--add', nargs=2, action='append', metavar=('uid', 'name'), - help='add new user') + help='add new user id and name') parser.add_argument('-u', '--update', nargs='+', metavar='uid', default=[], - help='users to update') + help='list of users whose password is to be updated') + + parser.add_argument('-U', '--update-all', + action='store_true', + help='all except admin will have the password updated') parser.add_argument('--pw', default=None, type=str, - help='set password for new and updated users') + help='password for new or updated users') parser.add_argument('-V', '--verbose', action='store_true', @@ -152,45 +156,55 @@ def main(): args = parse_commandline_arguments() - # --- make list of students to insert/update - students = [] + # --- database + print(f'Using database: {args.db}') + engine = sa.create_engine(f'sqlite:///{args.db}', echo=False) + Base.metadata.create_all(engine) # Criates schema if needed + SessionMaker = sa.orm.sessionmaker(bind=engine) + session = SessionMaker() - for csvfile in args.csvfile: - print('Adding users from:', csvfile) - students.extend(get_students_from_csv(csvfile)) + # --- make list of students to insert + new_students = [] if args.admin: print('Adding user: 0, Admin.') - students.append({'uid': '0', 'name': 'Admin'}) + new_students.append({'uid': '0', 'name': 'Admin'}) + + for csvfile in args.csvfile: + print('Adding users from:', csvfile) + new_students.extend(get_students_from_csv(csvfile)) if args.add: for uid, name in args.add: print(f'Adding user: {uid}, {name}.') - students.append({'uid': uid, 'name': name}) + new_students.append({'uid': uid, 'name': name}) - # --- password hashing - if students: + # --- insert new students + if new_students: print('Generating password hashes', end='') with ThreadPoolExecutor() as executor: # hashing in parallel - executor.map(lambda s: hashpw(s, args.pw), students) + executor.map(lambda s: hashpw(s, args.pw), new_students) + print(f'\nInserting {len(new_students)}') + insert_students_into_db(session, new_students) + + # --- update all students + if args.update_all: + all_students = session.query(Student).filter(Student.id != '0').all() + print(f'Updating password of {len(all_students)} users', end='') + for student in all_students: + password = (args.pw or student.id).encode('utf-8') + student.password = bcrypt.hashpw(password, bcrypt.gensalt()) + print('.', end='', flush=True) print() + session.commit() - # --- database stuff - print(f'Using database: {args.db}') - engine = sa.create_engine(f'sqlite:///{args.db}', echo=False) - Base.metadata.create_all(engine) # Criates schema if needed - SessionMaker = sa.orm.sessionmaker(bind=engine) - session = SessionMaker() - - if students: - print(f'Inserting {len(students)}') - insert_students_into_db(session, students) - - for student_id in args.update: - print(f'Updating password of: {student_id}') - student = session.query(Student).get(student_id) - password = (args.pw or student_id).encode('utf-8') - student.password = bcrypt.hashpw(password, bcrypt.gensalt()) + # --- update some students + else: + for student_id in args.update: + print(f'Updating password of {student_id}') + student = session.query(Student).get(student_id) + password = (args.pw or student_id).encode('utf-8') + student.password = bcrypt.hashpw(password, bcrypt.gensalt()) session.commit() show_students_in_database(session, args.verbose) diff --git a/perguntations/serve.py b/perguntations/serve.py index d4b843d..d943bf2 100644 --- a/perguntations/serve.py +++ b/perguntations/serve.py @@ -50,7 +50,7 @@ class WebApplication(tornado.web.Application): settings = { 'template_path': path.join(path.dirname(__file__), 'templates'), - 'static_path': path.join(path.dirname(__file__), 'static'), + 'static_path': path.join(path.dirname(__file__), 'static'), 'static_url_prefix': '/static/', 'xsrf_cookies': True, 'cookie_secret': base64.b64encode(uuid.uuid4().bytes), @@ -209,7 +209,7 @@ class LogoutHandler(BaseHandler): # ---------------------------------------------------------------------------- -# Test shown to students +# Handles the TEST # ---------------------------------------------------------------------------- # pylint: disable=abstract-method class RootHandler(BaseHandler): @@ -298,7 +298,7 @@ class RootHandler(BaseHandler): # show final grade and grades of other tests in the database # allgrades = self.testapp.get_student_grades_from_all_tests(uid) - grade = self.testapp.get_student_grade(uid) + # grade = self.testapp.get_student_grade(uid) self.render('grade.html', t=test) self.clear_cookie('perguntations_user') diff --git a/perguntations/templates/question-checkbox.html b/perguntations/templates/question-checkbox.html index 7c2ee25..e1cfd1a 100644 --- a/perguntations/templates/question-checkbox.html +++ b/perguntations/templates/question-checkbox.html @@ -8,7 +8,7 @@
- +
@@ -18,4 +18,4 @@ {% end %}
-{% end %} \ No newline at end of file +{% end %} diff --git a/perguntations/templates/question-radio.html b/perguntations/templates/question-radio.html index e57eb89..f68ba7a 100644 --- a/perguntations/templates/question-radio.html +++ b/perguntations/templates/question-radio.html @@ -8,7 +8,7 @@
- +
@@ -18,4 +18,4 @@ {% end %}
-{% end %} \ No newline at end of file +{% end %} diff --git a/perguntations/templates/question-text.html b/perguntations/templates/question-text.html index dfbb295..535e2f9 100644 --- a/perguntations/templates/question-text.html +++ b/perguntations/templates/question-text.html @@ -2,6 +2,6 @@ {% block answer %}
- +
{% end %} diff --git a/perguntations/templates/question-textarea.html b/perguntations/templates/question-textarea.html index 1216286..d6afa28 100644 --- a/perguntations/templates/question-textarea.html +++ b/perguntations/templates/question-textarea.html @@ -2,6 +2,6 @@ {% block answer %} -
+
{% end %} diff --git a/perguntations/templates/test.html b/perguntations/templates/test.html index 6a02285..6dbb116 100644 --- a/perguntations/templates/test.html +++ b/perguntations/templates/test.html @@ -88,11 +88,18 @@
-

{{ t['title'] }}

- +

{{ t['title'] }}


-
+
+ +
{{ escape(t['student']['name']) }}
+
+
+ +
{{ escape(t['student']['number']) }}
+
+
{{ str(t['duration'])+' minutos' if t['duration'] > 0 else 'sem limite de tempo' }}
@@ -101,10 +108,9 @@
{{ 'automática no fim do tempo' if t['autosubmit'] else 'manual' }}
-
-
+ {% module xsrf_form_html() %} {% for i, q in enumerate(t['questions']) %} diff --git a/perguntations/testfactory.py b/perguntations/testfactory.py index ad92d28..79ef45b 100644 --- a/perguntations/testfactory.py +++ b/perguntations/testfactory.py @@ -234,7 +234,7 @@ class TestFactory(dict): if question['type'] in ('code', 'textarea'): if 'tests_right' in question: - for i, right_answer in enumerate(question['tests_right']): + for tnum, right_answer in enumerate(question['tests_right']): try: question.set_answer(right_answer) question.correct() @@ -243,11 +243,11 @@ class TestFactory(dict): raise TestFactoryException(msg) from exc if question['grade'] == 1.0: - logger.info(' test %i Ok', i) + logger.info(' test %i Ok', tnum) else: - logger.error(' TEST %i IS WRONG!!!', i) + logger.error(' TEST %i IS WRONG!!!', tnum) elif 'tests_wrong' in question: - for i, wrong_answer in enumerate(question['tests_wrong']): + for tnum, wrong_answer in enumerate(question['tests_wrong']): try: question.set_answer(wrong_answer) question.correct() @@ -256,9 +256,9 @@ class TestFactory(dict): raise TestFactoryException(msg) from exc if question['grade'] < 1.0: - logger.info(' test %i Ok', i) + logger.info(' test %i Ok', tnum) else: - logger.error(' TEST %i IS WRONG!!!', i) + logger.error(' TEST %i IS WRONG!!!', tnum) else: try: question.set_answer('') diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..daf771c --- /dev/null +++ b/update.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +git pull +npm update +pip install -U . + -- libgit2 0.21.2