diff --git a/app/scodoc/sco_portal_apogee.py b/app/scodoc/sco_portal_apogee.py index 95ad30d998..ae58aa737c 100644 --- a/app/scodoc/sco_portal_apogee.py +++ b/app/scodoc/sco_portal_apogee.py @@ -35,6 +35,8 @@ import xml import xml.sax.saxutils import xml.dom.minidom +from flask import flash + import app.scodoc.sco_utils as scu from app import log from app.scodoc import sco_cache @@ -67,7 +69,7 @@ class PortalInterface(object): portal_url += "/" if self.first_time: if portal_url: - log("Portal URL=%s" % portal_url) + log(f"Portal URL={portal_url}") else: log("Portal not configured") self.first_time = False @@ -338,25 +340,27 @@ def get_etud_apogee(code_nip): if not d: return None if len(d) > 1: - raise ValueError("invalid XML response from Etudiant Web Service\n%s" % doc) + log(f"get_etud_apogee({code_nip}): {len(d)} etudiants !\n{doc}") + flash("Attention: plusieurs étudiants inscrits avec le NIP {code_nip}") + # dans ce cas, renvoie le premier étudiant return d[0] def get_default_etapes(): """Liste par défaut, lue du fichier de config""" filename = scu.SCO_TOOLS_DIR + "/default-etapes.txt" - log("get_default_etapes: reading %s" % filename) - f = open(filename) + log(f"get_default_etapes: reading {filename}") etapes = {} - for line in f.readlines(): - line = line.strip() - if line and line[0] != "#": - dept, code, intitule = [x.strip() for x in line.split(":")] - if dept and code: - if dept in etapes: - etapes[dept][code] = intitule - else: - etapes[dept] = {code: intitule} + with open(filename, encoding=scu.SCO_ENCODING) as f: + for line in f.readlines(): + line = line.strip() + if line and line[0] != "#": + dept, code, intitule = [x.strip() for x in line.split(":")] + if dept and code: + if dept in etapes: + etapes[dept][code] = intitule + else: + etapes[dept] = {code: intitule} return etapes @@ -369,7 +373,7 @@ def _parse_etapes_from_xml(doc): dom = xml.dom.minidom.parseString(doc) infos = {} if dom.childNodes[0].nodeName != "etapes": - raise ValueError + raise ValueError("élément 'etapes' attendu") if xml_etapes_by_dept: # Ancien format XML avec des sections par departement: for d in dom.childNodes[0].childNodes: @@ -394,8 +398,7 @@ def get_etapes_apogee(): if etapes_url: portal_timeout = sco_preferences.get_preference("portal_timeout") log( - "get_etapes_apogee: requesting '%s' with timeout=%s" - % (etapes_url, portal_timeout) + f"""get_etapes_apogee: requesting '{etapes_url}' with timeout={portal_timeout}""" ) doc = scu.query_portal(etapes_url, timeout=portal_timeout) try: @@ -403,15 +406,17 @@ def get_etapes_apogee(): # cache le resultat (utile si le portail repond de façon intermitente) if infos: log("get_etapes_apogee: caching result") - with open(SCO_CACHE_ETAPE_FILENAME, "w") as f: + with open( + SCO_CACHE_ETAPE_FILENAME, "w", encoding=scu.SCO_ENCODING + ) as f: f.write(doc) except: - log("invalid XML response from getEtapes Web Service\n%s" % etapes_url) - # Avons nous la copie d'une réponse récente ? + log(f"invalid XML response from getEtapes Web Service\n{etapes_url}") + # Avons-nous la copie d'une réponse récente ? try: - doc = open(SCO_CACHE_ETAPE_FILENAME).read() + doc = open(SCO_CACHE_ETAPE_FILENAME, encoding=scu.SCO_ENCODING).read() infos = _parse_etapes_from_xml(doc) - log("using last saved version from " + SCO_CACHE_ETAPE_FILENAME) + log(f"using last saved version from {SCO_CACHE_ETAPE_FILENAME}") except: infos = {} else: