# -*- mode: python -*- # -*- coding: utf-8 -*- ############################################################################## # # ScoDoc # # Copyright (c) 1999 - 2021 Emmanuel Viennet. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Emmanuel Viennet emmanuel.viennet@viennet.net # ############################################################################## """ Module main: essais divers Emmanuel Viennet, 2021 """ import pprint from pprint import pprint as pp import functools import thread # essai from zipfile import ZipFile from StringIO import StringIO import flask from flask import request, render_template, redirect from flask_login import login_required from app.main import bp from app.decorators import scodoc7func, admin_required from app.scodoc import VERSION context = None # temporaire pour #sco8 # ------------------------------------------------------------- # # ESSAIS DIVERS pour #sco8 # # ------------------------------------------------------------- @bp.route("/") @bp.route("/index") def index(): return render_template( "main/index.html", title=u"Essai Flask", current_app=flask.current_app ) @bp.route("/test_vue") @login_required def test_vue(): return """Vous avez vu. Retour à l'accueil""" def get_request_infos(): return [ "

request.base_url=%s

" % request.base_url, "

request.url_root=%s

" % request.url_root, "

request.query_string=%s

" % request.query_string, ] D = {"count": 0} # @app.route("/") # @app.route("/index") # def index(): # sleep(8) # D["count"] = D.get("count", 0) + 1 # return "Hello, World! %s count=%s" % (thread.get_ident(), D["count"]) @bp.route("/zopefunction", methods=["POST", "GET"]) @login_required @scodoc7func(context) def a_zope_function(y, x="defaut", REQUEST=None): """Une fonction typique de ScoDoc7""" H = get_request_infos() + [ "

x=%s

" % x, "

y=%s

" % y, "

URL=%s

" % REQUEST.URL, "

QUERY_STRING=%s

" % REQUEST.QUERY_STRING, "

AUTHENTICATED_USER=%s

" % REQUEST.AUTHENTICATED_USER, ] H.append("

form=%s

" % REQUEST.form) H.append("

form[x]=%s

" % REQUEST.form.get("x", "non fourni")) return "\n".join(H) @bp.route("/zopeform_get") @scodoc7func(context) def a_zope_form_get(REQUEST=None): H = [ """

Formulaire GET

x :
y :
fichier :
""" % flask.url_for("main.a_zope_function") ] return "\n".join(H) @bp.route("/zopeform_post") @scodoc7func(context) def a_zope_form_post(REQUEST=None): H = [ """

Formulaire POST

x :
y :
fichier :
""" % flask.url_for("main.a_zope_function") ] return "\n".join(H) @bp.route("/ScoDoc//Scolarite/Notes/formsemestre_statux") @scodoc7func(context) def formsemestre_statux(dept_id=None, formsemestre_id=None, REQUEST=None): """Essai méthode de département Le contrôle d'accès doit vérifier les bons rôles : ici Ens """ return u"""dept_id=%s , formsemestre_id=%s Retour à l'accueil""" % ( dept_id, formsemestre_id, ) @bp.route("/hello/world") def hello(): H = get_request_infos() + [ "

Hello, World! %s count=%s

" % (thread.get_ident(), D["count"]), ] # print(pprint.pformat(dir(request))) return "\n".join(H) @bp.route("/getzip") def getzip(): """Essai renvoi d'un ZIP en Flask""" # La version Zope: # REQUEST.RESPONSE.setHeader("content-type", "application/zip") # REQUEST.RESPONSE.setHeader("content-length", size) # REQUEST.RESPONSE.setHeader( # "content-disposition", 'attachement; filename="monzip.zip"' # ) zipdata = StringIO() zipfile = ZipFile(zipdata, "w") zipfile.writestr("fichier1", "un contenu") zipfile.writestr("fichier2", "deux contenus") zipfile.close() data = zipdata.getvalue() size = len(data) # open("/tmp/toto.zip", "w").write(data) # Flask response: r = flask.Response(response=data, status=200, mimetype="application/zip") r.headers["Content-Type"] = "application/zip" r.headers["content-length"] = size r.headers["content-disposition"] = 'attachement; filename="monzip.zip"' return r