open voting, without tokens
This commit is contained in:
57
utils.py
57
utils.py
@@ -16,6 +16,7 @@ def create_result_table(key):
|
||||
)
|
||||
g.db.commit()
|
||||
|
||||
|
||||
def create_voter_table(db, name):
|
||||
table_name = get_voter_table_name(name)
|
||||
cur = db.cursor()
|
||||
@@ -29,6 +30,7 @@ def create_voter_table(db, name):
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
def get_voter_table_name(key):
|
||||
return key + "__voters"
|
||||
|
||||
@@ -37,7 +39,14 @@ def get_result_table_name(key):
|
||||
return key
|
||||
|
||||
|
||||
def get_html_summary(key):
|
||||
# TODO
|
||||
return ''
|
||||
|
||||
|
||||
def has_voted(key, token):
|
||||
if token == None:
|
||||
return True
|
||||
cur = g.db.cursor()
|
||||
cur.execute(
|
||||
"SELECT token FROM %s WHERE token = ? AND answered = 'true'"%(
|
||||
@@ -50,15 +59,21 @@ def has_voted(key, token):
|
||||
return len(cur.fetchall()) > 0
|
||||
|
||||
|
||||
def is_closed_vote(form):
|
||||
return form['vote_style'] == 'closed'
|
||||
|
||||
|
||||
def is_draft(form):
|
||||
return form['draft']
|
||||
|
||||
|
||||
def is_expired(form):
|
||||
if form['expires'] == None:
|
||||
return False
|
||||
|
||||
return datetime.now(timezone.utc) > form['expires']
|
||||
|
||||
|
||||
def is_key(key, cli_opts = False):
|
||||
key = secure_filename(key)
|
||||
|
||||
@@ -74,10 +89,17 @@ def is_key(key, cli_opts = False):
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def is_show_results(form):
|
||||
return form['show_results']
|
||||
|
||||
|
||||
def parse_form(key):
|
||||
form = {
|
||||
'expires': None,
|
||||
'draft': False,
|
||||
'vote_style': "closed",
|
||||
'show_results': False,
|
||||
'questions': []
|
||||
}
|
||||
key = secure_filename(key)
|
||||
@@ -87,15 +109,24 @@ def parse_form(key):
|
||||
for row in fp:
|
||||
if row.strip() == "":
|
||||
continue
|
||||
rowsl = row.lower().rstrip()
|
||||
if row.startswith("#"):
|
||||
continue
|
||||
if row.lower().startswith("expires: "):
|
||||
if rowsl.startswith("expires: "):
|
||||
form['expires'] = parse_row_date(row)
|
||||
continue
|
||||
if row.lower().startswith("draft: "):
|
||||
if row.lower().rstrip() == "draft: true":
|
||||
if rowsl.startswith("draft: "):
|
||||
if rowsl == "draft: true":
|
||||
form['draft'] = True
|
||||
continue
|
||||
if rowsl.startswith("vote_style: "):
|
||||
if rowsl == "vote_style: open":
|
||||
form['vote_style'] = "open"
|
||||
continue
|
||||
if rowsl.startswith("show_results: "):
|
||||
if rowsl == "show_results: true":
|
||||
form['show_results'] = True
|
||||
continue
|
||||
if row.startswith("- "):
|
||||
if current_question == None:
|
||||
continue
|
||||
@@ -157,9 +188,11 @@ def write_vote(key, token, answers, form):
|
||||
answer_type = "open"
|
||||
if answer == None:
|
||||
continue
|
||||
if answer_type == None:
|
||||
continue
|
||||
for single in answer:
|
||||
cur.execute(
|
||||
"INSERT INTO %s VALUES (?, ?, ?)"%(
|
||||
"INSERT INTO `%s` VALUES (?, ?, ?)"%(
|
||||
key,
|
||||
),
|
||||
(
|
||||
@@ -168,15 +201,15 @@ def write_vote(key, token, answers, form):
|
||||
answer_type
|
||||
)
|
||||
)
|
||||
|
||||
cur.execute(
|
||||
"UPDATE %s SET answered = 'true' WHERE token = ?"%(
|
||||
get_voter_table_name(key),
|
||||
),
|
||||
(
|
||||
token,
|
||||
if is_closed_vote(form):
|
||||
cur.execute(
|
||||
"UPDATE %s SET answered = 'true' WHERE token = ?"%(
|
||||
get_voter_table_name(key),
|
||||
),
|
||||
(
|
||||
token,
|
||||
)
|
||||
)
|
||||
)
|
||||
g.db.commit()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user