polls: add a panel to manage own polls

This commit is contained in:
Eldeberen 2021-02-20 01:28:08 +01:00
parent 0801b8ec16
commit fabad32955
Signed by untrusted user: Darks
GPG Key ID: 7515644268BE1433
7 changed files with 114 additions and 19 deletions

View File

@ -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'
)

View File

@ -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

View File

@ -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)

View File

@ -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'))

View File

@ -4,19 +4,26 @@ from flask_login import current_user
from app.models.poll import Poll
@app.route("/poll/<int:poll_id>/vote", methods=['POST'])
@app.route("/sondages/<int:poll_id>/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)

View File

@ -0,0 +1,53 @@
{% extends "base/base.html" %}
{% import "widgets/poll.html" as poll_widget with context %}
{% block title %}
<h1>Gestion des sondages</h1>
{% endblock %}
{% block content %}
<section class="form">
<h1>Créer un sondage</h1>
<form action="" method="post">
{{ form.hidden_tag() }}
<div>
{{ form.title.label }}<br>
{{ form.title(size=32) }}<br>
{% for error in form.title.errors %}
<span style="color: red;">{{ error }}</span>
{% endfor %}
</div>
<div>
{{ form.choices.label }}
<textarea id="{{ form.choices.name }}" name="{{ form.choices.name }}"></textarea>
{% for error in form.choices.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>
{{ form.start.label }}
{{ form.start() }}
{% for error in form.start.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>
{{ form.end.label }}
{{ form.end() }}
{% for error in form.end.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>{{ form.submit(class_="bg-ok") }}</div>
</form>
</section>
<section>
<h1>Mes sondages</h1>
<div>
{% for p in polls %}
{{ poll_widget.wpoll(p) }}
{% endfor %}
</div>
</section>
{% endblock %}

View File

@ -11,6 +11,11 @@
<path fill="#ffffff" d="M20,2A2,2 0 0,1 22,4V16A2,2 0 0,1 20,18H6L2,22V4C2,2.89 2.9,2 4,2H20M4,4V17.17L5.17,16H20V4H4M6,7H18V9H6V7M6,11H15V13H6V11Z"></path>
</svg>Notifications{{ " ({})".format(current_user.notifications|length) if current_user.notifications|length }}
</a>
<a href="{{ url_for('account_polls') }}">
<svg viewBox="0 0 24 24">
<path fill="#ffffff" d="M9 17H7V10H9M13 17H11V7H13M17 17H15V13H17M19.5 19.1H4.5V5H19.5M19.5 3H4.5C3.4 3 2.5 3.9 2.5 5V19C2.5 20.1 3.4 21 4.5 21H19.5C20.6 21 21.5 20.1 21.5 19V5C21.5 3.9 20.6 3 19.5 3Z" />
</svg>Sondages
</a>
<a href="#">
<svg viewBox="0 0 24 24">
<path fill="#ffffff" d="M2,2V11C2,12 3,13 4,13H6.2C6.6,15 7.9,16.7 11,17V19.1C8.8,19.3 8,20.4 8,21.7V22H16V21.7C16,20.4 15.2,19.3 13,19.1V17C16.1,16.7 17.4,15 17.8,13H20C21,13 22,12 22,11V2H18C17.1,2 16,3 16,4H8C8,3 6.9,2 6,2H2M4,4H6V6L6,11H4V4M18,4H20V11H18V6L18,4M8,6H16V11.5C16,13.43 15.42,15 12,15C8.59,15 8,13.43 8,11.5V6Z"></path>