|
@@ -0,0 +1,124 @@
|
|
|
+
|
|
|
+"""
|
|
|
+Authentication module
|
|
|
+"""
|
|
|
+
|
|
|
+import os
|
|
|
+
|
|
|
+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 import db
|
|
|
+
|
|
|
+
|
|
|
+BLUEPRINT = Blueprint(
|
|
|
+ 'vote',
|
|
|
+ __name__,
|
|
|
+ template_folder='templates'
|
|
|
+)
|
|
|
+
|
|
|
+@register_menu(BLUEPRINT, 'vote', 'Vote')
|
|
|
+@login_required
|
|
|
+@BLUEPRINT.route("/")
|
|
|
+def main():
|
|
|
+ """Ballots overview"""
|
|
|
+ ballots = Ballot.query.all()
|
|
|
+
|
|
|
+ return render_template(
|
|
|
+ 'main.j2',
|
|
|
+ ballots=ballots,
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+@BLUEPRINT.route('/create', methods=["GET", "POST"])
|
|
|
+@login_required
|
|
|
+def create():
|
|
|
+ """Creating ballot"""
|
|
|
+ if request.method == 'POST':
|
|
|
+ ballot = Ballot()
|
|
|
+ print(request.form)
|
|
|
+ ballot.name = request.form['name']
|
|
|
+ ballot.description = request.form['description']
|
|
|
+ ballot.user_id = current_user.id
|
|
|
+
|
|
|
+ start_at = "%s %s" % (request.form['start_at_date'], request.form['start_at_time'])
|
|
|
+ ballot.start_at = datetime.strptime(start_at, "%Y-%m-%d %H:%M")
|
|
|
+ end_at = "%s %s" % (request.form['end_at_date'], request.form['end_at_time'])
|
|
|
+ ballot.end_at = datetime.strptime(end_at, "%Y-%m-%d %H:%M")
|
|
|
+
|
|
|
+ db.session.add(ballot)
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
+ flash('Page "%s" successfully created' % ballot.name, 'success')
|
|
|
+ return redirect(url_for('vote.view', ballot_id=ballot.id))
|
|
|
+
|
|
|
+ priorities = Priority.query.all()
|
|
|
+
|
|
|
+ return render_template(
|
|
|
+ 'vote/create.j2',
|
|
|
+ priorities=priorities
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+@BLUEPRINT.route('/<int:ballot_id>', methods=["GET", "POST"])
|
|
|
+@login_required
|
|
|
+def view(ballot_id):
|
|
|
+ """View ballot"""
|
|
|
+ ballot = Ballot.query.get(ballot_id)
|
|
|
+ if request.method == 'POST':
|
|
|
+ option = Option()
|
|
|
+ option.question_id = request.form['question_id']
|
|
|
+ option.name = request.form['name']
|
|
|
+
|
|
|
+ db.session.add(option)
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
+ return render_template(
|
|
|
+ 'vote/view.j2',
|
|
|
+ ballot=ballot,
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+@BLUEPRINT.route('/<int:ballot_id>/add_question', methods=["GET", "POST"])
|
|
|
+@login_required
|
|
|
+def add_question(ballot_id):
|
|
|
+ """Add question to ballot"""
|
|
|
+ ballot = Ballot.query.get(ballot_id)
|
|
|
+ if request.method == 'POST':
|
|
|
+ question = Question()
|
|
|
+ question.ballot_id = ballot.id
|
|
|
+ question.name = request.form['name']
|
|
|
+ question.description = request.form['description']
|
|
|
+ question.combined_approval_voting = 'combined_approval_voting' in request.form
|
|
|
+
|
|
|
+ db.session.add(question)
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
+ if question.combined_approval_voting:
|
|
|
+ options = ['Voor', 'Tegen', 'Onthouden']
|
|
|
+ for option_name in options:
|
|
|
+ option = Option()
|
|
|
+ option.question_id = question.id
|
|
|
+ option.name = option_name
|
|
|
+
|
|
|
+ db.session.add(option)
|
|
|
+
|
|
|
+ db.session.commit()
|
|
|
+ return redirect(url_for('vote.view', ballot_id=ballot.id))
|
|
|
+
|
|
|
+ return render_template(
|
|
|
+ 'vote/add_question.j2',
|
|
|
+ ballot=ballot,
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+@BLUEPRINT.route('/public/<int:ballot_id>', methods=["GET", "POST"])
|
|
|
+def public(ballot_id):
|
|
|
+ """Vote and view results of ballot"""
|
|
|
+ ballot = Ballot.query.get(ballot_id)
|
|
|
+ return render_template(
|
|
|
+ 'vote/public.j2',
|
|
|
+ ballot=ballot,
|
|
|
+ )
|