diff --git a/BUGS.md b/BUGS.md index a9558a7..49e3913 100644 --- a/BUGS.md +++ b/BUGS.md @@ -1,6 +1,7 @@ # BUGS +- correct devia poder ser corrido mais que uma vez (por exemplo para alterar cotacoes, corrigir perguntas) - nao esta a mostrar imagens?? internal server error? - 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? 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') -- libgit2 0.21.2