app.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. """
  2. Authentication module
  3. """
  4. import hashlib
  5. import hmac
  6. from datetime import datetime
  7. from flask_login import login_required, current_user
  8. from flask_menu import Menu, register_menu
  9. from flask import render_template, request, flash, Blueprint, redirect, url_for
  10. from app.models import User, Page, Ballot, Priority, Question, Option, Vote, Code
  11. from app import db
  12. BLUEPRINT = Blueprint(
  13. 'vote',
  14. __name__,
  15. template_folder='templates'
  16. )
  17. @register_menu(BLUEPRINT, 'vote', 'Vote')
  18. @login_required
  19. @BLUEPRINT.route("/")
  20. def main():
  21. """Ballots overview"""
  22. ballots = Ballot.query.all()
  23. return render_template(
  24. 'main.j2',
  25. ballots=ballots,
  26. )
  27. @login_required
  28. @BLUEPRINT.route("/codes")
  29. def codes():
  30. """codes overview"""
  31. code = Code.query.order_by(Code.expire_date.desc()).first()
  32. users = User.query.all()
  33. return render_template(
  34. 'vote/codes.j2',
  35. users=users,
  36. code=code,
  37. )
  38. @BLUEPRINT.route('/create', methods=["GET", "POST"])
  39. @login_required
  40. def create():
  41. """Creating ballot"""
  42. if request.method == 'POST':
  43. ballot = Ballot()
  44. print(request.form)
  45. ballot.name = request.form['name']
  46. ballot.description = request.form['description']
  47. ballot.user_id = current_user.id
  48. start_at = "%s %s" % (request.form['start_at_date'], request.form['start_at_time'])
  49. ballot.start_at = datetime.strptime(start_at, "%Y-%m-%d %H:%M")
  50. end_at = "%s %s" % (request.form['end_at_date'], request.form['end_at_time'])
  51. ballot.end_at = datetime.strptime(end_at, "%Y-%m-%d %H:%M")
  52. db.session.add(ballot)
  53. db.session.commit()
  54. flash('Page "%s" successfully created' % ballot.name, 'success')
  55. return redirect(url_for('vote.view', ballot_id=ballot.id))
  56. priorities = Priority.query.all()
  57. return render_template(
  58. 'vote/create.j2',
  59. priorities=priorities
  60. )
  61. @BLUEPRINT.route('/<int:ballot_id>', methods=["GET", "POST"])
  62. @login_required
  63. def view(ballot_id):
  64. """View ballot"""
  65. ballot = Ballot.query.get(ballot_id)
  66. if request.method == 'POST':
  67. option = Option()
  68. option.question_id = request.form['question_id']
  69. option.name = request.form['name']
  70. db.session.add(option)
  71. db.session.commit()
  72. return render_template(
  73. 'vote/view.j2',
  74. ballot=ballot,
  75. )
  76. @BLUEPRINT.route('/<int:ballot_id>/add_question', methods=["GET", "POST"])
  77. @login_required
  78. def add_question(ballot_id):
  79. """Add question to ballot"""
  80. ballot = Ballot.query.get(ballot_id)
  81. if request.method == 'POST':
  82. question = Question()
  83. question.ballot_id = ballot.id
  84. question.name = request.form['name']
  85. question.description = request.form['description']
  86. question.combined_approval_voting = 'combined_approval_voting' in request.form
  87. db.session.add(question)
  88. db.session.commit()
  89. if question.combined_approval_voting:
  90. options = ['Voor', 'Tegen', 'Onthouden']
  91. for option_name in options:
  92. option = Option()
  93. option.question_id = question.id
  94. option.name = option_name
  95. db.session.add(option)
  96. db.session.commit()
  97. return redirect(url_for('vote.view', ballot_id=ballot.id))
  98. return render_template(
  99. 'vote/add_question.j2',
  100. ballot=ballot,
  101. )
  102. @BLUEPRINT.route('/public/<int:ballot_id>', methods=["GET", "POST"])
  103. def public(ballot_id):
  104. """Vote and view results of ballot"""
  105. ballot = Ballot.query.get(ballot_id)
  106. if request.method == 'POST':
  107. security_code = request.form['security_code']
  108. code = Code.query.order_by(Code.expire_date.desc()).first()
  109. user_id = None
  110. for user in User.query.all():
  111. if security_code == code.get_digest(user.id):
  112. user_id = user.id
  113. if user_id is not None:
  114. for question_id, option_id in request.form.items():
  115. if question_id == 'security_code':
  116. continue
  117. question = Question.query.get(question_id)
  118. for option in question.options:
  119. if option.votes.filter(Vote.user_id == user_id).first():
  120. flash('Je hebt al gestemd.', 'warning')
  121. return redirect(url_for('vote.public', ballot_id=ballot.id))
  122. option = question.options.filter(Option.id == option_id).first()
  123. vote = Vote()
  124. vote.option_id = option.id
  125. vote.user_id = user_id
  126. db.session.add(vote)
  127. db.session.commit()
  128. flash('Succesvol gestemd.', 'success')
  129. else:
  130. flash('Fout in veiligheids code.', 'warning')
  131. return render_template(
  132. 'vote/public.j2',
  133. ballot=ballot,
  134. )