support for observers
This commit is contained in:
36
utils.py
36
utils.py
@@ -1,11 +1,12 @@
|
||||
from datetime import datetime, timezone
|
||||
from flask import current_app as app
|
||||
from flask import g
|
||||
import os
|
||||
from werkzeug.utils import secure_filename
|
||||
import html
|
||||
import sqlite3
|
||||
import hashlib
|
||||
import html
|
||||
import os
|
||||
import sqlite3
|
||||
|
||||
|
||||
def connect_db():
|
||||
return sqlite3.connect(app.config['DATABASE'])
|
||||
@@ -27,27 +28,30 @@ def create_db(db_file):
|
||||
CREATE TABLE IF NOT EXISTS tokens (
|
||||
token TEXT PRIMARY KEY,
|
||||
question_set TEXT,
|
||||
answered BOOLEAN
|
||||
answered BOOLEAN,
|
||||
role TEXT
|
||||
);
|
||||
"""
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
def get_hash(s):
|
||||
return hashlib.sha224(s.encode('utf-8')).hexdigest()
|
||||
|
||||
|
||||
def get_token_counts(db, key):
|
||||
cur = db.cursor()
|
||||
|
||||
cur.execute(
|
||||
"SELECT count(*) FROM tokens WHERE answered = 'true' and question_set = ?",
|
||||
"SELECT count(*) FROM tokens WHERE answered = 'true' AND question_set = ? AND role = 'voter'",
|
||||
(
|
||||
key,
|
||||
)
|
||||
)
|
||||
used_tokens = cur.fetchall()[0][0]
|
||||
cur.execute(
|
||||
"SELECT count(*) FROM tokens WHERE answered = 'false' and question_set = ?",
|
||||
"SELECT count(*) FROM tokens WHERE answered = 'false' AND question_set = ? AND role = 'voter'",
|
||||
(
|
||||
key,
|
||||
)
|
||||
@@ -60,6 +64,7 @@ def get_token_counts(db, key):
|
||||
}
|
||||
return tokens
|
||||
|
||||
|
||||
def get_summary(db, key):
|
||||
""" returns summary for a vote event """
|
||||
questions = []
|
||||
@@ -92,7 +97,21 @@ def has_voted(key, token):
|
||||
return True
|
||||
cur = g.db.cursor()
|
||||
cur.execute(
|
||||
"SELECT token FROM tokens WHERE token = ? AND answered = 'true' AND question_set = ?",
|
||||
"SELECT token FROM tokens WHERE token = ? AND answered = 'true' AND question_set = ? AND role = 'voter'",
|
||||
(
|
||||
get_hash(token),
|
||||
key
|
||||
)
|
||||
)
|
||||
return len(cur.fetchall()) > 0
|
||||
|
||||
|
||||
def is_observer(key, token):
|
||||
if token == None:
|
||||
return False
|
||||
cur = g.db.cursor()
|
||||
cur.execute(
|
||||
"SELECT token FROM tokens WHERE token = ? AND question_set = ? AND role = 'observer'",
|
||||
(
|
||||
get_hash(token),
|
||||
key
|
||||
@@ -214,6 +233,7 @@ def parse_row_date(row):
|
||||
raise err
|
||||
return None
|
||||
|
||||
|
||||
def write_vote(key, token, answers, form):
|
||||
|
||||
cur = g.db.cursor()
|
||||
@@ -253,6 +273,7 @@ def write_vote(key, token, answers, form):
|
||||
)
|
||||
g.db.commit()
|
||||
|
||||
|
||||
def sort_summary(questions, answers):
|
||||
sorted_answer_list = []
|
||||
for q,a in zip(questions, answers):
|
||||
@@ -264,6 +285,7 @@ def sort_summary(questions, answers):
|
||||
sorted_answer_list.append(sorted_answers)
|
||||
return questions, sorted_answer_list
|
||||
|
||||
|
||||
def time_to_expiry(form):
|
||||
if form['expires'] == None:
|
||||
return "Never"
|
||||
|
||||
Reference in New Issue
Block a user