forked from ScoDoc/ScoDoc
144 lines
4.1 KiB
Python
144 lines
4.1 KiB
Python
|
# -*- 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. <a href="/">Retour à l'accueil</a>"""
|
||
|
|
||
|
|
||
|
def get_request_infos():
|
||
|
return [
|
||
|
"<p>request.base_url=%s</p>" % request.base_url,
|
||
|
"<p>request.url_root=%s</p>" % request.url_root,
|
||
|
"<p>request.query_string=%s</p>" % 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() + [
|
||
|
"<p><b>x=<tt>%s</tt></b></p>" % x,
|
||
|
"<p><b>y=<tt>%s</tt></b></p>" % y,
|
||
|
"<p><b>URL=<tt>%s</tt></b></p>" % REQUEST.URL,
|
||
|
"<p><b>QUERY_STRING=<tt>%s</tt></b></p>" % REQUEST.QUERY_STRING,
|
||
|
"<p><b>AUTHENTICATED_USER=<tt>%s</tt></b></p>" % REQUEST.AUTHENTICATED_USER,
|
||
|
]
|
||
|
H.append("<p><b>form=<tt>%s</tt></b></p>" % REQUEST.form)
|
||
|
H.append("<p><b>form[x]=<tt>%s</tt></b></p>" % REQUEST.form.get("x", "non fourni"))
|
||
|
|
||
|
return "\n".join(H)
|
||
|
|
||
|
|
||
|
@bp.route("/zopeform_get")
|
||
|
@scodoc7func
|
||
|
def a_zope_form_get(REQUEST=None):
|
||
|
H = [
|
||
|
"""<h2>Formulaire GET</h2>
|
||
|
<form action="%s" method="get">
|
||
|
x : <input type="text" name="x"/><br/>
|
||
|
y : <input type="text" name="y"/><br/>
|
||
|
fichier : <input type="file" name="fichier"/><br/>
|
||
|
<input type="submit" value="Envoyer"/>
|
||
|
</form>
|
||
|
"""
|
||
|
% flask.url_for("main.a_zope_function")
|
||
|
]
|
||
|
return "\n".join(H)
|
||
|
|
||
|
|
||
|
@bp.route("/zopeform_post")
|
||
|
@scodoc7func
|
||
|
def a_zope_form_post(REQUEST=None):
|
||
|
H = [
|
||
|
"""<h2>Formulaire POST</h2>
|
||
|
<form action="%s" method="post" enctype="multipart/form-data">
|
||
|
x : <input type="text" name="x"/><br/>
|
||
|
y : <input type="text" name="y"/><br/>
|
||
|
fichier : <input type="file" name="fichier"/><br/>
|
||
|
<input type="submit" value="Envoyer"/>
|
||
|
</form>
|
||
|
"""
|
||
|
% flask.url_for("main.a_zope_function")
|
||
|
]
|
||
|
return "\n".join(H)
|
||
|
|
||
|
|
||
|
@bp.route("/ScoDoc/<dept_id>/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<dept_id>
|
||
|
"""
|
||
|
return u"""dept_id=%s , formsemestre_id=%s <a href="/">Retour à l'accueil</a>""" % (
|
||
|
dept_id,
|
||
|
formsemestre_id,
|
||
|
)
|
||
|
|
||
|
|
||
|
@bp.route("/hello/world")
|
||
|
def hello():
|
||
|
H = get_request_infos() + [
|
||
|
"<p>Hello, World! %s count=%s</p>" % (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
|