From 7daa49f2aa146c574c4b7a06ed6fc822d61f0067 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sat, 18 Sep 2021 13:42:19 +0200 Subject: [PATCH] Elimine les attributs de ZREQUEST, sauf forms. --- app/decorators.py | 4 +- app/scodoc/sco_archives.py | 9 ++-- app/scodoc/sco_archives_etud.py | 15 +++---- app/scodoc/sco_bulletins.py | 44 ++++++++++--------- app/scodoc/sco_bulletins_generator.py | 15 ++----- app/scodoc/sco_bulletins_json.py | 12 ++---- app/scodoc/sco_bulletins_xml.py | 5 +-- app/scodoc/sco_dump_db.py | 7 ++-- app/scodoc/sco_edit_module.py | 3 +- app/scodoc/sco_etud.py | 6 +++ app/scodoc/sco_formations.py | 3 +- app/scodoc/sco_groups_view.py | 7 ++-- app/scodoc/sco_moduleimpl_inscriptions.py | 3 +- app/scodoc/sco_moduleimpl_status.py | 14 +++---- app/scodoc/sco_page_etud.py | 5 ++- app/scodoc/sco_placement.py | 3 +- app/scodoc/sco_prepajury.py | 3 +- app/scodoc/sco_saisie_notes.py | 6 +-- app/scodoc/sco_ue_external.py | 2 +- app/scodoc/sco_utils.py | 39 ++++------------- app/views/absences.py | 10 +++-- app/views/entreprises.py | 51 ++++++++--------------- app/views/notes.py | 28 +++++-------- app/views/scolar.py | 2 +- app/views/users.py | 6 +-- 25 files changed, 127 insertions(+), 175 deletions(-) diff --git a/app/decorators.py b/app/decorators.py index a72d8253..87f2fd99 100644 --- a/app/decorators.py +++ b/app/decorators.py @@ -55,8 +55,8 @@ class ZRequest(object): # query_string is bytes: # self.QUERY_STRING = request.query_string.decode("utf-8") # self.REQUEST_METHOD = request.method - self.AUTHENTICATED_USER = current_user - self.REMOTE_ADDR = request.remote_addr + # self.AUTHENTICATED_USER = current_user + # self.REMOTE_ADDR = request.remote_addr if request.method == "POST": # request.form is a werkzeug.datastructures.ImmutableMultiDict # must copy to get a mutable version (needed by TrivialFormulator) diff --git a/app/scodoc/sco_archives.py b/app/scodoc/sco_archives.py index e17cea63..99ff4826 100644 --- a/app/scodoc/sco_archives.py +++ b/app/scodoc/sco_archives.py @@ -57,6 +57,7 @@ import time import flask from flask import g, request +from flask_login import current_user import app.scodoc.sco_utils as scu from config import Config @@ -382,9 +383,7 @@ def formsemestre_archive(REQUEST, formsemestre_id, group_ids=[]): (all students or only selected groups) """ if not sco_permissions_check.can_edit_pv(formsemestre_id): - raise AccessDenied( - "opération non autorisée pour %s" % str(REQUEST.AUTHENTICATED_USER) - ) + raise AccessDenied("opération non autorisée pour %s" % str(current_user)) sem = sco_formsemestre.get_formsemestre(formsemestre_id) if not group_ids: @@ -559,9 +558,7 @@ def formsemestre_delete_archive( ): """Delete an archive""" if not sco_permissions_check.can_edit_pv(formsemestre_id): - raise AccessDenied( - "opération non autorisée pour %s" % str(REQUEST.AUTHENTICATED_USER) - ) + raise AccessDenied("opération non autorisée pour %s" % str(current_user)) sem = sco_formsemestre.get_formsemestre(formsemestre_id) sem_archive_id = formsemestre_id archive_id = PVArchive.get_id_from_name(sem_archive_id, archive_name) diff --git a/app/scodoc/sco_archives_etud.py b/app/scodoc/sco_archives_etud.py index fa41bb65..0c88d2de 100644 --- a/app/scodoc/sco_archives_etud.py +++ b/app/scodoc/sco_archives_etud.py @@ -31,6 +31,7 @@ """ import flask from flask import url_for, g, request +from flask_login import current_user import app.scodoc.sco_utils as scu from app.scodoc import sco_import_etuds @@ -60,7 +61,7 @@ def can_edit_etud_archive(authuser): def etud_list_archives_html(REQUEST, etudid): """HTML snippet listing archives""" - can_edit = can_edit_etud_archive(REQUEST.AUTHENTICATED_USER) + can_edit = can_edit_etud_archive(current_user) etuds = sco_etud.get_etud_info(etudid=etudid) if not etuds: raise ScoValueError("étudiant inexistant") @@ -133,10 +134,8 @@ def add_archives_info_to_etud_list(etuds): def etud_upload_file_form(REQUEST, etudid): """Page with a form to choose and upload a file, with a description.""" # check permission - if not can_edit_etud_archive(REQUEST.AUTHENTICATED_USER): - raise AccessDenied( - "opération non autorisée pour %s" % REQUEST.AUTHENTICATED_USER - ) + if not can_edit_etud_archive(current_user): + raise AccessDenied("opération non autorisée pour %s" % current_user) etuds = sco_etud.get_etud_info(filled=True) if not etuds: raise ScoValueError("étudiant inexistant") @@ -202,10 +201,8 @@ def _store_etud_file_to_new_archive(etud_archive_id, data, filename, description def etud_delete_archive(REQUEST, etudid, archive_name, dialog_confirmed=False): """Delete an archive""" # check permission - if not can_edit_etud_archive(REQUEST.AUTHENTICATED_USER): - raise AccessDenied( - "opération non autorisée pour %s" % str(REQUEST.AUTHENTICATED_USER) - ) + if not can_edit_etud_archive(current_user): + raise AccessDenied("opération non autorisée pour %s" % str(current_user)) etuds = sco_etud.get_etud_info(filled=True) if not etuds: raise ScoValueError("étudiant inexistant") diff --git a/app/scodoc/sco_bulletins.py b/app/scodoc/sco_bulletins.py index 6a5bbb96..20a29f4e 100644 --- a/app/scodoc/sco_bulletins.py +++ b/app/scodoc/sco_bulletins.py @@ -48,7 +48,7 @@ import app.scodoc.sco_utils as scu import app.scodoc.notesdb as ndb from app import log from app.scodoc.sco_permissions import Permission -from app.scodoc.sco_exceptions import AccessDenied +from app.scodoc.sco_exceptions import AccessDenied, ScoValueError from app.scodoc import html_sco_header from app.scodoc import htmlutils from app.scodoc import sco_abs @@ -121,9 +121,7 @@ def make_context_dict(sem, etud): return C -def formsemestre_bulletinetud_dict( - formsemestre_id, etudid, version="long", REQUEST=None -): +def formsemestre_bulletinetud_dict(formsemestre_id, etudid, version="long"): """Collecte informations pour bulletin de notes Retourne un dictionnaire (avec valeur par défaut chaine vide). Le contenu du dictionnaire dépend des options (rangs, ...) @@ -143,10 +141,7 @@ def formsemestre_bulletinetud_dict( I["etudid"] = etudid I["formsemestre_id"] = formsemestre_id I["sem"] = nt.sem - if REQUEST: - I["server_name"] = request.url_root - else: - I["server_name"] = "" + I["server_name"] = request.url_root # Formation et parcours I["formation"] = sco_formations.formation_list( @@ -778,7 +773,10 @@ def formsemestre_bulletinetud( etud = sco_etud.get_etud_info(filled=True)[0] etudid = etud["etudid"] except: - return scu.log_unknown_etud(REQUEST, format=format) + sco_etud.log_unknown_etud() + raise ScoValueError("étudiant inconnu") + + sem = sco_formsemestre.get_formsemestre(formsemestre_id) bulletin = do_formsemestre_bulletinetud( formsemestre_id, @@ -791,7 +789,8 @@ def formsemestre_bulletinetud( REQUEST=REQUEST, )[0] if format not in {"html", "pdfmail"}: - return bulletin + filename = scu.bul_filename(sem, etud, format) + return scu.send_file(bulletin, filename, mime=scu.get_mime_suffix(format)) sem = sco_formsemestre.get_formsemestre(formsemestre_id) H = [ @@ -862,14 +861,13 @@ def do_formsemestre_bulletinetud( ): """Génère le bulletin au format demandé. Retourne: (bul, filigranne) - où bul est au format demandé (html, pdf, pdfmail, pdfpart, xml) + où bul est str ou bytes au format demandé (html, pdf, pdfmail, pdfpart, xml, json) et filigranne est un message à placer en "filigranne" (eg "Provisoire"). """ if format == "xml": bul = sco_bulletins_xml.make_xml_formsemestre_bulletinetud( formsemestre_id, etudid, - REQUEST=REQUEST, xml_with_decisions=xml_with_decisions, force_publishing=force_publishing, version=version, @@ -881,19 +879,18 @@ def do_formsemestre_bulletinetud( bul = sco_bulletins_json.make_json_formsemestre_bulletinetud( formsemestre_id, etudid, - REQUEST=REQUEST, xml_with_decisions=xml_with_decisions, force_publishing=force_publishing, version=version, ) return bul, "" - I = formsemestre_bulletinetud_dict(formsemestre_id, etudid, REQUEST=REQUEST) + I = formsemestre_bulletinetud_dict(formsemestre_id, etudid) etud = I["etud"] if format == "html": htm, _ = sco_bulletins_generator.make_formsemestre_bulletinetud( - I, version=version, format="html", REQUEST=REQUEST + I, version=version, format="html" ) return htm, I["filigranne"] @@ -903,7 +900,6 @@ def do_formsemestre_bulletinetud( version=version, format="pdf", stand_alone=(format != "pdfpart"), - REQUEST=REQUEST, ) if format == "pdf": return ( @@ -923,11 +919,11 @@ def do_formsemestre_bulletinetud( htm = "" # speed up if html version not needed else: htm, _ = sco_bulletins_generator.make_formsemestre_bulletinetud( - I, version=version, format="html", REQUEST=REQUEST + I, version=version, format="html" ) pdfdata, filename = sco_bulletins_generator.make_formsemestre_bulletinetud( - I, version=version, format="pdf", REQUEST=REQUEST + I, version=version, format="pdf" ) if prefer_mail_perso: @@ -998,7 +994,7 @@ def mail_bulletin(formsemestre_id, I, pdfdata, filename, recipient_addr): # Attach pdf msg.attach(filename, scu.PDF_MIMETYPE, pdfdata) - + breakpoint() log("mail bulletin a %s" % recipient_addr) email.send_message(msg) @@ -1119,6 +1115,16 @@ def _formsemestre_bulletinetud_header_html( "enabled": etud["emailperso"] and can_send_bulletin_by_mail(formsemestre_id), }, + { + "title": "Version json", + "endpoint": endpoint, + "args": { + "formsemestre_id": formsemestre_id, + "etudid": etudid, + "version": version, + "format": "json", + }, + }, { "title": "Version XML", "endpoint": endpoint, diff --git a/app/scodoc/sco_bulletins_generator.py b/app/scodoc/sco_bulletins_generator.py index 783c4986..2dee620a 100644 --- a/app/scodoc/sco_bulletins_generator.py +++ b/app/scodoc/sco_bulletins_generator.py @@ -53,6 +53,7 @@ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Frame, Page from reportlab.platypus import Table, TableStyle, Image, KeepInFrame from flask import request +from flask_login import current_user from app.scodoc import sco_utils as scu from app.scodoc.sco_exceptions import NoteProcessError @@ -150,14 +151,7 @@ class BulletinGenerator(object): def get_filename(self): """Build a filename to be proposed to the web client""" sem = sco_formsemestre.get_formsemestre(self.infos["formsemestre_id"]) - dt = time.strftime("%Y-%m-%d") - filename = "bul-%s-%s-%s.pdf" % ( - sem["titre_num"], - dt, - self.infos["etud"]["nom"], - ) - filename = scu.unescape_html(filename).replace(" ", "_").replace("&", "") - return filename + return scu.bul_filename(sem, self.infos["etud"], "pdf") def generate(self, format="", stand_alone=True): """Return bulletin in specified format""" @@ -262,7 +256,6 @@ def make_formsemestre_bulletinetud( version="long", # short, long, selectedevals format="pdf", # html, pdf stand_alone=True, - REQUEST=None, ): """Bulletin de notes @@ -288,7 +281,7 @@ def make_formsemestre_bulletinetud( PDFLOCK.acquire() bul_generator = gen_class( infos, - authuser=REQUEST.AUTHENTICATED_USER, + authuser=current_user, version=version, filigranne=infos["filigranne"], server_name=request.url_root, @@ -303,7 +296,7 @@ def make_formsemestre_bulletinetud( gen_class = bulletin_get_class(bul_class_name) bul_generator = gen_class( infos, - authuser=REQUEST.AUTHENTICATED_USER, + authuser=current_user, version=version, filigranne=infos["filigranne"], server_name=request.url_root, diff --git a/app/scodoc/sco_bulletins_json.py b/app/scodoc/sco_bulletins_json.py index 93e45e3c..160fc30d 100644 --- a/app/scodoc/sco_bulletins_json.py +++ b/app/scodoc/sco_bulletins_json.py @@ -47,27 +47,22 @@ from app.scodoc import sco_etud def make_json_formsemestre_bulletinetud( - formsemestre_id, - etudid, - REQUEST=None, + formsemestre_id: int, + etudid: int, xml_with_decisions=False, version="long", force_publishing=False, # force publication meme si semestre non publie sur "portail" -): +) -> str: """Renvoie bulletin en chaine JSON""" d = formsemestre_bulletinetud_published_dict( formsemestre_id, etudid, force_publishing=force_publishing, - REQUEST=REQUEST, xml_with_decisions=xml_with_decisions, version=version, ) - if REQUEST: - REQUEST.RESPONSE.setHeader("content-type", scu.JSON_MIMETYPE) - return json.dumps(d, cls=scu.ScoDocJSONEncoder) @@ -79,7 +74,6 @@ def formsemestre_bulletinetud_published_dict( etudid, force_publishing=False, xml_nodate=False, - REQUEST=None, xml_with_decisions=False, # inclue les decisions même si non publiées version="long", ): diff --git a/app/scodoc/sco_bulletins_xml.py b/app/scodoc/sco_bulletins_xml.py index 9329db68..bd20c7a1 100644 --- a/app/scodoc/sco_bulletins_xml.py +++ b/app/scodoc/sco_bulletins_xml.py @@ -69,16 +69,13 @@ def make_xml_formsemestre_bulletinetud( doc=None, # XML document force_publishing=False, xml_nodate=False, - REQUEST=None, xml_with_decisions=False, # inclue les decisions même si non publiées version="long", -): +) -> str: "bulletin au format XML" from app.scodoc import sco_bulletins log("xml_bulletin( formsemestre_id=%s, etudid=%s )" % (formsemestre_id, etudid)) - if REQUEST: - REQUEST.RESPONSE.setHeader("content-type", scu.XML_MIMETYPE) sem = sco_formsemestre.get_formsemestre(formsemestre_id) if (not sem["bul_hide_xml"]) or force_publishing: diff --git a/app/scodoc/sco_dump_db.py b/app/scodoc/sco_dump_db.py index befe5e33..5ee01a63 100644 --- a/app/scodoc/sco_dump_db.py +++ b/app/scodoc/sco_dump_db.py @@ -51,6 +51,7 @@ import fcntl import subprocess import requests +from flask_login import current_user import app.scodoc.notesdb as ndb import app.scodoc.sco_utils as scu @@ -190,10 +191,8 @@ def _send_db(REQUEST, ano_db_name): data={ "dept_name": sco_preferences.get_preference("DeptName"), "serial": _get_scodoc_serial(), - "sco_user": str(REQUEST.AUTHENTICATED_USER), - "sent_by": sco_users.user_info(str(REQUEST.AUTHENTICATED_USER))[ - "nomcomplet" - ], + "sco_user": str(current_user), + "sent_by": sco_users.user_info(str(current_user))["nomcomplet"], "sco_version": sco_version.SCOVERSION, "sco_fullversion": scu.get_scodoc_version(), }, diff --git a/app/scodoc/sco_edit_module.py b/app/scodoc/sco_edit_module.py index b0b6ca76..26799f25 100644 --- a/app/scodoc/sco_edit_module.py +++ b/app/scodoc/sco_edit_module.py @@ -30,6 +30,7 @@ """ import flask from flask import url_for, g, request +from flask_login import current_user import app.scodoc.notesdb as ndb import app.scodoc.sco_utils as scu @@ -544,7 +545,7 @@ def module_list(formation_id, REQUEST=None): % F, '