Commit 1bb7426ab9129e499b1e3b1c882e43109c049c1f
1 parent
8c6a2bf8
Exists in
master
and in
1 other branch
initdb: add option -U, --update-all to update password of all students
Showing
3 changed files
with
48 additions
and
33 deletions
Show diff stats
BUGS.md
1 | 1 | ||
2 | # BUGS | 2 | # BUGS |
3 | 3 | ||
4 | +- correct devia poder ser corrido mais que uma vez (por exemplo para alterar cotacoes, corrigir perguntas) | ||
4 | - nao esta a mostrar imagens?? internal server error? | 5 | - nao esta a mostrar imagens?? internal server error? |
5 | - guardar testes em JSON assim que sao atribuidos aos alunos (ou guardados inicialmente com um certo nome, e atribuidos posteriormente ao aluno). | 6 | - guardar testes em JSON assim que sao atribuidos aos alunos (ou guardados inicialmente com um certo nome, e atribuidos posteriormente ao aluno). |
6 | - cookies existe um perguntations_user e um user. De onde vem o user? | 7 | - cookies existe um perguntations_user e um user. De onde vem o user? |
perguntations/initdb.py
@@ -42,24 +42,28 @@ def parse_commandline_arguments(): | @@ -42,24 +42,28 @@ def parse_commandline_arguments(): | ||
42 | 42 | ||
43 | parser.add_argument('-A', '--admin', | 43 | parser.add_argument('-A', '--admin', |
44 | action='store_true', | 44 | action='store_true', |
45 | - help='insert the admin user') | 45 | + help='insert admin user 0 "Admin"') |
46 | 46 | ||
47 | parser.add_argument('-a', '--add', | 47 | parser.add_argument('-a', '--add', |
48 | nargs=2, | 48 | nargs=2, |
49 | action='append', | 49 | action='append', |
50 | metavar=('uid', 'name'), | 50 | metavar=('uid', 'name'), |
51 | - help='add new user') | 51 | + help='add new user id and name') |
52 | 52 | ||
53 | parser.add_argument('-u', '--update', | 53 | parser.add_argument('-u', '--update', |
54 | nargs='+', | 54 | nargs='+', |
55 | metavar='uid', | 55 | metavar='uid', |
56 | default=[], | 56 | default=[], |
57 | - help='users to update') | 57 | + help='list of users whose password is to be updated') |
58 | + | ||
59 | + parser.add_argument('-U', '--update-all', | ||
60 | + action='store_true', | ||
61 | + help='all except admin will have the password updated') | ||
58 | 62 | ||
59 | parser.add_argument('--pw', | 63 | parser.add_argument('--pw', |
60 | default=None, | 64 | default=None, |
61 | type=str, | 65 | type=str, |
62 | - help='set password for new and updated users') | 66 | + help='password for new or updated users') |
63 | 67 | ||
64 | parser.add_argument('-V', '--verbose', | 68 | parser.add_argument('-V', '--verbose', |
65 | action='store_true', | 69 | action='store_true', |
@@ -152,45 +156,55 @@ def main(): | @@ -152,45 +156,55 @@ def main(): | ||
152 | 156 | ||
153 | args = parse_commandline_arguments() | 157 | args = parse_commandline_arguments() |
154 | 158 | ||
155 | - # --- make list of students to insert/update | ||
156 | - students = [] | 159 | + # --- database |
160 | + print(f'Using database: {args.db}') | ||
161 | + engine = sa.create_engine(f'sqlite:///{args.db}', echo=False) | ||
162 | + Base.metadata.create_all(engine) # Criates schema if needed | ||
163 | + SessionMaker = sa.orm.sessionmaker(bind=engine) | ||
164 | + session = SessionMaker() | ||
157 | 165 | ||
158 | - for csvfile in args.csvfile: | ||
159 | - print('Adding users from:', csvfile) | ||
160 | - students.extend(get_students_from_csv(csvfile)) | 166 | + # --- make list of students to insert |
167 | + new_students = [] | ||
161 | 168 | ||
162 | if args.admin: | 169 | if args.admin: |
163 | print('Adding user: 0, Admin.') | 170 | print('Adding user: 0, Admin.') |
164 | - students.append({'uid': '0', 'name': 'Admin'}) | 171 | + new_students.append({'uid': '0', 'name': 'Admin'}) |
172 | + | ||
173 | + for csvfile in args.csvfile: | ||
174 | + print('Adding users from:', csvfile) | ||
175 | + new_students.extend(get_students_from_csv(csvfile)) | ||
165 | 176 | ||
166 | if args.add: | 177 | if args.add: |
167 | for uid, name in args.add: | 178 | for uid, name in args.add: |
168 | print(f'Adding user: {uid}, {name}.') | 179 | print(f'Adding user: {uid}, {name}.') |
169 | - students.append({'uid': uid, 'name': name}) | 180 | + new_students.append({'uid': uid, 'name': name}) |
170 | 181 | ||
171 | - # --- password hashing | ||
172 | - if students: | 182 | + # --- insert new students |
183 | + if new_students: | ||
173 | print('Generating password hashes', end='') | 184 | print('Generating password hashes', end='') |
174 | with ThreadPoolExecutor() as executor: # hashing in parallel | 185 | with ThreadPoolExecutor() as executor: # hashing in parallel |
175 | - executor.map(lambda s: hashpw(s, args.pw), students) | 186 | + executor.map(lambda s: hashpw(s, args.pw), new_students) |
187 | + print(f'\nInserting {len(new_students)}') | ||
188 | + insert_students_into_db(session, new_students) | ||
189 | + | ||
190 | + # --- update all students | ||
191 | + if args.update_all: | ||
192 | + all_students = session.query(Student).filter(Student.id != '0').all() | ||
193 | + print(f'Updating password of {len(all_students)} users', end='') | ||
194 | + for student in all_students: | ||
195 | + password = (args.pw or student.id).encode('utf-8') | ||
196 | + student.password = bcrypt.hashpw(password, bcrypt.gensalt()) | ||
197 | + print('.', end='', flush=True) | ||
176 | print() | 198 | print() |
199 | + session.commit() | ||
177 | 200 | ||
178 | - # --- database stuff | ||
179 | - print(f'Using database: {args.db}') | ||
180 | - engine = sa.create_engine(f'sqlite:///{args.db}', echo=False) | ||
181 | - Base.metadata.create_all(engine) # Criates schema if needed | ||
182 | - SessionMaker = sa.orm.sessionmaker(bind=engine) | ||
183 | - session = SessionMaker() | ||
184 | - | ||
185 | - if students: | ||
186 | - print(f'Inserting {len(students)}') | ||
187 | - insert_students_into_db(session, students) | ||
188 | - | ||
189 | - for student_id in args.update: | ||
190 | - print(f'Updating password of: {student_id}') | ||
191 | - student = session.query(Student).get(student_id) | ||
192 | - password = (args.pw or student_id).encode('utf-8') | ||
193 | - student.password = bcrypt.hashpw(password, bcrypt.gensalt()) | 201 | + # --- update some students |
202 | + else: | ||
203 | + for student_id in args.update: | ||
204 | + print(f'Updating password of {student_id}') | ||
205 | + student = session.query(Student).get(student_id) | ||
206 | + password = (args.pw or student_id).encode('utf-8') | ||
207 | + student.password = bcrypt.hashpw(password, bcrypt.gensalt()) | ||
194 | session.commit() | 208 | session.commit() |
195 | 209 | ||
196 | show_students_in_database(session, args.verbose) | 210 | show_students_in_database(session, args.verbose) |
perguntations/serve.py
@@ -50,7 +50,7 @@ class WebApplication(tornado.web.Application): | @@ -50,7 +50,7 @@ class WebApplication(tornado.web.Application): | ||
50 | 50 | ||
51 | settings = { | 51 | settings = { |
52 | 'template_path': path.join(path.dirname(__file__), 'templates'), | 52 | 'template_path': path.join(path.dirname(__file__), 'templates'), |
53 | - 'static_path': path.join(path.dirname(__file__), 'static'), | 53 | + 'static_path': path.join(path.dirname(__file__), 'static'), |
54 | 'static_url_prefix': '/static/', | 54 | 'static_url_prefix': '/static/', |
55 | 'xsrf_cookies': True, | 55 | 'xsrf_cookies': True, |
56 | 'cookie_secret': base64.b64encode(uuid.uuid4().bytes), | 56 | 'cookie_secret': base64.b64encode(uuid.uuid4().bytes), |
@@ -209,7 +209,7 @@ class LogoutHandler(BaseHandler): | @@ -209,7 +209,7 @@ class LogoutHandler(BaseHandler): | ||
209 | 209 | ||
210 | 210 | ||
211 | # ---------------------------------------------------------------------------- | 211 | # ---------------------------------------------------------------------------- |
212 | -# Test shown to students | 212 | +# Handles the TEST |
213 | # ---------------------------------------------------------------------------- | 213 | # ---------------------------------------------------------------------------- |
214 | # pylint: disable=abstract-method | 214 | # pylint: disable=abstract-method |
215 | class RootHandler(BaseHandler): | 215 | class RootHandler(BaseHandler): |
@@ -298,7 +298,7 @@ class RootHandler(BaseHandler): | @@ -298,7 +298,7 @@ class RootHandler(BaseHandler): | ||
298 | 298 | ||
299 | # show final grade and grades of other tests in the database | 299 | # show final grade and grades of other tests in the database |
300 | # allgrades = self.testapp.get_student_grades_from_all_tests(uid) | 300 | # allgrades = self.testapp.get_student_grades_from_all_tests(uid) |
301 | - grade = self.testapp.get_student_grade(uid) | 301 | + # grade = self.testapp.get_student_grade(uid) |
302 | 302 | ||
303 | self.render('grade.html', t=test) | 303 | self.render('grade.html', t=test) |
304 | self.clear_cookie('perguntations_user') | 304 | self.clear_cookie('perguntations_user') |