support for observers

This commit is contained in:
Ville Rantanen
2018-12-12 22:09:12 +02:00
parent 74cdb27037
commit cef7966ad2
12 changed files with 144 additions and 33 deletions

View File

@@ -10,19 +10,21 @@ import string
import sys
def insert_token(db, name, token):
def insert_token(db, name, token, role):
cur = db.cursor()
cur.execute("""
INSERT INTO tokens (token, question_set, answered) VALUES (
INSERT INTO tokens (token, question_set, answered, role) VALUES (
?,
?,
'false'
'false',
?
);
""",
(
get_hash(token),
name
name,
role
)
)
db.commit()
@@ -35,7 +37,7 @@ def manage_tokens(options):
if options.list:
cur = db.cursor()
cur.execute(
"SELECT token, answered FROM tokens WHERE question_set = ?",
"SELECT token, answered FROM tokens WHERE question_set = ? AND role = 'voter'",
(
options.name,
)
@@ -47,12 +49,17 @@ def manage_tokens(options):
"used" if row[1] == "true" else "unused"
))
return
if options.role == 'voter':
service = 'vote'
if options.role == 'observer':
service = 'observe'
for i in range(options.number):
N = 32
token = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
insert_token(db, options.name, token)
print("%s/vote/%s/%s"%(
insert_token(db, options.name, token, options.role)
print("%s/%s/%s/%s"%(
options.prefix,
service,
options.name,
token
))
@@ -90,11 +97,19 @@ def parse_options(database, questions):
)
parser_token.add_argument(
'--prefix',
action="store",
dest="prefix",
action = "store",
dest = "prefix",
default = "",
help = "Prefix tokens with the server URL to automate emails etc.."
)
parser_token.add_argument(
'--role',
action = "store",
dest = "role",
default = "voter",
choices = ['voter', 'observer'],
help = "Add token for role. observer is a token that can only view the current status of the vote event."
)
parser_token.add_argument(
'--list', '-l',
action="store_true",
@@ -177,6 +192,7 @@ def summary(options):
out = summary_list(questions, answers, tokens)
print(out)
def summary_list(questions, answers, tokens):
s = """# Tokens for this question set:
# used: {used}, unused: {unused}, total: {total}
@@ -266,6 +282,7 @@ def clear_tokens(options):
db.commit()
print("\nDeleted tokens for %s"%( options.name, ))
def main(database, questions):
options = parse_options(database, questions)
if options.subparser_name == "list":