114 lines
2.6 KiB
Python
114 lines
2.6 KiB
Python
# -*- coding: UTF-8 -*
|
|
"""
|
|
Module Essais: divers essais pour la migration vers Flask
|
|
|
|
Emmanuel Viennet, 2021
|
|
"""
|
|
|
|
import flask
|
|
|
|
from flask import g
|
|
from flask import current_app
|
|
from flask import render_template
|
|
from flask import request
|
|
from flask import url_for
|
|
|
|
from app.decorators import (
|
|
scodoc,
|
|
scodoc7func,
|
|
ScoDoc7Context,
|
|
permission_required,
|
|
admin_required,
|
|
login_required,
|
|
)
|
|
from app.auth.models import Permission
|
|
|
|
from app.views import essais_bp as bp
|
|
|
|
from app.scodoc import sco_cache
|
|
from app.scodoc import sco_exceptions
|
|
|
|
context = ScoDoc7Context(globals())
|
|
|
|
|
|
@bp.route("/<scodoc_dept>/Scolarite/sco_exemple")
|
|
@scodoc
|
|
@scodoc7func(context)
|
|
def sco_exemple(etudid="NON"):
|
|
"""Un exemple de fonction ScoDoc 7"""
|
|
return """<html>
|
|
<body><h1>ScoDoc 7 rules !</h1>
|
|
<p>etudid=%(etudid)s</p>
|
|
<p>g.scodoc_dept=%(scodoc_dept)s</p>
|
|
</body>
|
|
</html>
|
|
""" % {
|
|
"etudid": etudid,
|
|
"scodoc_dept": g.scodoc_dept,
|
|
}
|
|
|
|
|
|
# En ScoDoc 7, on avait des vues qui en appellaient d'autres
|
|
# avec context.sco_exemple( etudid="E12" )
|
|
# @bp.route("/<scodoc_dept>/Scolarite/sco_exemple2")
|
|
# @login_required
|
|
# @scodoc7func(context)
|
|
# def sco_exemple2():
|
|
# return "Exemple 2" + context.sco_exemple(etudid="deux")
|
|
|
|
|
|
@bp.route("/<scodoc_dept>/Scolarite/sco_exemple3")
|
|
@scodoc
|
|
@login_required
|
|
@scodoc7func(context)
|
|
def sco_exemple3(toto):
|
|
return "Exemple 3: toto=" + toto
|
|
|
|
|
|
@bp.route("/<scodoc_dept>/Scolarite/sco_exemple4")
|
|
@scodoc
|
|
@login_required
|
|
@scodoc7func(context)
|
|
def sco_exemple4(toto):
|
|
return "Exemple 4: " + sco_exemple3(toto)
|
|
|
|
|
|
# Test avec un seul argument REQUEST positionnel
|
|
@bp.route("/<scodoc_dept>/Scolarite/sco_get_version")
|
|
@scodoc
|
|
@scodoc7func(context)
|
|
def sco_get_version(REQUEST):
|
|
return "ok"
|
|
|
|
|
|
# Fonction ressemblant à une méthode Zope protégée
|
|
@bp.route("/<scodoc_dept>/Scolarite/sco_test_view")
|
|
@scodoc
|
|
@permission_required(Permission.ScoView)
|
|
@scodoc7func(context)
|
|
def sco_test_view(REQUEST=None):
|
|
return """Vous avez vu sco_test_view !"""
|
|
|
|
|
|
@bp.route("/essrep")
|
|
def essrep():
|
|
return flask.Response(status=200, response="Bonjour pépé %s" + u"papa")
|
|
|
|
|
|
# Tests formulaires avec checkbox et GET
|
|
@bp.route("/testcheckbox", methods=["GET"])
|
|
def testcheckbox():
|
|
# args = request.args
|
|
return render_template("essais/testcheckbox.html")
|
|
|
|
|
|
# Test exception
|
|
@bp.route("/test_exception_value_error", methods=["GET"])
|
|
def test_exception_value_error():
|
|
raise sco_exceptions.ScoValueError(
|
|
"hello",
|
|
dest_url=url_for(
|
|
"notes.formsemestre_status", scodoc_dept="RT", formsemestre_id="SEM38882"
|
|
),
|
|
)
|