polls: added admin panel and deletion form

pull/88/head
Eldeberen 2 years ago
parent d6512bb197
commit 938516cb3f
Signed by: Darks
GPG Key ID: 7515644268BE1433

@ -1,5 +1,6 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField, SelectField
from wtforms import StringField, SubmitField, TextAreaField, SelectField, \
BooleanField
from wtforms.fields.html5 import DateTimeField
from wtforms.validators import InputRequired, Optional
@ -43,3 +44,15 @@ class PollForm(FlaskForm):
submit = SubmitField(
'Créer le sondage'
)
class DeletePollForm(FlaskForm):
delete = BooleanField(
'Confirmer la suppression',
validators=[
InputRequired(),
],
description='Attention, cette opération est irréversible!'
)
submit = SubmitField(
'Supprimer le sondage'
)

@ -3,9 +3,9 @@
from app.routes import index, search, users, tools, development
from app.routes.account import login, account, notification, polls
from app.routes.admin import index, groups, account, trophies, forums, \
attachments, config, members
attachments, config, members, polls
from app.routes.forum import index, topic
from app.routes.polls import vote
from app.routes.polls import vote, delete
from app.routes.posts import edit
from app.routes.programs import index
from app.routes.api import markdown

@ -1,16 +1,20 @@
from app import app, db
from flask import abort, flash, redirect, request, url_for
from flask_login import current_user
from flask_login import current_user, login_required
from app.models.poll import Poll
from app.models.polls.simple import SimplePoll
from app.models.polls.multiple import MultiplePoll
from app.forms.poll import PollForm
from app.utils.render import render
@app.route("/compte/sondages", methods=['GET', 'POST'])
@login_required
def account_polls():
form = PollForm()
polls = current_user.polls
polls = (Poll.query.filter(Poll.author == current_user)
.order_by(Poll.end.desc()))
polls_types = {
'simplepoll': SimplePoll,
'multiplepoll': MultiplePoll,

@ -0,0 +1,11 @@
from app import app
from app.utils.priv_required import priv_required
from app.utils.render import render
from app.models.poll import Poll
@app.route('/admin/sondages', methods=['GET'])
@priv_required('access-admin-panel')
def adm_polls():
polls = Poll.query.order_by(Poll.end.desc()).all()
return render('admin/polls.html', polls=polls)

@ -0,0 +1,31 @@
from app import app, db
from flask import abort, flash, redirect, request, url_for
from flask_login import current_user
from app.utils.render import render
from app.models.poll import Poll
from app.forms.poll import DeletePollForm
@app.route("/sondages/<int:poll_id>/supprimer", methods=['GET', 'POST'])
def poll_delete(poll_id):
poll = Poll.query.get(poll_id)
if poll is None:
abort(404)
if current_user != poll.author and \
not current_user.priv('delete-posts'):
abort(403)
form = DeletePollForm()
if form.validate_on_submit():
for a in poll.answers:
db.session.delete(a)
db.session.commit()
db.session.delete(poll)
db.session.commit()
flash('Le sondage a été supprimé', 'info')
return redirect(url_for('account_polls'))
return render('poll/delete.html', poll=poll, del_form=form)

@ -54,6 +54,7 @@
<div>
{% for p in polls %}
{{ poll_widget.wpoll(p) }}
<a href="{{ url_for('poll_delete', poll_id=p.id) }}">Supprimer le sondage</a>
{% endfor %}
</div>
</section>

@ -0,0 +1,19 @@
{% extends "base/base.html" %}
{% import "widgets/poll.html" as poll_widget with context %}
{% block title %}
<h1>Gestion des sondages</h1>
{% endblock %}
{% block content %}
<section>
<h1>Tous les sondages</h1>
<div>
{% for p in polls %}
{{ poll_widget.wpoll(p) }}
<span>Auteur : {{ p.author.name }}</span> |
<a href="{{ url_for('poll_delete', poll_id=p.id) }}">Supprimer le sondage</a>
{% endfor %}
</div>
</section>
{% endblock %}

@ -0,0 +1,25 @@
{% extends "base/base.html" %}
{% import "widgets/poll.html" as poll_widget with context %}
{% block title %}
<h1>Supprimer un sondage</h1>
{% endblock %}
{% block content %}
<section>
{{ poll_widget.wpoll(poll) }}
<form action="{{ url_for('poll_delete', poll_id=poll.id) }}" method=post>
{{ del_form.hidden_tag() }}
<div>
{{ del_form.delete.label }}
{{ del_form.delete(checked=False) }}
<div style="font-size: 80%; color: gray">{{ del_form.delete.description }}</div>
{% for error in del_form.delete.errors %}
<span class=msgerror>{{ error }}</span>
{% endfor %}
</div>
<div>{{ del_form.submit(class_="bg-error") }}</div>
</form>
</section>
{% endblock %}
Loading…
Cancel
Save