changed database structure

This commit is contained in:
Ville Rantanen
2018-12-05 10:47:58 +02:00
parent 2910cc2186
commit 59c371cd79
5 changed files with 108 additions and 112 deletions

View File

@@ -11,17 +11,18 @@ import sys
def insert_token(db, name, token):
table_name = get_voter_table_name(name)
cur = db.cursor()
cur.execute("""
INSERT INTO `%s` VALUES (
INSERT INTO tokens (token, question_set, answered) VALUES (
?,
?,
'false'
);
"""%( table_name, ),
""",
(
token,
name
)
)
db.commit()
@@ -31,7 +32,6 @@ def add_token(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)
create_voter_table(db, options.name)
for i in range(options.number):
N = 32
token = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
@@ -42,6 +42,7 @@ def add_token(options):
token
))
def open_db(db):
return sqlite3.connect(db)
@@ -98,22 +99,29 @@ def parse_options(database, questions):
help = "Name of the question set"
)
## clear
parser_clear = subparsers.add_parser('clear', help = "Delete results")
parser_clear.add_argument(
parser_clear_votes = subparsers.add_parser('clear_votes', help = "Delete results")
parser_clear_votes.add_argument(
'--really',
action="store_true",
dest="really",
default = False,
help = "Really delete results for the vote"
)
parser_clear.add_argument(
'--tokens',
action="store_true",
dest="tokens",
default = False,
help = "Delete tokens too"
parser_clear_votes.add_argument(
dest = "name",
help = "Name of the question set"
)
parser_clear.add_argument(
## clear tokens
parser_clear_tokens = subparsers.add_parser('clear_tokens', help = "Delete tokens")
parser_clear_tokens.add_argument(
'--really',
action="store_true",
dest="really",
default = False,
help = "Really delete tokens for the vote"
)
parser_clear_tokens.add_argument(
dest = "name",
help = "Name of the question set"
)
@@ -141,7 +149,7 @@ def summary(options):
questions, answers = get_summary(db, options.name)
tokens = get_token_counts(db, options.name)
if options.tsv:
if hasattr(options, 'tsv') and options.tsv:
out = summary_tsv(questions, answers, tokens)
else:
out = summary_list(questions, answers, tokens)
@@ -203,29 +211,35 @@ def summary_tsv(questions, answers, tokens):
def clear_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, ))
summary(options)
try:
summary(options)
except Exception as err:
print("\nQuestions no longer available")
if not options.really:
print("\nNot really deleting results")
print("\nNot really deleting results. use --really")
sys.exit(0)
db = open_db(options.db)
cur = db.cursor()
cur.execute(
"DROP TABLE IF EXISTS `%s`"%( options.name, )
"DELETE FROM answers WHERE question_set = ?",
( options.name, )
)
db.commit()
print("\nDeleted votes for %s"%( options.name, ))
def clear_tokens(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)
print(get_token_counts(db, options.name))
if not options.really:
print("\nNot really deleting tokens. use --really")
sys.exit(0)
db = open_db(options.db)
cur = db.cursor()
cur.execute(
"DROP TABLE IF EXISTS `%s`"%( get_voter_table_name(options.name,) )
"DELETE FROM tokens WHERE question_set = ?",
( options.name, )
)
db.commit()
print("\nDeleted tokens for %s"%( options.name, ))
@@ -238,8 +252,8 @@ def main(database, questions):
add_token(options)
if options.subparser_name == "summary":
summary(options)
if options.subparser_name == "clear":
if options.subparser_name == "clear_votes":
clear_votes(options)
if options.tokens:
clear_tokens(options)
if options.subparser_name == "clear_tokens":
clear_tokens(options)