Commit fecc60f6e87a2ce27853f4bbf674b115a9063f5d

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

- initdb_from_csv.py now uses bcrypt instead of sha512, and argparse instead of optparse.

BUGS.md
1 1  
2 2 # BUGS
3 3  
  4 +- MathJax safe mode?? (vi referencia a isto no ipython3.2 whats new)
4 5 - alunos vêm nota final arredondada às decimas, mas é apenas um arredondamento visual. Pode acontecer o aluno chumbar, mas ver uma nota positiva (e.g. 9.46 mostra 9.5 e presume que esta aprovado). Mostrar 3 casas?
5 6 - alunos podem entrar duas vezes em simultaneo. impedir, e permitir ao docente fazer kick-out
6 7 - detectar se falta 'correct' nas perguntas.
... ...
MANUAL.md
... ... @@ -8,6 +8,7 @@ Install python 3.4 and the following packages from pip:
8 8 - Mako (1.0.1)
9 9 - Markdown (2.6.2)
10 10 - PyYAML (3.11)
  11 +- bcrypt (2.0.0)
11 12  
12 13 Before using the program you need to
13 14  
... ...
README.md
... ... @@ -10,6 +10,7 @@ Installed using `pip3`:
10 10 - Mako (1.0.1)
11 11 - Markdown (2.6.2)
12 12 - PyYAML (3.11)
  13 +- bcrypt (2.0.0)
13 14  
14 15 ### System setup:
15 16  
... ...
initdb_from_csv.py
... ... @@ -3,9 +3,11 @@
3 3  
4 4 import sqlite3
5 5 import csv
6   -from optparse import OptionParser
7   -from hashlib import sha256
8   -import os.path
  6 +# from optparse import OptionParser
  7 +import argparse
  8 +# from hashlib import sha512
  9 +import bcrypt
  10 +import os
9 11 import sys
10 12 import string
11 13 import re
... ... @@ -16,29 +18,50 @@ def fixname(s):
16 18  
17 19  
18 20 # --------- Parse command line options -----------
19   -parser = OptionParser('usage: %prog [options] inputfile.csv')
  21 +argparser = argparse.ArgumentParser(description='Create new database from a CSV file (SIIUE format)')
  22 +argparser.add_argument('--db', default='students.db', type=str, help='database filename')
  23 +argparser.add_argument('--pw', default='', type=str, help='initial password')
  24 +argparser.add_argument('csvfile', type=str, help='CSV filename')
  25 +args = argparser.parse_args()
20 26  
21   -parser.add_option('--db', dest='db_filename', default='students.db',
22   - help='database filename to create [default: %default]')
  27 +# parser = OptionParser('usage: %prog [options] inputfile.csv')
23 28  
24   -parser.add_option('--pw', dest='password', default='',
25   - help='initial password [default: %default]')
  29 +# parser.add_option('--db', dest='db_filename', default='students.db',
  30 +# help='database filename to create [default: %default]')
26 31  
27   -(options, args) = parser.parse_args()
  32 +# parser.add_option('--pw', dest='password', default='',
  33 +# help='initial password [default: %default]')
28 34  
29   -if len(args) != 1:
30   - print('You must specify a CSV file to import.\nUse option -h for help.')
31   - sys.exit()
  35 +# (options, args) = parser.parse_args()
32 36  
33   -# terminate if db_filename exist
34   -if os.path.exists(options.db_filename):
35   - print('Database already exists. Please use a different name.')
36   - sys.exit()
  37 +# if len(args) != 1:
  38 +# print('You must specify a CSV file to import.\nUse option -h for help.')
  39 +# sys.exit()
37 40  
38   -# -------- Create database ------------
39   -conn = sqlite3.connect(options.db_filename)
40   -c = conn.cursor()
41 41  
  42 +if os.path.exists(args.db):
  43 + print('Database already exists. Aborting...')
  44 + sys.exit(0)
  45 +
  46 +# -------- Parse CSV --------
  47 +with open(args.csvfile, encoding='iso-8859-1') as csvfile: # SIIUE format
  48 + csvreader = csv.reader(csvfile, delimiter=';', quotechar='"')
  49 + next(csvreader) # ignore header
  50 +
  51 + rows = []
  52 + for r in csvreader:
  53 + number = r[0]
  54 + name = fixname(r[1])
  55 + password = bcrypt.hashpw(args.pw.encode('utf-8'), bcrypt.gensalt())
  56 + rows.append((number, name, password))
  57 +
  58 + # add professor (superuser)
  59 + number = '0'
  60 + name = 'Professor'
  61 + password = bcrypt.hashpw(args.pw.encode('utf-8'), bcrypt.gensalt())
  62 + rows.append((number, name, password))
  63 +
  64 +# ---- CREATE DATABASE AND INSERT DATA ----
42 65 sql_cmd = '''PRAGMA foreign_keys = ON;
43 66 CREATE TABLE students (
44 67 number TEXT PRIMARY KEY,
... ... @@ -61,20 +84,7 @@ sql_cmd = '''PRAGMA foreign_keys = ON;
61 84 time TEXT,
62 85 FOREIGN KEY(student_id) REFERENCES students(number)
63 86 );'''
64   -c.executescript(sql_cmd)
65   -
66   -# -------- Parse CSV and insert into database --------
67   -
68   -password = options.password # initial common password for all students
69   -if password != '':
70   - password = sha256(password.encode('utf-8')).hexdigest()
71   -
72   -with open(args[0], encoding='iso-8859-1') as csvfile: # SIIUE format
73   - csvreader = csv.reader(csvfile, delimiter=';', quotechar='"')
74   - next(csvreader) # ignore header
75 87  
76   - c.executemany('INSERT INTO students VALUES (?,?,?)',
77   - [(row[0], fixname(row[1]), password) for row in csvreader])
78   - c.execute('INSERT INTO students VALUES ("0", "Professor", "")')
79   - conn.commit() # commit DB changes
80   - c.close() # close DB cursor
  88 +with sqlite3.connect(args.db) as c:
  89 + c.executescript(sql_cmd)
  90 + c.executemany('INSERT INTO students VALUES (?,?,?)', rows)
... ...