DocScoDoc/app/main/routes.py

188 lines
5.5 KiB
Python

# -*- 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. <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(context)
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(context)
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(context)
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_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<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