1
0
forked from ScoDoc/ScoDoc
ScoDoc/app/scodoc/sco_formsemestre_custommenu.py

162 lines
5.6 KiB
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
2021-01-01 17:51:08 +01:00
# Copyright (c) 1999 - 2021 Emmanuel Viennet. All rights reserved.
2020-09-26 16:19:37 +02:00
#
# 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
#
##############################################################################
"""Menu "custom" (défini par l'utilisateur) dans les semestres
"""
2021-08-01 10:16:16 +02:00
import flask
2021-08-15 22:08:38 +02:00
from flask import g, url_for
2020-09-26 16:19:37 +02:00
import app.scodoc.sco_utils as scu
import app.scodoc.notesdb as ndb
from app.scodoc.TrivialFormulator import TrivialFormulator
from app.scodoc import html_sco_header
from app.scodoc import htmlutils
from app.scodoc import sco_formsemestre
from app.scodoc import sco_edt_cal
2020-09-26 16:19:37 +02:00
2021-02-03 22:00:41 +01:00
_custommenuEditor = ndb.EditableTable(
2020-09-26 16:19:37 +02:00
"notes_formsemestre_custommenu",
"custommenu_id",
("custommenu_id", "formsemestre_id", "title", "url", "idx"),
sortkey="idx",
)
notes_formsemestre_custommenu_create = _custommenuEditor.create
notes_formsemestre_custommenu_list = _custommenuEditor.list
notes_formsemestre_custommenu_edit = _custommenuEditor.edit
def formsemestre_custommenu_get(context, formsemestre_id):
"returns dict [ { 'title' : xxx, 'url' : xxx } ]"
2021-06-15 13:59:56 +02:00
cnx = ndb.GetDBConnexion()
2020-09-26 16:19:37 +02:00
vals = notes_formsemestre_custommenu_list(cnx, {"formsemestre_id": formsemestre_id})
return vals
2021-06-14 18:08:52 +02:00
def formsemestre_custommenu_html(context, formsemestre_id):
2020-09-26 16:19:37 +02:00
"HTML code for custom menu"
menu = []
# Calendrier électronique ?
2021-08-19 10:28:35 +02:00
sem = sco_formsemestre.get_formsemestre(formsemestre_id)
2020-09-26 16:19:37 +02:00
ics_url = sco_edt_cal.formsemestre_get_ics_url(context, sem)
if ics_url:
menu.append({"title": "Emploi du temps (ics)", "url": ics_url})
menu += formsemestre_custommenu_get(context, formsemestre_id)
menu.append(
{
"title": "Modifier ce menu...",
2021-06-14 18:08:52 +02:00
"endpoint": "notes.formsemestre_custommenu_edit",
"args": {"formsemestre_id": formsemestre_id},
2020-09-26 16:19:37 +02:00
}
)
return htmlutils.make_menu("Liens", menu)
2020-09-26 16:19:37 +02:00
def formsemestre_custommenu_edit(context, formsemestre_id, REQUEST=None):
"""Dialog to edit the custom menu"""
2021-08-19 10:28:35 +02:00
sem = sco_formsemestre.get_formsemestre(formsemestre_id)
2021-02-07 09:10:26 +01:00
dest_url = (
2021-06-15 12:34:33 +02:00
scu.NotesURL() + "/formsemestre_status?formsemestre_id=%s" % formsemestre_id
2021-02-07 09:10:26 +01:00
)
2020-09-26 16:19:37 +02:00
H = [
2021-06-14 18:08:52 +02:00
html_sco_header.html_sem_header(
context, REQUEST, "Modification du menu du semestre ", sem
),
2020-09-26 16:19:37 +02:00
"""<p class="help">Ce menu, spécifique à chaque semestre, peut être utilisé pour placer des liens vers vos applications préférées.</p>
<p class="help">Procédez en plusieurs fois si vous voulez ajouter plusieurs items.</p>""",
]
descr = [
("formsemestre_id", {"input_type": "hidden"}),
(
"sep",
{
"input_type": "separator",
"template": "<tr><td><b>Titre</b></td><td><b>URL</b></td></tr>",
},
),
]
menu = formsemestre_custommenu_get(context, formsemestre_id)
menu.append({"custommenu_id": "new", "url": "", "title": ""})
initvalues = {}
for item in menu:
descr.append(
(
2021-08-15 22:08:38 +02:00
"title_" + str(item["custommenu_id"]),
2020-09-26 16:19:37 +02:00
{"size": 40, "template": '<tr><td class="tf-field">%(elem)s</td>'},
)
)
descr.append(
(
2021-08-15 22:08:38 +02:00
"url_" + str(item["custommenu_id"]),
2020-09-26 16:19:37 +02:00
{"size": 80, "template": '<td class="tf-field">%(elem)s</td></tr>'},
)
)
2021-08-15 22:08:38 +02:00
initvalues["title_" + str(item["custommenu_id"])] = item["title"]
initvalues["url_" + str(item["custommenu_id"])] = item["url"]
2020-09-26 16:19:37 +02:00
tf = TrivialFormulator(
REQUEST.URL0,
REQUEST.form,
descr,
initvalues=initvalues,
cancelbutton="Annuler",
method="GET",
submitlabel="Enregistrer",
name="tf",
)
if tf[0] == 0:
return "\n".join(H) + "\n" + tf[1] + html_sco_header.sco_footer()
2020-09-26 16:19:37 +02:00
elif tf[0] == -1:
2021-07-31 18:01:10 +02:00
return flask.redirect(dest_url)
2020-09-26 16:19:37 +02:00
else:
# form submission
2021-06-15 13:59:56 +02:00
cnx = ndb.GetDBConnexion()
2020-09-26 16:19:37 +02:00
# add new
if tf[2]["title_new"]:
notes_formsemestre_custommenu_create(
cnx,
{
"formsemestre_id": formsemestre_id,
"title": tf[2]["title_new"],
"url": tf[2]["url_new"],
},
)
# edit existings
s = "title_"
for x in tf[2].keys():
if x[: len(s)] == s and x != "title_new":
custommenu_id = x[len(s) :]
notes_formsemestre_custommenu_edit(
cnx,
{
"custommenu_id": custommenu_id,
"title": tf[2]["title_" + custommenu_id],
"url": tf[2]["url_" + custommenu_id],
},
)
2021-07-31 18:01:10 +02:00
return flask.redirect(dest_url)