changed database structure
This commit is contained in:
111
utils.py
111
utils.py
@@ -3,65 +3,54 @@ from flask import current_app as app
|
||||
from flask import g
|
||||
import os
|
||||
from werkzeug.utils import secure_filename
|
||||
import html
|
||||
import sqlite3
|
||||
|
||||
def create_result_table(key):
|
||||
cur = g.db.cursor()
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS `%s` (
|
||||
question TEXT,
|
||||
answer TEXT,
|
||||
answer_type TEXT
|
||||
);
|
||||
"""%(key, )
|
||||
)
|
||||
g.db.commit()
|
||||
def connect_db():
|
||||
return sqlite3.connect(app.config['DATABASE'])
|
||||
|
||||
|
||||
def create_voter_table(db, name):
|
||||
table_name = get_voter_table_name(name)
|
||||
def create_db(db_file):
|
||||
db = sqlite3.connect(db_file)
|
||||
cur = db.cursor()
|
||||
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS `%s` (
|
||||
token TEXT PRIMARY KEY,
|
||||
answered BOOLEAN
|
||||
CREATE TABLE IF NOT EXISTS answers (
|
||||
question_set TEXT,
|
||||
question TEXT,
|
||||
answer TEXT,
|
||||
answer_type TEXT
|
||||
);
|
||||
"""%(table_name, )
|
||||
""")
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS tokens (
|
||||
token TEXT PRIMARY KEY,
|
||||
question_set TEXT,
|
||||
answered BOOLEAN
|
||||
);
|
||||
"""
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
def get_voter_table_name(key):
|
||||
return key + "__voters"
|
||||
|
||||
|
||||
def get_result_table_name(key):
|
||||
return key
|
||||
|
||||
def get_token_counts(db, key):
|
||||
cur = db.cursor()
|
||||
token_table = get_voter_table_name(key)
|
||||
|
||||
cur.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name=?;",
|
||||
( token_table, )
|
||||
"SELECT count(*) FROM tokens WHERE answered = 'true' and question_set = ?",
|
||||
(
|
||||
key,
|
||||
)
|
||||
)
|
||||
matching_tables = cur.fetchall()
|
||||
if len(matching_tables) == 0:
|
||||
used_tokens = 0
|
||||
unused_tokens = 0
|
||||
else:
|
||||
cur.execute(
|
||||
"SELECT count(*) FROM `%s` WHERE answered = 'true'"%(
|
||||
token_table,
|
||||
)
|
||||
used_tokens = cur.fetchall()[0][0]
|
||||
cur.execute(
|
||||
"SELECT count(*) FROM tokens WHERE answered = 'false' and question_set = ?",
|
||||
(
|
||||
key,
|
||||
)
|
||||
used_tokens = cur.fetchall()[0][0]
|
||||
cur.execute(
|
||||
"SELECT count(*) FROM `%s` WHERE answered = 'false'"%(
|
||||
token_table,
|
||||
)
|
||||
)
|
||||
unused_tokens = cur.fetchall()[0][0]
|
||||
)
|
||||
unused_tokens = cur.fetchall()[0][0]
|
||||
tokens = {
|
||||
'unused': unused_tokens,
|
||||
'used': used_tokens,
|
||||
@@ -76,15 +65,8 @@ def get_summary(db, key):
|
||||
cur = db.cursor()
|
||||
|
||||
cur.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name=?;",
|
||||
( key, )
|
||||
)
|
||||
matching_tables = cur.fetchall()
|
||||
if len(matching_tables) == 0:
|
||||
return questions, answers
|
||||
|
||||
cur.execute(
|
||||
"SELECT question, answer, answer_type FROM `%s`"%(
|
||||
"SELECT question, answer, answer_type FROM answers WHERE question_set = ?",
|
||||
(
|
||||
key,
|
||||
)
|
||||
)
|
||||
@@ -108,11 +90,10 @@ def has_voted(key, token):
|
||||
return True
|
||||
cur = g.db.cursor()
|
||||
cur.execute(
|
||||
"SELECT token FROM %s WHERE token = ? AND answered = 'true'"%(
|
||||
get_voter_table_name(key),
|
||||
),
|
||||
"SELECT token FROM tokens WHERE token = ? AND answered = 'true' AND question_set = ?",
|
||||
(
|
||||
token,
|
||||
key
|
||||
)
|
||||
)
|
||||
return len(cur.fetchall()) > 0
|
||||
@@ -252,26 +233,34 @@ def write_vote(key, token, answers, form):
|
||||
continue
|
||||
for single in answer:
|
||||
cur.execute(
|
||||
"INSERT INTO `%s` VALUES (?, ?, ?)"%(
|
||||
key,
|
||||
),
|
||||
"INSERT INTO answers (question_set, question, answer, answer_type) VALUES (?, ?, ?, ?)",
|
||||
(
|
||||
key,
|
||||
question['name'],
|
||||
single.strip(),
|
||||
html.escape(single).strip(),
|
||||
answer_type
|
||||
)
|
||||
)
|
||||
if is_closed_vote(form):
|
||||
cur.execute(
|
||||
"UPDATE %s SET answered = 'true' WHERE token = ?"%(
|
||||
get_voter_table_name(key),
|
||||
),
|
||||
"UPDATE tokens SET answered = 'true' WHERE token = ? AND question_set = ?",
|
||||
(
|
||||
token,
|
||||
key
|
||||
)
|
||||
)
|
||||
g.db.commit()
|
||||
|
||||
def sort_summary(questions, answers):
|
||||
sorted_answer_list = []
|
||||
for q,a in zip(questions, answers):
|
||||
sum_answers = sum([answers[q]['answers'][x] for x in answers[q]['answers']])
|
||||
sorted_answers = sorted(
|
||||
[{"answer_type": answers[q]['answer_type'] , "answer": x, "count": answers[q]['answers'][x], "percent": int(100 * float(answers[q]['answers'][x]) / sum_answers)} for x in answers[q]['answers']],
|
||||
key = lambda i: -i['count']
|
||||
)
|
||||
sorted_answer_list.append(sorted_answers)
|
||||
return questions, sorted_answer_list
|
||||
|
||||
def time_to_expiry(form):
|
||||
if form['expires'] == None:
|
||||
|
||||
Reference in New Issue
Block a user