|
@@ -3,13 +3,14 @@
|
|
|
Authentication module
|
|
|
"""
|
|
|
|
|
|
-import os
|
|
|
+import hashlib
|
|
|
+import hmac
|
|
|
|
|
|
from datetime import datetime
|
|
|
from flask_login import login_required, current_user
|
|
|
from flask_menu import Menu, register_menu
|
|
|
from flask import render_template, request, flash, Blueprint, redirect, url_for
|
|
|
-from app.models import User, Page, Ballot, Priority, Question, Option
|
|
|
+from app.models import User, Page, Ballot, Priority, Question, Option, Vote, Code
|
|
|
from app import db
|
|
|
|
|
|
|
|
@@ -118,6 +119,38 @@ def add_question(ballot_id):
|
|
|
def public(ballot_id):
|
|
|
"""Vote and view results of ballot"""
|
|
|
ballot = Ballot.query.get(ballot_id)
|
|
|
+
|
|
|
+# code = Code()
|
|
|
+# code.secret = "test"
|
|
|
+
|
|
|
+# db.session.add(code)
|
|
|
+# db.session.commit()
|
|
|
+ if request.method == 'POST':
|
|
|
+ security_code = request.form['security_code']
|
|
|
+ code = Code.query.order_by(Code.expire_date.desc()).first()
|
|
|
+ print(code.get_digest(code))
|
|
|
+ user_id = None
|
|
|
+ for user in User.all():
|
|
|
+ if security_code == code.get_digest(str(user.id)):
|
|
|
+ user_id = user.id
|
|
|
+
|
|
|
+ if user_id is not None:
|
|
|
+ for question_id, option_id in request.form.items():
|
|
|
+ if question_id == 'code':
|
|
|
+ continue
|
|
|
+ question = Question.query.get(question_id)
|
|
|
+ option = question.options.filter(Option.id == option_id).first()
|
|
|
+
|
|
|
+ vote = Vote()
|
|
|
+ vote.option_id = option.id
|
|
|
+ vote.user_id = user_id
|
|
|
+ db.session.add(vote)
|
|
|
+
|
|
|
+ db.session.commit()
|
|
|
+ flash('Succesvol gestemd', 'success')
|
|
|
+ else:
|
|
|
+ flash('Fout in veiligheids code', 'warning')
|
|
|
+
|
|
|
return render_template(
|
|
|
'vote/public.j2',
|
|
|
ballot=ballot,
|