# -*- coding: UTF-8 -* 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 @bp.route("/") @bp.route("/index") def index(): return render_template("main/index.html", title=u"Essai Flask") @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 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 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 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_status") @scodoc7func def formsemestre_status(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