From 08e9d2449a4708b25e800ac9b8d00892fa8b3e6d Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Tue, 25 Jan 2022 23:48:39 +0100 Subject: [PATCH] Reorganisation form configuration globale. Doc bonus. --- app/forms/main/config_apo.py | 1 - .../main/{config_forms.py => config_logos.py} | 35 ++--- app/forms/main/config_main.py | 76 +++++++++++ app/scodoc/sco_config_actions.py | 25 ---- app/scodoc/sco_preferences.py | 2 +- .../js/{configuration.js => config_logos.js} | 2 +- app/templates/config_logos.html | 125 +++++++++++++++++ app/templates/configuration.html | 128 +++++------------- app/views/scodoc.py | 24 +++- scodoc.py | 1 - 10 files changed, 267 insertions(+), 152 deletions(-) rename app/forms/main/{config_forms.py => config_logos.py} (92%) create mode 100644 app/forms/main/config_main.py rename app/static/js/{configuration.js => config_logos.js} (54%) create mode 100644 app/templates/config_logos.html diff --git a/app/forms/main/config_apo.py b/app/forms/main/config_apo.py index a655f450f..6151f2f31 100644 --- a/app/forms/main/config_apo.py +++ b/app/forms/main/config_apo.py @@ -28,7 +28,6 @@ """ Formulaires configuration Exports Apogée (codes) """ -import re from flask import flash, url_for, redirect, render_template from flask_wtf import FlaskForm diff --git a/app/forms/main/config_forms.py b/app/forms/main/config_logos.py similarity index 92% rename from app/forms/main/config_forms.py rename to app/forms/main/config_logos.py index 26548f08d..91a73747a 100644 --- a/app/forms/main/config_forms.py +++ b/app/forms/main/config_logos.py @@ -47,7 +47,6 @@ from app.scodoc.sco_config_actions import ( LogoDelete, LogoUpdate, LogoInsert, - BonusSportUpdate, ) from flask_login import current_user @@ -296,23 +295,15 @@ def _make_depts_data(modele): return data -def _make_data(bonus_sport, modele): +def _make_data(modele): data = { - "bonus_sport_func_name": bonus_sport, "depts": _make_depts_data(modele=modele), } return data -class ScoDocConfigurationForm(FlaskForm): - "Panneau de configuration général" - bonus_sport_func_name = SelectField( - label="Fonction de calcul des bonus sport&culture", - choices=[ - (x, x if x else "Aucune") - for x in ScoDocSiteConfig.get_bonus_sport_class_names() - ], - ) +class LogosConfigurationForm(FlaskForm): + "Panneau de configuration des logos" depts = FieldList(FormField(DeptForm)) def __init__(self, *args, **kwargs): @@ -361,11 +352,6 @@ class ScoDocConfigurationForm(FlaskForm): return dept_form.get_form(logoname) def select_action(self): - if ( - self.data["bonus_sport_func_name"] - != ScoDocSiteConfig.get_bonus_sport_class_name() - ): - return BonusSportUpdate(self.data) for dept_entry in self.depts: dept_form = dept_entry.form action = dept_form.select_action() @@ -374,14 +360,11 @@ class ScoDocConfigurationForm(FlaskForm): return None -def configuration(): - """Panneau de configuration général""" - auth_name = str(current_user) - if not current_user.is_administrator(): - raise AccessDenied("invalid user (%s) must be SuperAdmin" % auth_name) - form = ScoDocConfigurationForm( +def config_logos(): + "Page de configuration des logos" + # nb: le contrôle d'accès (SuperAdmin) doit être fait dans la vue + form = LogosConfigurationForm( data=_make_data( - bonus_sport=ScoDocSiteConfig.get_bonus_sport_class_name(), modele=sco_logos.list_logos(), ) ) @@ -392,11 +375,11 @@ def configuration(): flash(action.message) return redirect( url_for( - "scodoc.configuration", + "scodoc.configure_logos", ) ) return render_template( - "configuration.html", + "config_logos.html", scodoc_dept=None, title="Configuration ScoDoc", form=form, diff --git a/app/forms/main/config_main.py b/app/forms/main/config_main.py new file mode 100644 index 000000000..e0b349e49 --- /dev/null +++ b/app/forms/main/config_main.py @@ -0,0 +1,76 @@ +# -*- mode: python -*- +# -*- coding: utf-8 -*- + +############################################################################## +# +# ScoDoc +# +# Copyright (c) 1999 - 2022 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 +# +############################################################################## + +""" +Formulaires configuration Exports Apogée (codes) +""" + +from flask import flash, url_for, redirect, request, render_template +from flask_wtf import FlaskForm +from wtforms import SelectField, SubmitField + +import app +from app.models import ScoDocSiteConfig + + +class ScoDocConfigurationForm(FlaskForm): + "Panneau de configuration des logos" + bonus_sport_func_name = SelectField( + label="Fonction de calcul des bonus sport&culture", + choices=[ + (x, x if x else "Aucune") + for x in ScoDocSiteConfig.get_bonus_sport_class_names() + ], + ) + submit = SubmitField("Valider") + cancel = SubmitField("Annuler", render_kw={"formnovalidate": True}) + + +def configuration(): + "Page de configuration principale" + # nb: le contrôle d'accès (SuperAdmin) doit être fait dans la vue + form = ScoDocConfigurationForm( + data={ + "bonus_sport_func_name": ScoDocSiteConfig.get_bonus_sport_class_name(), + } + ) + if request.method == "POST" and form.cancel.data: # cancel button + return redirect(url_for("scodoc.index")) + if form.validate_on_submit(): + if ( + form.data["bonus_sport_func_name"] + != ScoDocSiteConfig.get_bonus_sport_class_name() + ): + ScoDocSiteConfig.set_bonus_sport_class(form.data["bonus_sport_func_name"]) + app.clear_scodoc_cache() + flash(f"Fonction bonus sport&culture configurée.") + return redirect(url_for("scodoc.index")) + + return render_template( + "configuration.html", + form=form, + ) diff --git a/app/scodoc/sco_config_actions.py b/app/scodoc/sco_config_actions.py index 949567eca..f6ea96371 100644 --- a/app/scodoc/sco_config_actions.py +++ b/app/scodoc/sco_config_actions.py @@ -152,28 +152,3 @@ class LogoInsert(Action): name=self.parameters["name"], dept_id=dept_id, ) - - -class BonusSportUpdate(Action): - """Action: Change bonus_sport_function_name. - bonus_sport_function_name: the new value""" - - def __init__(self, parameters): - super().__init__( - f"Changement du calcul de bonus sport pour ({parameters['bonus_sport_func_name']}).", - parameters, - ) - - @staticmethod - def build_action(parameters): - if ( - parameters["bonus_sport_func_name"] - != ScoDocSiteConfig.get_bonus_sport_class_name() - ): - return [BonusSportUpdate(parameters)] - return [] - - def execute(self): - current_app.logger.info(self.message) - ScoDocSiteConfig.set_bonus_sport_func(self.parameters["bonus_sport_func_name"]) - app.clear_scodoc_cache() diff --git a/app/scodoc/sco_preferences.py b/app/scodoc/sco_preferences.py index 6aba9694d..dbbdd0ac5 100644 --- a/app/scodoc/sco_preferences.py +++ b/app/scodoc/sco_preferences.py @@ -2019,7 +2019,7 @@ class BasePreferences(object): H = [ html_sco_header.sco_header(page_title="Préférences"), "

Préférences globales pour %s

" % scu.ScoURL(), - # f"""

modification des logos du département (pour documents pdf)

""" # if current_user.is_administrator() # else "", diff --git a/app/static/js/configuration.js b/app/static/js/config_logos.js similarity index 54% rename from app/static/js/configuration.js rename to app/static/js/config_logos.js index b537d572f..56b7cd27d 100644 --- a/app/static/js/configuration.js +++ b/app/static/js/config_logos.js @@ -1,5 +1,5 @@ function submit_form() { - $("#configuration_form").submit(); + $("#config_logos_form").submit(); } $(function () { diff --git a/app/templates/config_logos.html b/app/templates/config_logos.html new file mode 100644 index 000000000..f4bd543cb --- /dev/null +++ b/app/templates/config_logos.html @@ -0,0 +1,125 @@ +{# -*- mode: jinja-html -*- #} +{% extends 'base.html' %} +{% import 'bootstrap/wtf.html' as wtf %} + +{% macro render_field(field, with_label=True) %} +
+ {% if with_label %} + {{ field.label }} : + {% endif %} + {{ field(**kwargs)|safe }} + {% if field.errors %} +
    + {% for error in field.errors %} +
  • {{ error }}
  • + {% endfor %} +
+ {% endif %} +
+
+{% endmacro %} + +{% macro render_add_logo(add_logo_form) %} +
+

Ajouter un logo

+ {{ add_logo_form.hidden_tag() }} + {{ render_field(add_logo_form.name) }} + {{ render_field(add_logo_form.upload) }} + {{ render_field(add_logo_form.do_insert, False, onSubmit="submit_form") }} +
+{% endmacro %} + +{% macro render_logo(dept_form, logo_form) %} +
+ {{ logo_form.hidden_tag() }} + {% if logo_form.titre %} + + +
+

{{ logo_form.titre }}

+
+
{{ logo_form.description or "" }}
+ + + {% else %} + + + +

Logo personalisé: {{ logo_form.logo_id.data }}

+
+ {{ logo_form.description or "" }} + + + {% endif %} + + +
+ pas de logo chargé +
+ + +

{{ logo_form.logo.logoname }} (Format: {{ logo_form.logo.suffix }})

+ Taille: {{ logo_form.logo.size }} px + {% if logo_form.logo.mm %}   /   {{ logo_form.logo.mm }} mm {% endif %}
+ Aspect ratio: {{ logo_form.logo.aspect_ratio }}
+ Usage: {{ logo_form.logo.get_usage() }} + + +

Modifier l'image

+ {{ render_field(logo_form.upload, False, onchange="submit_form()") }} + {% if logo_form.can_delete %} +

Supprimer l'image

+ {{ render_field(logo_form.do_delete, False, onSubmit="submit_form()") }} + {% endif %} + + +
+{% endmacro %} + +{% macro render_logos(dept_form) %} + + {% for logo_entry in dept_form.logos.entries %} + {% set logo_form = logo_entry.form %} + {{ render_logo(dept_form, logo_form) }} + {% else %} +

+

Aucun logo défini en propre à ce département

+

+ {% endfor %} +
+{% endmacro %} + +{% block app_content %} + + + + +
+ {{ form.hidden_tag() }} + + +
+{% endblock %} \ No newline at end of file diff --git a/app/templates/configuration.html b/app/templates/configuration.html index 1eee13d45..888d95914 100644 --- a/app/templates/configuration.html +++ b/app/templates/configuration.html @@ -19,104 +19,46 @@ {% endmacro %} -{% macro render_add_logo(add_logo_form) %} -
-

Ajouter un logo

- {{ add_logo_form.hidden_tag() }} - {{ render_field(add_logo_form.name) }} - {{ render_field(add_logo_form.upload) }} - {{ render_field(add_logo_form.do_insert, False, onSubmit="submit_form") }} -
-{% endmacro %} - -{% macro render_logo(dept_form, logo_form) %} -
- {{ logo_form.hidden_tag() }} - {% if logo_form.titre %} - - -

{{ logo_form.titre }}

-
{{ logo_form.description or "" }}
- - - {% else %} - - -

Logo personalisé: {{ logo_form.logo_id.data }}

- {{ logo_form.description or "" }} - - - {% endif %} - - -
- pas de logo chargé
- -

{{ logo_form.logo.logoname }} (Format: {{ logo_form.logo.suffix }})

- Taille: {{ logo_form.logo.size }} px - {% if logo_form.logo.mm %}   /   {{ logo_form.logo.mm }} mm {% endif %}
- Aspect ratio: {{ logo_form.logo.aspect_ratio }}
- Usage: {{ logo_form.logo.get_usage() }} - -

Modifier l'image

- {{ render_field(logo_form.upload, False, onchange="submit_form()") }} - {% if logo_form.can_delete %} -

Supprimer l'image

- {{ render_field(logo_form.do_delete, False, onSubmit="submit_form()") }} - {% endif %} - - -
-{% endmacro %} - -{% macro render_logos(dept_form) %} - - {% for logo_entry in dept_form.logos.entries %} - {% set logo_form = logo_entry.form %} - {{ render_logo(dept_form, logo_form) }} - {% else %} -

Aucun logo défini en propre à ce département

- {% endfor %} -
-{% endmacro %} - {% block app_content %} - - -
{{ form.hidden_tag() }} -
-{% endblock %} \ No newline at end of file + +{% endblock %} + +{% block scripts %} +{{ super() }} + + +{% endblock %} diff --git a/app/views/scodoc.py b/app/views/scodoc.py index ea2f2a419..234d688c5 100644 --- a/app/views/scodoc.py +++ b/app/views/scodoc.py @@ -45,7 +45,7 @@ from werkzeug.exceptions import BadRequest, NotFound from app import db from app.auth.models import User -from app.forms.main import config_forms +from app.forms.main import config_logos, config_main from app.forms.main.create_dept import CreateDeptForm from app.forms.main.config_apo import CodesDecisionsForm from app import models @@ -250,10 +250,26 @@ def about(scodoc_dept=None): @bp.route("/ScoDoc/configuration", methods=["GET", "POST"]) @admin_required def configuration(): - auth_name = str(current_user) + "Page de configuration globale" if not current_user.is_administrator(): - raise AccessDenied("invalid user (%s) must be SuperAdmin" % auth_name) - return config_forms.configuration() + raise AccessDenied("invalid user (%s) must be SuperAdmin" % current_user) + return config_main.configuration() + + +@bp.route("/ScoDoc/get_bonus_description/", methods=["GET"]) +def get_bonus_description(bonus_name: str): + "description text/html du bonus" + bonus_class = ScoDocSiteConfig.get_bonus_sport_class_from_name(bonus_name) + return bonus_class.__doc__ + + +@bp.route("/ScoDoc/configure_logos", methods=["GET", "POST"]) +@admin_required +def configure_logos(): + "Page de configuration des logos (globale)" + if not current_user.is_administrator(): + raise AccessDenied("invalid user (%s) must be SuperAdmin" % current_user) + return config_logos.config_logos() SMALL_SIZE = (200, 200) diff --git a/scodoc.py b/scodoc.py index 451ebee2d..a18251fdc 100755 --- a/scodoc.py +++ b/scodoc.py @@ -21,7 +21,6 @@ from app import clear_scodoc_cache from app import models from app.auth.models import User, Role, UserRole -from app.models import ScoPreference from app.scodoc.sco_logos import make_logo_local from app.models import Formation, UniteEns, Module from app.models import FormSemestre, FormSemestreInscription