Commit 5a885ad9d6523922eaa8ec1d48c436a56e2f72c8

Authored by Miguel Barão
1 parent b6643fc8
Exists in master and in 1 other branch dev

- added script to insert/update users

Showing 2 changed files with 56 additions and 1 deletions   Show diff stats
BUGS.md
1 1  
2 2 BUGS:
3 3  
4   -- permitir adicionar users. página de admin. se pw vazia, gera pw por defeito.
  4 +- cada topico tem uma pagina begin e uma end
  5 +- pertuntas tipo tristate: (sim, não, não sei)
  6 +- script para adicionar users/reset passwords.
5 7 - animação no final de cada topico para se perceber a transição
6 8 - aumentar espaço a seguir às tabelas no texto
7 9 - guardar state cada vez que topico termina
... ...
addstudent.py 0 → 100755
... ... @@ -0,0 +1,53 @@
  1 +#!/usr/bin/env python3
  2 +
  3 +import csv
  4 +import argparse
  5 +import re
  6 +import string
  7 +from sys import exit
  8 +
  9 +import bcrypt
  10 +from sqlalchemy import create_engine
  11 +from sqlalchemy.orm import sessionmaker
  12 +
  13 +from models import Base, Student
  14 +
  15 +# ===========================================================================
  16 +# Parse command line options
  17 +# ===========================================================================
  18 +argparser = argparse.ArgumentParser(description='Add new student to database or update its password')
  19 +argparser.add_argument('--db', default='students.db', type=str, help='database filename')
  20 +argparser.add_argument('uid', type=str, default='', help='Student ID')
  21 +argparser.add_argument('password', type=str, default='', help='Student password')
  22 +argparser.add_argument('name', nargs='*', type=str, default='', help='Student name')
  23 +args = argparser.parse_args()
  24 +
  25 +# =======================================================x====================
  26 +engine = create_engine(f'sqlite:///{args.db}', echo=False)
  27 +Base.metadata.create_all(engine) # Criate schema if needed
  28 +Session = sessionmaker(bind=engine)
  29 +
  30 +uid = args.uid
  31 +password = bcrypt.hashpw(args.password.encode('utf-8'), bcrypt.gensalt())
  32 +name = ' '.join(args.name)
  33 +
  34 +# --- start db session ---
  35 +session = Session()
  36 +
  37 +u = session.query(Student).get(uid)
  38 +
  39 +if u is None:
  40 + session.add(Student(id=uid, name=name, password=password))
  41 + print(f'New user added to "{args.db}".')
  42 +
  43 +else:
  44 + print('Student already exists.')
  45 + if input(' Update password? [y/n] ') in ('y', 'yes', 'Y', 'Yes', 'YES'):
  46 + u.password = password
  47 + print(' Password updated.')
  48 + else:
  49 + print(' Nothing done.')
  50 +
  51 +session.commit()
  52 +session.close()
  53 +# --- end db session ---
... ...