From 5a885ad9d6523922eaa8ec1d48c436a56e2f72c8 Mon Sep 17 00:00:00 2001 From: Miguel Barão Date: Tue, 6 Jun 2017 17:55:29 +0100 Subject: [PATCH] - added script to insert/update users --- BUGS.md | 4 +++- addstudent.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100755 addstudent.py diff --git a/BUGS.md b/BUGS.md index 88d464b..4c2211e 100644 --- a/BUGS.md +++ b/BUGS.md @@ -1,7 +1,9 @@ BUGS: -- permitir adicionar users. página de admin. se pw vazia, gera pw por defeito. +- cada topico tem uma pagina begin e uma end +- pertuntas tipo tristate: (sim, não, não sei) +- script para adicionar users/reset passwords. - animação no final de cada topico para se perceber a transição - aumentar espaço a seguir às tabelas no texto - guardar state cada vez que topico termina diff --git a/addstudent.py b/addstudent.py new file mode 100755 index 0000000..d330ffe --- /dev/null +++ b/addstudent.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import csv +import argparse +import re +import string +from sys import exit + +import bcrypt +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +from models import Base, Student + +# =========================================================================== +# Parse command line options +# =========================================================================== +argparser = argparse.ArgumentParser(description='Add new student to database or update its password') +argparser.add_argument('--db', default='students.db', type=str, help='database filename') +argparser.add_argument('uid', type=str, default='', help='Student ID') +argparser.add_argument('password', type=str, default='', help='Student password') +argparser.add_argument('name', nargs='*', type=str, default='', help='Student name') +args = argparser.parse_args() + +# =======================================================x==================== +engine = create_engine(f'sqlite:///{args.db}', echo=False) +Base.metadata.create_all(engine) # Criate schema if needed +Session = sessionmaker(bind=engine) + +uid = args.uid +password = bcrypt.hashpw(args.password.encode('utf-8'), bcrypt.gensalt()) +name = ' '.join(args.name) + +# --- start db session --- +session = Session() + +u = session.query(Student).get(uid) + +if u is None: + session.add(Student(id=uid, name=name, password=password)) + print(f'New user added to "{args.db}".') + +else: + print('Student already exists.') + if input(' Update password? [y/n] ') in ('y', 'yes', 'Y', 'Yes', 'YES'): + u.password = password + print(' Password updated.') + else: + print(' Nothing done.') + +session.commit() +session.close() +# --- end db session --- -- libgit2 0.21.2