From af1d1884c7a35db1558d10fc992c3c814e97d848 Mon Sep 17 00:00:00 2001 From: Lyanis Souidi Date: Fri, 24 May 2024 13:01:56 +0200 Subject: [PATCH] Template/wtf form pour bug report MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ajout d'un template pour gérer le formulaire et utilisation de WTF form pour la validation des données. --- app/forms/main/create_bug_report.py | 64 ++++++++++++++++++ app/templates/sco_bug_report.j2 | 19 ++++++ app/views/scolar.py | 100 +++++++++------------------- 3 files changed, 116 insertions(+), 67 deletions(-) create mode 100644 app/forms/main/create_bug_report.py create mode 100644 app/templates/sco_bug_report.j2 diff --git a/app/forms/main/create_bug_report.py b/app/forms/main/create_bug_report.py new file mode 100644 index 000000000..5e3541927 --- /dev/null +++ b/app/forms/main/create_bug_report.py @@ -0,0 +1,64 @@ +# -*- mode: python -*- +# -*- coding: utf-8 -*- + +############################################################################## +# +# ScoDoc +# +# Copyright (c) 1999 - 2024 Emmanuel Viennet. All rights reserved. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# Emmanuel Viennet emmanuel.viennet@viennet.net +# +############################################################################## + +""" +Formulaire création de ticket de bug +""" + +from flask_wtf import FlaskForm +from wtforms import SubmitField, validators +from wtforms.fields.simple import StringField, TextAreaField, BooleanField +from app.scodoc import sco_preferences + + +class CreateBugReport(FlaskForm): + """Formulaire permettant la création d'un ticket de bug""" + + title = StringField( + label="Titre du ticket", + validators=[ + validators.DataRequired("titre du ticket requis"), + ], + ) + message = TextAreaField( + label="Message", + id="ticket_message", + validators=[ + validators.DataRequired("message du ticket requis"), + ], + ) + etab = StringField(label="Etablissement") + include_dump = BooleanField( + "Inclure une copie anonymisée de la base de données ? (ces données permettent aux développeur·euse·s de reproduire l'erreur rencontrée)", + default=False, + ) + submit = SubmitField("Envoyer") + cancel = SubmitField("Annuler", render_kw={"formnovalidate": True}) + + def __init__(self, *args, **kwargs): + super(CreateBugReport, self).__init__(*args, **kwargs) + self.etab.data = sco_preferences.get_preference("InstituteName") or "" diff --git a/app/templates/sco_bug_report.j2 b/app/templates/sco_bug_report.j2 new file mode 100644 index 000000000..07bd189c9 --- /dev/null +++ b/app/templates/sco_bug_report.j2 @@ -0,0 +1,19 @@ +{# -*- mode: jinja-html -*- #} +{% extends 'base.j2' %} +{% import 'wtf.j2' as wtf %} + +{% block app_content %} +

Assistance technique

+

+ Ce formulaire permet d'effectuer une demande d'assistance technique.
+ Son contenu sera accessible publiquement sur scodoc.org, veuillez donc ne pas y inclure d'informations sensibles.
+ L'adresse email associée à votre compte ScoDoc est automatiquement transmise avec votre demande mais ne sera pas + affichée publiquement.
+

+ +
+
+ {{ wtf.quick_form(form) }} +
+
+{% endblock app_content %} diff --git a/app/views/scolar.py b/app/views/scolar.py index 87896d922..b9953863d 100644 --- a/app/views/scolar.py +++ b/app/views/scolar.py @@ -110,6 +110,7 @@ from app.scodoc import ( sco_up_to_date, ) from app.tables import list_etuds +from app.forms.main.create_bug_report import CreateBugReport def sco_publish(route, function, permission, methods=["GET"]): @@ -2562,77 +2563,42 @@ def sco_dump_and_send_db(message="", request_url="", traceback_str_base64=""): # --- Report form (assistance) -@bp.route("/sco_bug_report", methods=["GET"]) +@bp.route("/sco_bug_report", methods=["GET", "POST"]) @scodoc @permission_required(Permission.ScoView) def sco_bug_report_form(): "Formulaire de création d'un ticket d'assistance" - H = [ - html_sco_header.sco_header(page_title="Assistance technique"), - """

Assistance technique

-

- Ce formulaire permet d'effectuer une demande d'assistance technique.
- Son contenu sera accessible publiquement sur scodoc.org, veuillez donc ne pas y inclure d'informations sensibles.
- L'adresse email associée à votre compte ScoDoc est automatiquement transmise avec votre demande mais ne sera pas - affichée publiquement.
-

-
- *
-
- *
-
-
- """, - ] + form = CreateBugReport() + if request.method == "POST" and form.cancel.data: # cancel button + return redirect(url_for("scodoc.index")) + if form.validate_on_submit(): + r = sco_bug_report.sco_bug_report( + form.title.data, form.message.data, form.etab.data, form.include_dump.data + ) - H.append( - '
' + status_code = r.status_code + try: + r_msg = r.json()["message"] + except (requests.exceptions.JSONDecodeError, KeyError): + r_msg = ( + "Erreur: code " + + str(status_code) + + ' Merci de contacter ' + + scu.SCO_DEV_MAIL + + "" + ) + + H = [html_sco_header.sco_header(page_title="Assistance technique")] + if r.status_code >= 200 and r.status_code < 300: + H.append(f"""

Opération effectuée.

{r_msg}

""") + else: + H.append(f"""

{r_msg}

""") + return "\n".join(H) + html_sco_header.sco_footer() + + return render_template( + "sco_bug_report.j2", + form=form, ) - - H.append( - """ - -

- -
- """ - ) - - return "\n".join(H) + html_sco_header.sco_footer() - - -# --- Report create (assistance) -@bp.route("/sco_bug_report", methods=["POST"]) -@scodoc -@permission_required(Permission.ScoView) -@scodoc7func -def sco_bug_report_create(title=None, message=None, etab="", include_dump=False): - "Création d'un ticket d'assistance" - - if not title or not message: - flash("Les champs titre et message sont obligatoires.") - return flask.redirect(url_for("scolar.sco_bug_report")) - - r = sco_bug_report.sco_bug_report(title, message, etab, include_dump) - - status_code = r.status_code - try: - r_msg = r.json()["message"] - except (requests.exceptions.JSONDecodeError, KeyError): - r_msg = "Erreur: code " - +status_code - +' Merci de contacter ' - +scu.SCO_DEV_MAIL - +"" - - H = [html_sco_header.sco_header(page_title="Assistance technique")] - if r.status_code >= 200 and r.status_code < 300: - H.append(f"""

Opération effectuée.

{r_msg}

""") - else: - H.append(f"""

{r_msg}

""") - return "\n".join(H) + html_sco_header.sco_footer()