Commit 683a1709d152ad305358eb04ac78222795ab239a
1 parent
9b8add7e
Exists in
master
and in
1 other branch
- more database stuff.
- list of students and password reset/definition in /students is now working.
Showing
8 changed files
with
42 additions
and
20 deletions
Show diff stats
conf/server.conf
database.py
1 | 1 | ||
2 | import sqlite3 | 2 | import sqlite3 |
3 | +from hashlib import sha256 | ||
3 | 4 | ||
4 | class Database(object): | 5 | class Database(object): |
5 | def __init__(self, db): | 6 | def __init__(self, db): |
@@ -24,3 +25,18 @@ class Database(object): | @@ -24,3 +25,18 @@ class Database(object): | ||
24 | students = c.execute('SELECT number,name,password FROM students ORDER BY number ASC;') | 25 | students = c.execute('SELECT number,name,password FROM students ORDER BY number ASC;') |
25 | return students.fetchall() | 26 | return students.fetchall() |
26 | 27 | ||
28 | + def student_reset_pw(self, d): | ||
29 | + # d = {'12345': 'mypassword', ...} | ||
30 | + with sqlite3.connect(self.db) as c: | ||
31 | + for num, pw in d.items(): | ||
32 | + if pw != '': | ||
33 | + pw = sha256(pw.encode('utf-8')).hexdigest() | ||
34 | + cmd = 'UPDATE students SET password=? WHERE number=?' | ||
35 | + c.execute(cmd, (pw, num)) | ||
36 | + | ||
37 | + def student_add(self, number, name, password=''): # FIXME testar... | ||
38 | + with sqlite3.connect(self.db) as c: | ||
39 | + if password != '': | ||
40 | + password = sha256(password.encode('utf-8')).hexdigest() | ||
41 | + cmd = 'INSERT INTO students VALUES (?, ?, ?);' | ||
42 | + c.execute(cmd, number, name, password) |
serve.py
@@ -6,7 +6,6 @@ | @@ -6,7 +6,6 @@ | ||
6 | 6 | ||
7 | import cherrypy | 7 | import cherrypy |
8 | from mako.lookup import TemplateLookup | 8 | from mako.lookup import TemplateLookup |
9 | -import sqlite3 | ||
10 | import yaml | 9 | import yaml |
11 | import argparse | 10 | import argparse |
12 | from datetime import datetime | 11 | from datetime import datetime |
@@ -38,13 +37,18 @@ class Root(object): | @@ -38,13 +37,18 @@ class Root(object): | ||
38 | 37 | ||
39 | # --- STUDENTS ----------------------------------------------------------- | 38 | # --- STUDENTS ----------------------------------------------------------- |
40 | @cherrypy.expose | 39 | @cherrypy.expose |
41 | - def students(self, reset_pw=None): | ||
42 | - if cherrypy.session.get('userid') != '0': | ||
43 | - raise cherrypy.HTTPRedirect('/') | 40 | + @require() |
41 | + # def students(self, reset_pw=None): | ||
42 | + def students(self, **reset_pw): | ||
43 | + uid = cherrypy.session.get('userid') | ||
44 | + | ||
45 | + if uid != '0': | ||
46 | + raise cherrypy.HTTPRedirect('/') #FIXME use authorization @require(admin) | ||
44 | 47 | ||
45 | - print(reset_pw) | ||
46 | - if reset_pw is not None: | ||
47 | - self.database.reset_pw(reset_pw) | 48 | + if reset_pw: |
49 | + self.database.student_reset_pw(reset_pw) | ||
50 | + for num in reset_pw: | ||
51 | + cherrypy.log.error('Password updated for student %s.' % str(num), 'APPLICATION') | ||
48 | 52 | ||
49 | students = self.database.students() | 53 | students = self.database.students() |
50 | # print(students) | 54 | # print(students) |
static/.DS_Store
No preview for this file type
static/css/.DS_Store
No preview for this file type
static/js/.DS_Store
No preview for this file type
templates/results.html
@@ -47,9 +47,9 @@ | @@ -47,9 +47,9 @@ | ||
47 | <tbody> | 47 | <tbody> |
48 | % for r in results: | 48 | % for r in results: |
49 | <tr> | 49 | <tr> |
50 | - <td>${r[0]}</td> <!-- data --> | ||
51 | - <td>${r[1]}</td> <!-- teste --> | ||
52 | - <td> | 50 | + <td>${r[0]}</td> <!-- numero --> |
51 | + <td>${r[1]}</td> <!-- nome --> | ||
52 | + <td> <!-- nota --> | ||
53 | <div class="progress"> | 53 | <div class="progress"> |
54 | % if r[2] < 10: | 54 | % if r[2] < 10: |
55 | <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="${'{0}'.format(round(r[2]))}" aria-valuemin="0" aria-valuemax="20" style="min-width: 2em; width: ${'{0}'.format(round(5 * r[2]))}%;"> | 55 | <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="${'{0}'.format(round(r[2]))}" aria-valuemin="0" aria-valuemax="20" style="min-width: 2em; width: ${'{0}'.format(round(5 * r[2]))}%;"> |
templates/students.html
@@ -45,7 +45,7 @@ | @@ -45,7 +45,7 @@ | ||
45 | </tr> | 45 | </tr> |
46 | </thead> | 46 | </thead> |
47 | <tbody> | 47 | <tbody> |
48 | - % for r in students: | 48 | + % for r in students: |
49 | % if r[0] in loggedin: | 49 | % if r[0] in loggedin: |
50 | <tr class="success"> | 50 | <tr class="success"> |
51 | % else: | 51 | % else: |
@@ -54,17 +54,19 @@ | @@ -54,17 +54,19 @@ | ||
54 | <td>${r[0]}</td> <!-- numero --> | 54 | <td>${r[0]}</td> <!-- numero --> |
55 | <td>${r[1]}</td> <!-- nome --> | 55 | <td>${r[1]}</td> <!-- nome --> |
56 | <td class="col-sm-6"> | 56 | <td class="col-sm-6"> |
57 | - <form action="/students" method="post"> | ||
58 | - <div class="input-group"> | ||
59 | - <span class="input-group-btn"> | ||
60 | - <button class="btn btn-danger" type="submit">reset</button> | ||
61 | - </span> | ||
62 | - <input type="text" class="form-control" placeholder="${r[2]}" name="number"> | ||
63 | - </div><!-- /input-group --> | 57 | + <form action="/students/" method="post" id="${r[0]}"> |
58 | + <div class="input-group"> | ||
59 | + <span class="input-group-btn"> | ||
60 | + <!-- <button class="btn btn-danger" type="submit">reset</button> --> | ||
61 | + <button form="${r[0]}" type="submit" value="submit" class="btn btn-danger">reset</button> | ||
62 | + | ||
63 | + </span> | ||
64 | + <input type="password" class="form-control" placeholder="${r[2]}" name="${r[0]}"> | ||
65 | + </div><!-- /input-group --> | ||
64 | </form> | 66 | </form> |
65 | </td> <!-- password --> | 67 | </td> <!-- password --> |
66 | </tr> | 68 | </tr> |
67 | - % endfor | 69 | + % endfor |
68 | </tbody> | 70 | </tbody> |
69 | </table> | 71 | </table> |
70 | </div> <!-- panel --> | 72 | </div> <!-- panel --> |