From 834adb0745edd0562096caab6ed9aabc05dee1fc Mon Sep 17 00:00:00 2001 From: Miguel Barão Date: Mon, 15 May 2017 17:59:40 +0100 Subject: [PATCH] - changed initdb.py default password to be equal to the student id --- README.md | 4 ++-- initdb.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------- 2 files changed, 51 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index a33bdb1..5f4ecfc 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,11 @@ We will need to install python3.6 with sqlite3 support. This can be done using the system package management, downloaded from [http://www.python.org](), or compiled from sources. -- Installing from the system package management: +- Installing from the system package manager: - OSX: `port install python36` - FreeBSD: `pkg install python36 py36-sqlite3` - Linux: `apt-get install ???` (not available yet?) -- Installing from sources: +- Installing from source: - Download from [http://www.python.org]() - `unxz Python-3.6.tar.xz` - `tar xvf Python-3.6.tar` diff --git a/initdb.py b/initdb.py index e916cc2..21a308b 100755 --- a/initdb.py +++ b/initdb.py @@ -4,13 +4,13 @@ import csv import argparse import re import string -import sys +from sys import exit import bcrypt from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -from models import Base, Student #, Answer +from models import Base, Student # SIIUE names have alien strings like "(TE)" and are sometimes capitalized # We remove them so that students dont keep asking what it means @@ -26,62 +26,65 @@ argparser.add_argument('--pw', default='', type=str, help='default password') argparser.add_argument('csvfile', nargs='?', type=str, default='', help='CSV filename') args = argparser.parse_args() -# =========================================================================== -hashed_pw = bcrypt.hashpw(args.pw.encode('utf-8'), bcrypt.gensalt()) - -engine = create_engine('sqlite:///{}'.format(args.db), echo=False) +# =======================================================x==================== +engine = create_engine(f'sqlite:///{args.db}', echo=False) Base.metadata.create_all(engine) # Criate schema if needed Session = sessionmaker(bind=engine) -# --- start session --- -try: - session = Session() +if args.csvfile: + # add students from csv file if available + try: + csvreader = csv.DictReader(open(args.csvfile, encoding='iso-8859-1'), delimiter=';', quotechar='"', skipinitialspace=True) + except EnvironmentError: + print(f'Error: CSV file "{args.csvfile}" not found.') + exit(1) - # add administrator - session.add(Student(id='0', name='Professor', password=hashed_pw)) - - # add students - if args.csvfile: - # from csv file if available - try: - csvreader = csv.DictReader(open(args.csvfile, encoding='iso-8859-1'), delimiter=';', quotechar='"', skipinitialspace=True) - except EnvironmentError: - print('Error: CSV file "{0}" not found.'.format(args.csvfile)) - session.rollback() - sys.exit(1) - else: - session.add_all([Student(id=r['N.º'], name=fix(r['Nome']), password=hashed_pw) for r in csvreader]) - elif args.demo: - # add a few fake students - fakes = [ - ['1915', 'Alan Turing'], - ['1938', 'Donald Knuth'], - ['1815', 'Ada Lovelace'], - ['1969', 'Linus Torvalds'], - ['1955', 'Tim Burners-Lee'], - ['1916', 'Claude Shannon'], - ['1903', 'John von Neumann'], - ] - session.add_all([Student(id=i, name=name, password=hashed_pw) for i,name in fakes]) + students = {s['N.º']: fix(s['Nome']) for s in csvreader} - session.commit() +elif args.demo: + # add a few fake students + students = { + '1915': 'Alan Turing', + '1938': 'Donald Knuth', + '1815': 'Ada Lovelace', + '1969': 'Linus Torvalds', + '1955': 'Tim Burners-Lee', + '1916': 'Claude Shannon', + '1903': 'John von Neumann', + } -except Exception: - print('Error: Database already exists.') - session.rollback() - sys.exit(1) +# add administrator +students['0'] = 'Professor' + + +print('Generating bcrypt password hashes takes time. Please be patient.') +try: + # --- start db session --- + session = Session() + + for num, name in students.items(): + print('.', end='', flush=True) + pw = (args.pw or num).encode('utf-8') + session.add(Student(id=num, name=name, password=bcrypt.hashpw(pw, bcrypt.gensalt()))) + print() -else: n = session.query(Student).count() - print('New database created: {0}\n{1} user(s) inserted:'.format(args.db, n)) + print(f'New database created: {args.db}\n{n} user(s) inserted:') users = session.query(Student).order_by(Student.id).all() - print(' {0:8} - {1} (administrator)'.format(users[0].id, users[0].name)) + print(f' {users[0].id:8} - {users[0].name} (administrator)') if n > 1: - print(' {0:8} - {1}'.format(users[1].id, users[1].name)) + print(f' {users[1].id:8} - {users[1].name}') if n > 3: print(' ... ...') if n > 2: - print(' {0:8} - {1}'.format(users[-1].id, users[-1].name)) + print(f' {users[-1].id:8} - {users[-1].name}') -# --- end session --- +except Exception as e: + print(f'Error: Database "{args.db}" already exists?') + session.rollback() + exit(1) + +else: + # --- end session --- + session.commit() -- libgit2 0.21.2