Commit 683a1709d152ad305358eb04ac78222795ab239a

Authored by Miguel Barao
1 parent 9b8add7e
Exists in master and in 1 other branch dev

- more database stuff.

- list of students and password reset/definition in /students is now working.
conf/server.conf
1 1 # -*- coding: utf-8 -*-
2 2  
3 3 [global]
4   -environment= 'production'
  4 +;environment= 'production'
5 5  
6 6 ; number of threads running
7 7 server.thread_pool= 10
... ...
database.py
1 1  
2 2 import sqlite3
  3 +from hashlib import sha256
3 4  
4 5 class Database(object):
5 6 def __init__(self, db):
... ... @@ -24,3 +25,18 @@ class Database(object):
24 25 students = c.execute('SELECT number,name,password FROM students ORDER BY number ASC;')
25 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 6  
7 7 import cherrypy
8 8 from mako.lookup import TemplateLookup
9   -import sqlite3
10 9 import yaml
11 10 import argparse
12 11 from datetime import datetime
... ... @@ -38,13 +37,18 @@ class Root(object):
38 37  
39 38 # --- STUDENTS -----------------------------------------------------------
40 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 53 students = self.database.students()
50 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 47 <tbody>
48 48 % for r in results:
49 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 53 <div class="progress">
54 54 % if r[2] < 10:
55 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 45 </tr>
46 46 </thead>
47 47 <tbody>
48   - % for r in students:
  48 + % for r in students:
49 49 % if r[0] in loggedin:
50 50 <tr class="success">
51 51 % else:
... ... @@ -54,17 +54,19 @@
54 54 <td>${r[0]}</td> <!-- numero -->
55 55 <td>${r[1]}</td> <!-- nome -->
56 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 66 </form>
65 67 </td> <!-- password -->
66 68 </tr>
67   - % endfor
  69 + % endfor
68 70 </tbody>
69 71 </table>
70 72 </div> <!-- panel -->
... ...