From 8d62455ef01840411a42ff6f655c65a2654d16fd Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Fri, 25 Jun 2021 18:25:46 +0200 Subject: [PATCH] =?UTF-8?q?connexion=20bd=20dept=20avec=20Flask=20global.?= =?UTF-8?q?=20Test=C3=A9=20en=20mode=20DEBIG=20seulement.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/__init__.py | 18 +++++++++++++ app/decorators.py | 4 +++ app/scodoc/notesdb.py | 46 ++++++++++++++++++++++++---------- app/scodoc/sco_abs.py | 12 +++++++-- app/static/js/abs_ajax.js | 2 +- app/static/js/etud_debouche.js | 2 +- app/static/js/groupmgr.js | 2 +- app/static/js/saisie_notes.js | 2 +- app/views/notes.py | 13 +++++++--- app/views/scolar.py | 4 +-- 10 files changed, 81 insertions(+), 24 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index c5353e032d..2c0d603d31 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -7,6 +7,8 @@ from logging.handlers import SMTPHandler, RotatingFileHandler from flask import request from flask import Flask +from flask import current_app +from flask import g from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_login import LoginManager @@ -16,6 +18,8 @@ from flask_moment import Moment from config import Config +from app.scodoc import notesdb as ndb + app = Flask(__name__) app.config.from_object(Config) @@ -29,6 +33,20 @@ bootstrap = Bootstrap(app) moment = Moment() +@app.before_request +def open_dept_db_connection(): + # current_app.logger.info("open_dept_db_connection") + if hasattr(g, "scodoc_dept") and not hasattr(g, "db_conn"): + g.db_conn = ndb.open_dept_connection() + + +@app.teardown_request +def close_dept_db_connection(): + # current_app.logger.info("close_dept_db_connection") + if hasattr(g, "db_conn"): + ndb.close_dept_connection() + + def create_app(config_class=Config): app = Flask(__name__, static_url_path="/ScoDoc/static", static_folder="static") app.logger.setLevel(logging.DEBUG) diff --git a/app/decorators.py b/app/decorators.py index 57f561b86d..cab531a804 100644 --- a/app/decorators.py +++ b/app/decorators.py @@ -16,6 +16,8 @@ from flask import request from flask_login import current_user from flask_login import login_required from flask import current_app + +import app from app.auth.models import Permission @@ -144,6 +146,8 @@ def scodoc7func(context): del kwargs["scodoc_dept"] elif not hasattr(g, "scodoc_dept"): g.scodoc_dept = None + # --- Open DB connection + app.open_dept_db_connection() # --- Emulate Zope's REQUEST REQUEST = ZRequest() g.zrequest = REQUEST diff --git a/app/scodoc/notesdb.py b/app/scodoc/notesdb.py index 9fcd9bfeb3..26fce29ede 100644 --- a/app/scodoc/notesdb.py +++ b/app/scodoc/notesdb.py @@ -8,6 +8,8 @@ import psycopg2.pool import psycopg2.extras import thread +from flask import g + import app.scodoc.sco_utils as scu from app.scodoc.notes_log import log from app.scodoc.sco_exceptions import ScoException, ScoValueError, NoteProcessError @@ -38,21 +40,39 @@ def unquote(s): # pour l'instance donnee par context # La connexion est unique (réutilisée) pour chaque thread # et est par défaut en autocommit -_pools = {} +# _pools = {} +# +# +# def GetDBConnexion(autocommit=True): +# """connexion to the DB of a departement""" +# pool = _pools.get(scu.get_db_cnx_string(), None) +# if not pool: +# pool = psycopg2.pool.ThreadedConnectionPool(2, 8, dsn=scu.get_db_cnx_string()) +# _pools[scu.get_db_cnx_string()] = pool +# # log('GetDBConnexion: created pool for "%s"' % scu.get_db_cnx_string()) +# cnx = pool.getconn(key=(thread.get_ident(), autocommit)) +# # log('GetDBConnexion: autocommit=%s cnx=%s' % (autocommit,cnx)) +# if cnx.autocommit != autocommit: +# cnx.autocommit = autocommit +# return cnx -def GetDBConnexion(autocommit=True): - """connexion to the DB of a departement""" - pool = _pools.get(scu.get_db_cnx_string(), None) - if not pool: - pool = psycopg2.pool.ThreadedConnectionPool(2, 8, dsn=scu.get_db_cnx_string()) - _pools[scu.get_db_cnx_string()] = pool - # log('GetDBConnexion: created pool for "%s"' % scu.get_db_cnx_string()) - cnx = pool.getconn(key=(thread.get_ident(), autocommit)) - # log('GetDBConnexion: autocommit=%s cnx=%s' % (autocommit,cnx)) - if cnx.autocommit != autocommit: - cnx.autocommit = autocommit - return cnx +def open_dept_connection(): + """Open a connection to the current dept db""" + # log("open_dept_connection to " + scu.get_db_cnx_string()) # XXX + return psycopg2.connect(scu.get_db_cnx_string()) + + +def close_dept_connection(): + """Commit and close dept db.""" + # log("close_dept_connection to " + scu.get_db_cnx_string()) # XXX + g.db_conn.commit() + g.db_conn.close() + + +# Essai bien plus simple pour Flask: +def GetDBConnexion(autocommit=True): # on n'utilise plus autocommit + return g.db_conn # Same for users: diff --git a/app/scodoc/sco_abs.py b/app/scodoc/sco_abs.py index 7090f0766e..3b9a8d24e1 100644 --- a/app/scodoc/sco_abs.py +++ b/app/scodoc/sco_abs.py @@ -32,6 +32,7 @@ import string import time +import types import datetime import calendar import cgi @@ -60,7 +61,8 @@ def _isFarFutur(jour): def _toboolean(x): "convert a value to boolean" - return x # not necessary anymore ! + assert (x is None) or isinstance(x, types.IntType) # sco8 + return bool(x) def is_work_saturday(context): @@ -316,7 +318,13 @@ WHERE A.ETUDID = %(etudid)s + """ AND A.JOUR BETWEEN %(debut)s AND %(fin)s """, - {"etudid": etudid, "debut": debut, "fin": fin, "moduleimpl_id": moduleimpl_id}, + { + "etudid": etudid, + "debut": debut, + "fin": fin, + "matin": matin, + "moduleimpl_id": moduleimpl_id, + }, ) res = cursor.dictfetchall() return res diff --git a/app/static/js/abs_ajax.js b/app/static/js/abs_ajax.js index 4e5aee1a0d..4f67294f95 100644 --- a/app/static/js/abs_ajax.js +++ b/app/static/js/abs_ajax.js @@ -28,7 +28,7 @@ function ajaxFunction(mod, etudid, dat) { document.getElementById("AjaxDiv").innerHTML = ajaxRequest.responseText; } } - ajaxRequest.open("POST", SCO_URL + "Absences/doSignaleAbsenceGrSemestre", true); + ajaxRequest.open("POST", SCO_URL + "/Absences/doSignaleAbsenceGrSemestre", true); ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); var oSelectOne = $("#abs_form")[0].elements["moduleimpl_id"]; var index = oSelectOne.selectedIndex; diff --git a/app/static/js/etud_debouche.js b/app/static/js/etud_debouche.js index da0febd66b..fbcdd1977d 100644 --- a/app/static/js/etud_debouche.js +++ b/app/static/js/etud_debouche.js @@ -15,7 +15,7 @@ function display_itemsuivis(active) { if (!readonly) { $('#adddebouchelink').off("click").click(function (e) { e.preventDefault(); - $.post(SCO_URL + "itemsuivi_create", { etudid: etudid, format: 'json' }).done(item_insert_new); + $.post(SCO_URL + "/itemsuivi_create", { etudid: etudid, format: 'json' }).done(item_insert_new); return false; }); diff --git a/app/static/js/groupmgr.js b/app/static/js/groupmgr.js index 90a50272f9..7d16adc9b1 100644 --- a/app/static/js/groupmgr.js +++ b/app/static/js/groupmgr.js @@ -19,7 +19,7 @@ function loadGroupes() { $("#gmsg")[0].innerHTML = 'Chargement des groupes en cours...'; var partition_id = document.formGroup.partition_id.value; - $.get(SCO_URL + 'XMLgetGroupsInPartition', { partition_id: partition_id }) + $.get(SCO_URL + '/XMLgetGroupsInPartition', { partition_id: partition_id }) .done( function (data) { var nodes = data.getElementsByTagName('group'); diff --git a/app/static/js/saisie_notes.js b/app/static/js/saisie_notes.js index b1907df4ea..5fc09a1266 100644 --- a/app/static/js/saisie_notes.js +++ b/app/static/js/saisie_notes.js @@ -41,7 +41,7 @@ function save_note(elem, v, etudid) { var evaluation_id = $("#formnotes_evaluation_id").attr("value"); var formsemestre_id = $("#formnotes_formsemestre_id").attr("value"); $('#sco_msg').html("en cours...").show(); - $.post(SCO_URL + 'Notes/save_note', + $.post(SCO_URL + '/Notes/save_note', { 'etudid': etudid, 'evaluation_id': evaluation_id, diff --git a/app/views/notes.py b/app/views/notes.py index 07b990d176..0b27a9aadb 100644 --- a/app/views/notes.py +++ b/app/views/notes.py @@ -132,12 +132,14 @@ import app.scodoc.VERSION as VERSION context = ScoDoc7Context("notes") -def sco_publish(route, function, permission): +def sco_publish(route, function, permission, methods=["GET"]): """Declare a route for a python function, protected by permission and called following ScoDoc 7 Zope standards. """ # f = - bp.route(route)(permission_required(permission)(scodoc7func(context)(function))) + bp.route(route, methods=methods)( + permission_required(permission)(scodoc7func(context)(function)) + ) # setattr(sys.modules[__name__], f.__name__, f) @@ -1563,7 +1565,12 @@ sco_publish( Permission.ScoEnsView, ) sco_publish("/saisie_notes", sco_saisie_notes.saisie_notes, Permission.ScoEnsView) -sco_publish("/save_note", sco_saisie_notes.save_note, Permission.ScoEnsView) +sco_publish( + "/save_note", + sco_saisie_notes.save_note, + Permission.ScoEnsView, + methods=["GET", "POST"], +) sco_publish( "/do_evaluation_set_missing", sco_saisie_notes.do_evaluation_set_missing, diff --git a/app/views/scolar.py b/app/views/scolar.py index 1b0bac7bf7..691e86dfda 100644 --- a/app/views/scolar.py +++ b/app/views/scolar.py @@ -1011,7 +1011,7 @@ def _do_cancel_dem_or_def( return REQUEST.RESPONSE.redirect("ficheEtud?etudid=%s" % etudid) -@bp.route("/etudident_create_form") +@bp.route("/etudident_create_form", methods=["GET", "POST"]) @permission_required(Permission.ScoEtudInscrit) @scodoc7func(context) def etudident_create_form(context, REQUEST=None): @@ -1019,7 +1019,7 @@ def etudident_create_form(context, REQUEST=None): return _etudident_create_or_edit_form(context, REQUEST, edit=False) -@bp.route("/etudident_edit_form") +@bp.route("/etudident_edit_form", methods=["GET", "POST"]) @permission_required(Permission.ScoEtudInscrit) @scodoc7func(context) def etudident_edit_form(context, REQUEST=None):