From 64615036eccefc803f5d91c35cea2dab5d4cf8b9 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 21 Jun 2021 14:40:58 +0200 Subject: [PATCH] WIP: fix http redirects, fix some JS urls --- app/decorators.py | 8 ++- app/scodoc/html_sco_header.py | 2 + app/scodoc/sco_evaluations.py | 1 + app/scodoc/sco_photos.py | 21 ++++--- app/static/js/etud_info.js | 32 +++++------ app/static/js/groups_view.js | 100 +++++++++++++++++----------------- app/views/essais.py | 8 +++ app/views/notes.py | 4 +- app/views/scolar.py | 2 +- 9 files changed, 99 insertions(+), 79 deletions(-) diff --git a/app/decorators.py b/app/decorators.py index 5199f85ed..f7bd46d4f 100644 --- a/app/decorators.py +++ b/app/decorators.py @@ -7,6 +7,8 @@ import inspect import types import logging +import werkzeug +from werkzeug.exceptions import BadRequest import flask from flask import g from flask import abort, current_app @@ -14,7 +16,6 @@ from flask import request from flask_login import current_user from flask_login import login_required from flask import current_app -from werkzeug.exceptions import BadRequest from app.auth.models import Permission @@ -80,7 +81,8 @@ class ZResponse(object): self.headers = {} def redirect(self, url): - return flask.redirect(url) # http 302 + current_app.logger.debug("ZResponse redirect to:" + str(url)) + return flask.redirect(url.decode("utf-8")) # http 302 # #sco8 unicode def setHeader(self, header, value): self.headers[header.lower()] = value @@ -188,6 +190,8 @@ def scodoc7func(context): if not top_level: return value else: + if isinstance(value, werkzeug.wrappers.response.Response): + return value # redirected # Build response, adding collected http headers: headers = [] kw = {"response": value, "status": 200} diff --git a/app/scodoc/html_sco_header.py b/app/scodoc/html_sco_header.py index a22275a33..72fb42d8e 100644 --- a/app/scodoc/html_sco_header.py +++ b/app/scodoc/html_sco_header.py @@ -231,6 +231,8 @@ def sco_header( """ % params ) diff --git a/app/scodoc/sco_evaluations.py b/app/scodoc/sco_evaluations.py index 76de15f2a..2383e3d60 100644 --- a/app/scodoc/sco_evaluations.py +++ b/app/scodoc/sco_evaluations.py @@ -248,6 +248,7 @@ def do_evaluation_create( evaluation_type=None, numero=None, REQUEST=None, + **kw # ceci pour absorber les arguments excedentaires de tf #sco8 ): """Create an evaluation""" if not sco_permissions_check.can_edit_evaluation( diff --git a/app/scodoc/sco_photos.py b/app/scodoc/sco_photos.py index 480f2f708..5e20a8e80 100644 --- a/app/scodoc/sco_photos.py +++ b/app/scodoc/sco_photos.py @@ -53,15 +53,17 @@ from PIL import Image as PILImage from cStringIO import StringIO import glob +from flask import request + from config import Config -import app.scodoc.sco_utils as scu -import app.scodoc.notesdb as ndb -from app.scodoc.notes_log import log -from app.scodoc.scolog import logdb +from app.scodoc import sco_etud from app.scodoc import sco_portal_apogee from app.scodoc import sco_preferences -from app.scodoc import sco_etud +from app.scodoc.notes_log import log +from app.scodoc.scolog import logdb +import app.scodoc.notesdb as ndb +import app.scodoc.sco_utils as scu # Full paths on server's filesystem. Something like "/opt/scodoc/var/scodoc/photos" PHOTO_DIR = os.path.join(Config.INSTANCE_HOME, "var", "scodoc", "photos") @@ -89,7 +91,10 @@ def etud_photo_url(context, etud, size="small", fast=False, REQUEST=None): """url to the image of the student, in "small" size or "orig" size. If ScoDoc doesn't have an image and a portal is configured, link to it. """ - photo_url = "get_photo_image?etudid=%s&size=%s" % (etud["etudid"], size) + photo_url = scu.ScoURL() + "/get_photo_image?etudid=%s&size=%s" % ( + etud["etudid"], + size, + ) if fast: return photo_url path = photo_pathname(context, etud, size=size) @@ -98,7 +103,7 @@ def etud_photo_url(context, etud, size="small", fast=False, REQUEST=None): ext_url = photo_portal_url(context, etud) if not ext_url: # fallback: Photo "unknown" - photo_url = UNKNOWN_IMAGE_URL + photo_url = scu.ScoURL() + "/" + UNKNOWN_IMAGE_URL else: # essaie de copier la photo du portail new_path, _ = copy_portal_photo_to_fs(context, etud, REQUEST=REQUEST) @@ -141,7 +146,7 @@ def _http_jpeg_file(context, filename, REQUEST=None): RESPONSE.setHeader("Last-Modified", last_modified_str) RESPONSE.setHeader("Cache-Control", "max-age=3600") RESPONSE.setHeader("Content-Length", str(file_size)) - header = REQUEST.get_header("If-Modified-Since", None) + header = request.headers.get("If-Modified-Since") if header is not None: header = header.split(";")[0] # Some proxies seem to send invalid date strings for this diff --git a/app/static/js/etud_info.js b/app/static/js/etud_info.js index 1b2a7e500..f1ab66c52 100644 --- a/app/static/js/etud_info.js +++ b/app/static/js/etud_info.js @@ -13,14 +13,14 @@ function get_etudid_from_elem(e) { } } -$().ready(function(){ +$().ready(function () { var elems = $(".etudinfo"); var q_args = get_query_args(); var args_to_pass = new Set( - [ "formsemestre_id", "group_ids","group_id", "partition_id", - "moduleimpl_id", "evaluation_id" + ["formsemestre_id", "group_ids", "group_id", "partition_id", + "moduleimpl_id", "evaluation_id" ]); var qs = ""; for (var k in q_args) { @@ -28,11 +28,11 @@ $().ready(function(){ qs += '&' + k + '=' + q_args[k]; } } - for (var i=0; i < elems.length; i++) { - $(elems[i]).qtip({ - content: { - ajax: { - url: "etud_info_html?etudid=" + get_etudid_from_elem(elems[i]) + qs, + for (var i = 0; i < elems.length; i++) { + $(elems[i]).qtip({ + content: { + ajax: { + url: SCO_URL + "/etud_info_html?etudid=" + get_etudid_from_elem(elems[i]) + qs, type: "GET" //success: function(data, status) { // this.set('content.text', data); @@ -40,14 +40,14 @@ $().ready(function(){ // console.log(status); } }, - text: "Loading...", - position: { - at: "right bottom", - my: "left top" - }, - style: { - classes: 'qtip-etud' - }, + text: "Loading...", + position: { + at: "right bottom", + my: "left top" + }, + style: { + classes: 'qtip-etud' + }, hide: { fixed: true, delay: 300 diff --git a/app/static/js/groups_view.js b/app/static/js/groups_view.js index e050e40ed..54476d5ff 100644 --- a/app/static/js/groups_view.js +++ b/app/static/js/groups_view.js @@ -1,11 +1,11 @@ // Affichage progressif du trombinoscope html -$().ready(function(){ +$().ready(function () { var spans = $(".unloaded_img"); - for (var i=0; i < spans.length; i++) { - var sp = spans[i]; - var etudid = sp.id; - $(sp).load('etud_photo_html?etudid='+etudid); + for (var i = 0; i < spans.length; i++) { + var sp = spans[i]; + var etudid = sp.id; + $(sp).load(SCO_URL + '/etud_photo_html?etudid=' + etudid); } }); @@ -25,7 +25,7 @@ function groups_view_url() { var selected_groups = $("#group_selector select").val(); url.param()['group_ids'] = selected_groups; // remplace par groupes selectionnes - + return url; } @@ -34,16 +34,16 @@ function select_tous() { var url = groups_view_url(); var default_group_id = $("#group_selector")[0].default_group_id.value; delete url.param()['group_ids']; - url.param()['group_ids'] = [ default_group_id ]; + url.param()['group_ids'] = [default_group_id]; - var query_string = $.param(url.param(), traditional=true ); + var query_string = $.param(url.param(), traditional = true); window.location = url.attr('base') + url.attr('path') + '?' + query_string; } // L'URL pour l'état courant de la page: function get_current_url() { var url = groups_view_url(); - var query_string = $.param(url.param(), traditional=true ); + var query_string = $.param(url.param(), traditional = true); return url.attr('base') + url.attr('path') + '?' + query_string; } @@ -53,12 +53,12 @@ function submit_group_selector() { } function show_current_tab() { - $('.nav-tabs [href="#'+CURRENT_TAB_HASH+'"]').tab('show'); + $('.nav-tabs [href="#' + CURRENT_TAB_HASH + '"]').tab('show'); } var CURRENT_TAB_HASH = $.url().param()['curtab']; -$().ready(function(){ +$().ready(function () { $('.nav-tabs a').on('shown.bs.tab', function (e) { CURRENT_TAB_HASH = e.target.hash.slice(1); // sans le # }); @@ -69,15 +69,15 @@ $().ready(function(){ function change_list_options() { var url = groups_view_url(); var selected_options = $("#group_list_options").val(); - var options = [ "with_paiement", "with_archives", "with_annotations", "with_codes" ]; - for (var i=0; i= 0) { + if ($.inArray(option, selected_options) >= 0) { url.param()[option] = 1; } } - var query_string = $.param(url.param(), traditional=true ); + var query_string = $.param(url.param(), traditional = true); window.location = url.attr('base') + url.attr('path') + '?' + query_string; } @@ -86,45 +86,45 @@ function toggle_visible_etuds() { // $(".etud_elem").hide(); var qargs = ""; - $("#group_ids_sel option:selected").each( function(index, opt) { + $("#group_ids_sel option:selected").each(function (index, opt) { var group_id = opt.value; - $("."+group_id).show(); + $("." + group_id).show(); qargs += "&group_ids=" + group_id; }); // Update url saisie tableur: var input_eval = $("#formnotes_evaluation_id"); if (input_eval.length > 0) { var evaluation_id = input_eval[0].value; - $("#menu_saisie_tableur a").attr("href", "saisie_notes_tableur?evaluation_id=" + evaluation_id + qargs ); + $("#menu_saisie_tableur a").attr("href", "saisie_notes_tableur?evaluation_id=" + evaluation_id + qargs); // lien feuille excel: - $("#lnk_feuille_saisie").attr("href", "feuille_saisie_notes?evaluation_id=" + evaluation_id + qargs ); + $("#lnk_feuille_saisie").attr("href", "feuille_saisie_notes?evaluation_id=" + evaluation_id + qargs); } } -$().ready(function() { +$().ready(function () { $('#group_ids_sel').multiselect( { includeSelectAllOption: false, - nonSelectedText:'choisir...', + nonSelectedText: 'choisir...', // buttonContainer: '
', - onChange: function(element, checked){ + onChange: function (element, checked) { if (checked == true) { var default_group_id = $(".default_group")[0].value; - + if (element.hasClass("default_group")) { // click sur groupe "tous" // deselectionne les autres - $("#group_ids_sel option:selected").each( function(index, opt) { + $("#group_ids_sel option:selected").each(function (index, opt) { if (opt.value != default_group_id) { $("#group_ids_sel").multiselect('deselect', opt.value); } }); - + } else { // click sur un autre item // si le groupe "tous" est selectionne et que l'on coche un autre, le deselectionner var default_is_selected = false; - $("#group_ids_sel option:selected").each( function(index, opt) { + $("#group_ids_sel option:selected").each(function (index, opt) { if (opt.value == default_group_id) { default_is_selected = true; return false; @@ -133,13 +133,13 @@ $().ready(function() { if (default_is_selected) { $("#group_ids_sel").multiselect('deselect', default_group_id); } - } - } + } + } toggle_visible_etuds(); // referme le menu apres chaque choix: $("#group_selector .btn-group").removeClass('open'); - + if ($("#group_ids_sel").hasClass("submit_on_change")) { submit_group_selector(); } @@ -152,28 +152,28 @@ $().ready(function() { }); // Trombinoscope -$().ready(function(){ +$().ready(function () { var elems = $(".trombi-photo"); - for (var i=0; i < elems.length; i++) { - $(elems[i]).qtip( - { - content: { - ajax: { - url: "etud_info_html?with_photo=0&etudid=" + get_etudid_from_elem(elems[i]) - }, - text: "Loading..." - }, - position: { - at : "right", - my : "left top" - }, - style: { - classes: 'qtip-etud' - }, - // utile pour debugguer le css: - // hide: { event: 'unfocus' } - } - ); + for (var i = 0; i < elems.length; i++) { + $(elems[i]).qtip( + { + content: { + ajax: { + url: SCO_URL + "/etud_info_html?with_photo=0&etudid=" + get_etudid_from_elem(elems[i]) + }, + text: "Loading..." + }, + position: { + at: "right", + my: "left top" + }, + style: { + classes: 'qtip-etud' + }, + // utile pour debugguer le css: + // hide: { event: 'unfocus' } + } + ); } }); diff --git a/app/views/essais.py b/app/views/essais.py index fb913233f..0a0bfde45 100644 --- a/app/views/essais.py +++ b/app/views/essais.py @@ -77,3 +77,11 @@ def sco_get_version(REQUEST): @permission_required(Permission.ScoView) def sco_test_view(REQUEST=None): return """Vous avez vu sco_test_view !""" + + +import flask + + +@bp.route("/essrep") +def essrep(): + return flask.Response(status=200, response="Bonjour") diff --git a/app/views/notes.py b/app/views/notes.py index 739200a7a..07b990d17 100644 --- a/app/views/notes.py +++ b/app/views/notes.py @@ -1482,7 +1482,7 @@ sco_publish( ) -@bp.route("/evaluation_edit") +@bp.route("/evaluation_edit", methods=["GET", "POST"]) @permission_required(Permission.ScoEnsView) @scodoc7func(context) def evaluation_edit(context, evaluation_id, REQUEST): @@ -1492,7 +1492,7 @@ def evaluation_edit(context, evaluation_id, REQUEST): ) -@bp.route("/evaluation_create") +@bp.route("/evaluation_create", methods=["GET", "POST"]) @permission_required(Permission.ScoEnsView) @scodoc7func(context) def evaluation_create(context, moduleimpl_id, REQUEST): diff --git a/app/views/scolar.py b/app/views/scolar.py index 128cd6f5a..1b0bac7bf 100644 --- a/app/views/scolar.py +++ b/app/views/scolar.py @@ -184,7 +184,7 @@ def about(context, REQUEST): # -------------------------------------------------------------------- -@bp.route("/edit_preferences") +@bp.route("/edit_preferences", methods=["GET", "POST"]) @permission_required(Permission.ScoChangePreferences) @scodoc7func(context) def edit_preferences(context, REQUEST):