From 6e525b3f0239dfd553e7a87a5c290c59cc08e457 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 29 Aug 2022 15:23:44 +0200 Subject: [PATCH] Formulaires: check min/max integers and floats --- app/scodoc/TrivialFormulator.py | 15 +++++++++++++++ app/scodoc/sco_edit_module.py | 3 +++ app/scodoc/sco_edit_ue.py | 4 ++++ app/scodoc/sco_evaluation_edit.py | 2 +- app/scodoc/sco_ue_external.py | 2 ++ app/scodoc/sco_utils.py | 7 +++++++ app/views/scolar.py | 2 ++ 7 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/scodoc/TrivialFormulator.py b/app/scodoc/TrivialFormulator.py index 5b7020a8f..20004c87a 100644 --- a/app/scodoc/TrivialFormulator.py +++ b/app/scodoc/TrivialFormulator.py @@ -10,6 +10,7 @@ """ import html import re +import app.scodoc.sco_utils as scu # re validant dd/mm/yyyy DMY_REGEXP = re.compile( @@ -297,6 +298,20 @@ class TF(object): % (val, field, descr["max_value"]) ) ok = 0 + if typ[:3] == "int": + if not (scu.DB_MIN_INT <= self.values[field] <= scu.DB_MAX_INT): + msg.append( + f"Le champ '{field}' est a une valeur hors limite" + ) + ok = 0 + elif typ == "float" or typ == "real": + if not ( + scu.DB_MIN_FLOAT <= self.values[field] <= scu.DB_MAX_FLOAT + ): + msg.append( + f"Le champ '{field}' est a une valeur hors limite" + ) + ok = 0 if ok and (typ[:3] == "str") and "max_length" in descr: if len(self.values[field]) > descr["max_length"]: msg.append( diff --git a/app/scodoc/sco_edit_module.py b/app/scodoc/sco_edit_module.py index 2e8c9ca71..ef4cc3aa4 100644 --- a/app/scodoc/sco_edit_module.py +++ b/app/scodoc/sco_edit_module.py @@ -466,6 +466,7 @@ def module_edit( "title": "Heures cours :", "size": 4, "type": "float", + "min_value": 0, "explanation": "nombre d'heures de cours (optionnel)", }, ), @@ -475,6 +476,7 @@ def module_edit( "title": "Heures de TD :", "size": 4, "type": "float", + "min_value": 0, "explanation": "nombre d'heures de Travaux Dirigés (optionnel)", }, ), @@ -484,6 +486,7 @@ def module_edit( "title": "Heures de TP :", "size": 4, "type": "float", + "min_value": 0, "explanation": "nombre d'heures de Travaux Pratiques (optionnel)", }, ), diff --git a/app/scodoc/sco_edit_ue.py b/app/scodoc/sco_edit_ue.py index 4cc745027..767049a30 100644 --- a/app/scodoc/sco_edit_ue.py +++ b/app/scodoc/sco_edit_ue.py @@ -352,6 +352,8 @@ def ue_edit(ue_id=None, create=False, formation_id=None, default_semestre_idx=No { "size": 4, "type": "float", + "min_value": 0, + "max_value": 1000, "title": "ECTS", "explanation": "nombre de crédits ECTS (indiquer 0 si UE bonus)", "allow_null": not is_apc, # ects requis en APC @@ -365,6 +367,7 @@ def ue_edit(ue_id=None, create=False, formation_id=None, default_semestre_idx=No { "size": 4, "type": "float", + "min_value": 0, "title": "Coef. RCUE", "explanation": """pondération utilisée pour le calcul de la moyenne du RCUE. Laisser à 1, sauf si votre établissement a explicitement décidé de pondérations. """, @@ -381,6 +384,7 @@ def ue_edit(ue_id=None, create=False, formation_id=None, default_semestre_idx=No { "size": 4, "type": "float", + "min_value": 0, "title": "Coefficient", "explanation": """les coefficients d'UE ne sont utilisés que lorsque l'option Utiliser les coefficients d'UE pour calculer diff --git a/app/scodoc/sco_evaluation_edit.py b/app/scodoc/sco_evaluation_edit.py index c7d3402cb..b4b6ba38b 100644 --- a/app/scodoc/sco_evaluation_edit.py +++ b/app/scodoc/sco_evaluation_edit.py @@ -209,7 +209,7 @@ def evaluation_create_form( "coefficient", { "size": 6, - "type": "float", + "type": "float", # peut être négatif (!) "explanation": "coef. dans le module (choisi librement par l'enseignant, non utilisé pour rattrapage et 2ème session)", "allow_null": False, }, diff --git a/app/scodoc/sco_ue_external.py b/app/scodoc/sco_ue_external.py index 825982286..21f9dadd2 100644 --- a/app/scodoc/sco_ue_external.py +++ b/app/scodoc/sco_ue_external.py @@ -318,6 +318,8 @@ def external_ue_create_form(formsemestre_id: int, etudid: int): { "size": 4, "type": "float", + "min_value": 0, + "max_value": 1000, "title": "ECTS", "explanation": "nombre de crédits ECTS", "dom_id": "tf_extue_ects", diff --git a/app/scodoc/sco_utils.py b/app/scodoc/sco_utils.py index 9e7223749..247788b14 100644 --- a/app/scodoc/sco_utils.py +++ b/app/scodoc/sco_utils.py @@ -655,6 +655,13 @@ def to_bool(x) -> bool: return bool(x) +# Min/Max values for numbers stored in database: +DB_MIN_FLOAT = -1e30 +DB_MAX_FLOAT = 1e30 +DB_MIN_INT = -(1 << 31) +DB_MAX_INT = (1 << 31) - 1 + + def bul_filename_old(sem: dict, etud: dict, format): """Build a filename for this bulletin""" dt = time.strftime("%Y-%m-%d") diff --git a/app/views/scolar.py b/app/views/scolar.py index 9b6388a7e..e1f3da345 100644 --- a/app/views/scolar.py +++ b/app/views/scolar.py @@ -1488,6 +1488,8 @@ def _etudident_create_or_edit_form(edit): "size": 5, "title": "Année bac", "type": "int", + "min_value": 1945, + "max_value": 2100, "explanation": "année obtention du bac", }, ),