inline HTML, result viewer

This commit is contained in:
Ville Rantanen
2018-12-04 20:58:24 +02:00
parent 1a8cc70914
commit 2910cc2186
10 changed files with 185 additions and 101 deletions

View File

@@ -38,10 +38,69 @@ def get_voter_table_name(key):
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, )
)
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 `%s` WHERE answered = 'false'"%(
token_table,
)
)
unused_tokens = cur.fetchall()[0][0]
tokens = {
'unused': unused_tokens,
'used': used_tokens,
'total': used_tokens + unused_tokens
}
return tokens
def get_html_summary(key):
# TODO
return ''
def get_summary(db, key):
""" returns summary for a vote event """
questions = []
answers = {}
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`"%(
key,
)
)
for row in cur:
if row[0] not in answers.keys():
questions.append(row[0])
answers[row[0]] = {
'answers': {},
'answer_type': row[2]
}
if row[1] not in answers[row[0]]['answers'].keys():
answers[row[0]]['answers'][row[1]] = 0
answers[row[0]]['answers'][row[1]] += 1
return questions, answers
def has_voted(key, token):
@@ -104,7 +163,7 @@ def parse_form(key):
}
key = secure_filename(key)
try:
current_question = None
current_question = 0
with open(os.path.join(app.config['QUESTIONS'], key + ".txt"), "rt") as fp:
for row in fp:
if row.strip() == "":
@@ -145,9 +204,10 @@ def parse_form(key):
form['questions'].append({
'choices': [],
'multichoices': [],
'index': len(form['questions']) + 1,
'index': current_question + 1,
'name': row.strip().rstrip("_:").rstrip(),
'open_question': row.strip().endswith("___")
'open_question': row.strip().endswith("___"),
'autoformat': not rowsl.startswith("<")
})
return form
except Exception as err: