help to print votes and questions
This commit is contained in:
109
manager.py
109
manager.py
@@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from collections import OrderedDict
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from utils import *
|
from utils import *
|
||||||
import argparse
|
import argparse
|
||||||
@@ -123,6 +124,18 @@ def parse_options(database, questions):
|
|||||||
help = "List existing tokens, instead of generating more"
|
help = "List existing tokens, instead of generating more"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
## show question set
|
||||||
|
parser_show = subparsers.add_parser('show', help = "Show a question set")
|
||||||
|
|
||||||
|
## list votes
|
||||||
|
parser_list = subparsers.add_parser('list', help = "List all votes")
|
||||||
|
parser_list.add_argument(
|
||||||
|
'--tsv',
|
||||||
|
action="store_true",
|
||||||
|
dest="tsv",
|
||||||
|
default = False,
|
||||||
|
help = "TSV output"
|
||||||
|
)
|
||||||
|
|
||||||
## summary of vote
|
## summary of vote
|
||||||
parser_summary = subparsers.add_parser('summary', help = "Vote results")
|
parser_summary = subparsers.add_parser('summary', help = "Vote results")
|
||||||
@@ -169,6 +182,61 @@ def list_question_sets(options):
|
|||||||
print(f[0:-4])
|
print(f[0:-4])
|
||||||
|
|
||||||
|
|
||||||
|
def list_votes(options):
|
||||||
|
if not is_key(options.name, options):
|
||||||
|
raise Exception("%s does not exist, or is not a valid question set name"%( options.name, ))
|
||||||
|
db = open_db(options.db)
|
||||||
|
votes = OrderedDict()
|
||||||
|
cur = db.cursor()
|
||||||
|
cur.execute(
|
||||||
|
"SELECT question, answer, answer_type FROM answers WHERE question_set = ?",
|
||||||
|
(
|
||||||
|
options.name,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
for row in cur:
|
||||||
|
if row[0] not in votes.keys():
|
||||||
|
votes[row[0]] = {
|
||||||
|
'answers': [],
|
||||||
|
'answer_type': row[2]
|
||||||
|
}
|
||||||
|
votes[row[0]]['answers'].append(row[1])
|
||||||
|
|
||||||
|
if hasattr(options, 'tsv') and options.tsv:
|
||||||
|
out = votes_tsv(votes)
|
||||||
|
else:
|
||||||
|
out = votes_list(votes)
|
||||||
|
print(out)
|
||||||
|
|
||||||
|
|
||||||
|
def show(options):
|
||||||
|
if not is_key(options.name, options):
|
||||||
|
raise Exception("%s does not exist, or is not a valid question set name"%( options.name, ))
|
||||||
|
form = parse_form(options.name, options.questions, True)
|
||||||
|
|
||||||
|
form['ends'] = time_to('expires', form)
|
||||||
|
form['starts'] = time_to('opens', form)
|
||||||
|
print("""
|
||||||
|
Voting ends at: {ends}
|
||||||
|
Voting starts at: {starts}
|
||||||
|
Style (open/closed): {vote_style}
|
||||||
|
Show results after voting: {show_results}
|
||||||
|
Title: {title}
|
||||||
|
Draft: {draft}
|
||||||
|
""".format_map(form))
|
||||||
|
|
||||||
|
for question in form['questions']:
|
||||||
|
print("\n# {name}".format_map(question))
|
||||||
|
for choice in question['choices']:
|
||||||
|
print("- " + choice)
|
||||||
|
for choice in question['multichoices']:
|
||||||
|
print("+ " + choice)
|
||||||
|
if question['open_question']:
|
||||||
|
print(",----------.")
|
||||||
|
print("| |")
|
||||||
|
print("`----------'")
|
||||||
|
|
||||||
|
|
||||||
def summary(options):
|
def summary(options):
|
||||||
if not is_key(options.name, options):
|
if not is_key(options.name, options):
|
||||||
raise Exception("%s does not exist, or is not a valid question set name"%( options.name, ))
|
raise Exception("%s does not exist, or is not a valid question set name"%( options.name, ))
|
||||||
@@ -238,6 +306,43 @@ def summary_tsv(questions, answers, tokens):
|
|||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
def votes_list(votes):
|
||||||
|
""" votes as list format """
|
||||||
|
s = ""
|
||||||
|
|
||||||
|
for q in votes:
|
||||||
|
s += "\n# %s (%s)\n"%( q, votes[q]['answer_type'])
|
||||||
|
|
||||||
|
for answer in votes[q]['answers']:
|
||||||
|
prefix = ""
|
||||||
|
if votes[q]['answer_type'] == "single":
|
||||||
|
prefix = "- "
|
||||||
|
if votes[q]['answer_type'] == "multiple":
|
||||||
|
prefix = "+ "
|
||||||
|
if votes[q]['answer_type'] == "open":
|
||||||
|
prefix = "----\n> "
|
||||||
|
|
||||||
|
s += "%s%s\n"%(
|
||||||
|
prefix,
|
||||||
|
answer,
|
||||||
|
)
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
def votes_tsv(votes):
|
||||||
|
s = '"Question"\t"Question type"\t"Answer"\n'
|
||||||
|
good_characters = dict.fromkeys(range(32))
|
||||||
|
for q in votes:
|
||||||
|
for answer in votes[q]['answers']:
|
||||||
|
s += '"%s"\t"%s"\t"%s"\n'%(
|
||||||
|
q,
|
||||||
|
votes[q]['answer_type'],
|
||||||
|
answer.translate(good_characters),
|
||||||
|
)
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def clear_votes(options):
|
def clear_votes(options):
|
||||||
try:
|
try:
|
||||||
summary(options)
|
summary(options)
|
||||||
@@ -277,6 +382,10 @@ def main(database, questions):
|
|||||||
options = parse_options(database, questions)
|
options = parse_options(database, questions)
|
||||||
if options.subparser_name == "token":
|
if options.subparser_name == "token":
|
||||||
manage_tokens(options)
|
manage_tokens(options)
|
||||||
|
if options.subparser_name == "show":
|
||||||
|
show(options)
|
||||||
|
if options.subparser_name == "list":
|
||||||
|
list_votes(options)
|
||||||
if options.subparser_name == "summary":
|
if options.subparser_name == "summary":
|
||||||
summary(options)
|
summary(options)
|
||||||
if options.subparser_name == "clear-votes":
|
if options.subparser_name == "clear-votes":
|
||||||
|
|||||||
15
utils.py
15
utils.py
@@ -173,7 +173,7 @@ def is_voter(key, token):
|
|||||||
)
|
)
|
||||||
return len(cur.fetchall()) > 0
|
return len(cur.fetchall()) > 0
|
||||||
|
|
||||||
def parse_form(key):
|
def parse_form(key, questions = None, debug = None):
|
||||||
form = {
|
form = {
|
||||||
'expires': None,
|
'expires': None,
|
||||||
'opens': None,
|
'opens': None,
|
||||||
@@ -188,7 +188,9 @@ def parse_form(key):
|
|||||||
key = secure_filename(key)
|
key = secure_filename(key)
|
||||||
try:
|
try:
|
||||||
current_question = 0
|
current_question = 0
|
||||||
with open(os.path.join(app.config['QUESTIONS'], key + ".txt"), "rt", encoding = "UTF-8") as fp:
|
if questions == None:
|
||||||
|
questions = app.config['QUESTIONS']
|
||||||
|
with open(os.path.join(questions, key + ".txt"), "rt", encoding = "UTF-8") as fp:
|
||||||
for row in fp:
|
for row in fp:
|
||||||
if row.strip() == "":
|
if row.strip() == "":
|
||||||
continue
|
continue
|
||||||
@@ -197,7 +199,10 @@ def parse_form(key):
|
|||||||
continue
|
continue
|
||||||
if rowsl.startswith("title: "):
|
if rowsl.startswith("title: "):
|
||||||
form['title'] = row[6:].strip()
|
form['title'] = row[6:].strip()
|
||||||
g.title = row[6:].strip()
|
try:
|
||||||
|
g.title = row[6:].strip()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
continue
|
continue
|
||||||
if rowsl.startswith("expires: "):
|
if rowsl.startswith("expires: "):
|
||||||
form['expires'] = parse_row_date(row)
|
form['expires'] = parse_row_date(row)
|
||||||
@@ -242,7 +247,9 @@ def parse_form(key):
|
|||||||
})
|
})
|
||||||
return form
|
return form
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
if app.config['DEBUG']:
|
if debug == None:
|
||||||
|
debug = app.config['DEBUG']
|
||||||
|
if debug:
|
||||||
raise err
|
raise err
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user