# -*- 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 context = None @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(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 = [ """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