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 | 2 | # BUGS |
3 | 3 | |
4 | +- correct devia poder ser corrido mais que uma vez (por exemplo para alterar cotacoes, corrigir perguntas) | |
4 | 5 | - nao esta a mostrar imagens?? internal server error? |
5 | 6 | - guardar testes em JSON assim que sao atribuidos aos alunos (ou guardados inicialmente com um certo nome, e atribuidos posteriormente ao aluno). |
6 | 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 | 42 | |
43 | 43 | parser.add_argument('-A', '--admin', |
44 | 44 | action='store_true', |
45 | - help='insert the admin user') | |
45 | + help='insert admin user 0 "Admin"') | |
46 | 46 | |
47 | 47 | parser.add_argument('-a', '--add', |
48 | 48 | nargs=2, |
49 | 49 | action='append', |
50 | 50 | metavar=('uid', 'name'), |
51 | - help='add new user') | |
51 | + help='add new user id and name') | |
52 | 52 | |
53 | 53 | parser.add_argument('-u', '--update', |
54 | 54 | nargs='+', |
55 | 55 | metavar='uid', |
56 | 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 | 63 | parser.add_argument('--pw', |
60 | 64 | default=None, |
61 | 65 | type=str, |
62 | - help='set password for new and updated users') | |
66 | + help='password for new or updated users') | |
63 | 67 | |
64 | 68 | parser.add_argument('-V', '--verbose', |
65 | 69 | action='store_true', |
... | ... | @@ -152,45 +156,55 @@ def main(): |
152 | 156 | |
153 | 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 | 169 | if args.admin: |
163 | 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 | 177 | if args.add: |
167 | 178 | for uid, name in args.add: |
168 | 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 | 184 | print('Generating password hashes', end='') |
174 | 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 | 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 | 208 | session.commit() |
195 | 209 | |
196 | 210 | show_students_in_database(session, args.verbose) | ... | ... |
perguntations/serve.py
... | ... | @@ -50,7 +50,7 @@ class WebApplication(tornado.web.Application): |
50 | 50 | |
51 | 51 | settings = { |
52 | 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 | 54 | 'static_url_prefix': '/static/', |
55 | 55 | 'xsrf_cookies': True, |
56 | 56 | 'cookie_secret': base64.b64encode(uuid.uuid4().bytes), |
... | ... | @@ -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 | 214 | # pylint: disable=abstract-method |
215 | 215 | class RootHandler(BaseHandler): |
... | ... | @@ -298,7 +298,7 @@ class RootHandler(BaseHandler): |
298 | 298 | |
299 | 299 | # show final grade and grades of other tests in the database |
300 | 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 | 303 | self.render('grade.html', t=test) |
304 | 304 | self.clear_cookie('perguntations_user') | ... | ... |