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
|
||||
|
||||
from collections import OrderedDict
|
||||
from datetime import datetime
|
||||
from utils import *
|
||||
import argparse
|
||||
@@ -123,6 +124,18 @@ def parse_options(database, questions):
|
||||
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
|
||||
parser_summary = subparsers.add_parser('summary', help = "Vote results")
|
||||
@@ -169,6 +182,61 @@ def list_question_sets(options):
|
||||
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):
|
||||
if not is_key(options.name, options):
|
||||
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
|
||||
|
||||
|
||||
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):
|
||||
try:
|
||||
summary(options)
|
||||
@@ -277,6 +382,10 @@ def main(database, questions):
|
||||
options = parse_options(database, questions)
|
||||
if options.subparser_name == "token":
|
||||
manage_tokens(options)
|
||||
if options.subparser_name == "show":
|
||||
show(options)
|
||||
if options.subparser_name == "list":
|
||||
list_votes(options)
|
||||
if options.subparser_name == "summary":
|
||||
summary(options)
|
||||
if options.subparser_name == "clear-votes":
|
||||
|
||||
Reference in New Issue
Block a user