diff --git a/app/forms/poll.py b/app/forms/poll.py index 988b598..4a003ff 100644 --- a/app/forms/poll.py +++ b/app/forms/poll.py @@ -1,22 +1,38 @@ from flask_wtf import FlaskForm from wtforms import StringField, SubmitField, TextAreaField from wtforms.fields.html5 import DateTimeField -from wtforms.validators import InputRequired +from wtforms.validators import InputRequired, Optional +from datetime import datetime, timedelta -class FPollForm(FlaskForm): +class PollForm(FlaskForm): title = StringField( 'Question', validators=[ InputRequired(), - ], + ] ) - choices = PasswordField( + choices = TextAreaField( 'Choix (un par ligne)', validators=[ InputRequired(), - ], + # TODO: add a validator to check if there is at least one choice + ] + ) + start = DateTimeField( + 'Début', + default=datetime.now(), + validators=[ + Optional() + ] + ) + end = DateTimeField( + 'Fin', + default=datetime.now() + timedelta(days=1), + validators=[ + Optional() + ] ) submit = SubmitField( - 'Créer le sondage', + 'Créer le sondage' ) diff --git a/app/routes/__init__.py b/app/routes/__init__.py index c8d40ae..28a675d 100644 --- a/app/routes/__init__.py +++ b/app/routes/__init__.py @@ -1,11 +1,11 @@ # Register routes here from app.routes import index, search, users, tools, development -from app.routes.account import login, account, notification +from app.routes.account import login, account, notification, polls from app.routes.admin import index, groups, account, trophies, forums, \ attachments, config, members from app.routes.forum import index, topic -from app.routes.polls import create, vote +from app.routes.polls import vote from app.routes.posts import edit from app.routes.programs import index from app.routes.api import markdown diff --git a/app/routes/account/polls.py b/app/routes/account/polls.py new file mode 100644 index 0000000..665566f --- /dev/null +++ b/app/routes/account/polls.py @@ -0,0 +1,23 @@ +from app import app, db +from flask import abort, flash, redirect, request, url_for +from flask_login import current_user + +from app.models.polls.simple import SimplePoll +from app.forms.poll import PollForm +from app.utils.render import render + +@app.route("/compte/sondages", methods=['GET', 'POST']) +def account_polls(): + form = PollForm() + polls = current_user.polls + + if form.validate_on_submit(): + choices = list(filter(None, form.choices.data.split('\n'))) + p = SimplePoll(current_user, form.title.data, choices, + start=form.start.data, end=form.end.data) + db.session.add(p) + db.session.commit() + + flash(f"Le sondage {p.id} a été créé", "info") + + return render("account/polls.html", polls=polls, form=form) diff --git a/app/routes/polls/create.py b/app/routes/polls/create.py deleted file mode 100644 index 4bc7c22..0000000 --- a/app/routes/polls/create.py +++ /dev/null @@ -1,9 +0,0 @@ -from app import app, db -from flask import abort, redirect, request, url_for -from flask_login import current_user - -from app.models.poll import Poll - -@app.route("/poll/new", methods=['GET', 'POST']) -def poll_create(poll_id): - return redirect(url_for('index')) diff --git a/app/routes/polls/vote.py b/app/routes/polls/vote.py index 08a7911..7079bdb 100644 --- a/app/routes/polls/vote.py +++ b/app/routes/polls/vote.py @@ -4,19 +4,26 @@ from flask_login import current_user from app.models.poll import Poll -@app.route("/poll//vote", methods=['POST']) +@app.route("/sondages//voter", methods=['POST']) def poll_vote(poll_id): - poll = Poll.query.first_or_404(poll_id) + poll = Poll.query.get(poll_id) + if poll is None: + abort(404) if not current_user.is_authenticated: + flash("Seuls les membres connectés peuvent voter", 'error') abort(401) if not poll.can_vote(current_user): + flash("Vous n'avez pas le droit de voter", 'error') abort(403) if poll.has_voted(current_user): + flash("Vous avez déjà voté", 'error') abort(403) if not poll.started: + flash("Le sondage n'a pas débuté", 'error') abort(403) if poll.ended: + flash("Le sondage est terminé", 'error') abort(403) answer = poll.vote(current_user, request) diff --git a/app/templates/account/polls.html b/app/templates/account/polls.html new file mode 100644 index 0000000..bac4c4a --- /dev/null +++ b/app/templates/account/polls.html @@ -0,0 +1,53 @@ +{% extends "base/base.html" %} +{% import "widgets/poll.html" as poll_widget with context %} + +{% block title %} +

Gestion des sondages

+{% endblock %} + +{% block content %} +
+

Créer un sondage

+
+ {{ form.hidden_tag() }} +
+ {{ form.title.label }}
+ {{ form.title(size=32) }}
+ {% for error in form.title.errors %} + {{ error }} + {% endfor %} +
+
+ {{ form.choices.label }} + + {% for error in form.choices.errors %} + {{ error }} + {% endfor %} +
+
+ {{ form.start.label }} + {{ form.start() }} + {% for error in form.start.errors %} + {{ error }} + {% endfor %} +
+
+ {{ form.end.label }} + {{ form.end() }} + {% for error in form.end.errors %} + {{ error }} + {% endfor %} +
+
{{ form.submit(class_="bg-ok") }}
+
+
+ +
+

Mes sondages

+
+ {% for p in polls %} + {{ poll_widget.wpoll(p) }} + {% endfor %} +
+
+{% endblock %} diff --git a/app/templates/base/navbar/account.html b/app/templates/base/navbar/account.html index 000e0a7..de139f7 100644 --- a/app/templates/base/navbar/account.html +++ b/app/templates/base/navbar/account.html @@ -11,6 +11,11 @@ Notifications{{ " ({})".format(current_user.notifications|length) if current_user.notifications|length }} + + + + Sondages +