2023-09-23 04:28:19 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-05-10 20:55:19 +02:00
|
|
|
################
|
|
|
|
### UniSquat ###
|
|
|
|
################
|
|
|
|
|
2023-09-23 04:28:19 +02:00
|
|
|
# Une application pour afficher les salles libres dans les différents
|
|
|
|
# départements de l'Université de Strasbourg.
|
2022-05-10 20:55:19 +02:00
|
|
|
|
2022-05-12 15:32:12 +02:00
|
|
|
### Fichier de l'interface Web Flask ###
|
2022-05-10 20:55:19 +02:00
|
|
|
|
|
|
|
# Modules :
|
2022-05-12 14:27:16 +02:00
|
|
|
import datetime as dti
|
2022-09-02 11:39:38 +02:00
|
|
|
import pytz
|
2022-09-07 16:49:43 +02:00
|
|
|
import json
|
|
|
|
import os
|
2024-02-11 18:25:16 +01:00
|
|
|
import traceback
|
2022-05-10 20:55:19 +02:00
|
|
|
|
2022-05-12 14:27:16 +02:00
|
|
|
from flask import Flask
|
|
|
|
from flask import render_template
|
|
|
|
from flask import url_for
|
|
|
|
from flask import request
|
2022-09-06 13:49:14 +02:00
|
|
|
from flask.helpers import make_response
|
2022-05-12 14:27:16 +02:00
|
|
|
|
2022-05-10 20:55:19 +02:00
|
|
|
# Fichiers locaux :
|
|
|
|
import date_tools
|
|
|
|
import rooms_get as ro
|
|
|
|
|
2022-05-13 16:27:21 +02:00
|
|
|
# Constantes :
|
2023-09-23 04:28:19 +02:00
|
|
|
# Le maximum de départements qu'il est possible de sélectionner :
|
|
|
|
MAX_DEPT = 5
|
|
|
|
|
|
|
|
# Le nombre de jours pendant lesquels les logs sont conservés :
|
|
|
|
MAX_LOG_DAYS = 30
|
|
|
|
|
|
|
|
# Le nombre maximal de départements les plus cherchés à afficher :
|
|
|
|
MAX_LOG_DEPT = 5
|
|
|
|
|
|
|
|
# Le nombre maximal de salles favorites à afficher :
|
|
|
|
MAX_LOG_FAVS = 10
|
|
|
|
|
|
|
|
# Nombre d'utilisations à partir duquel un message d'avertissement est affiché :
|
|
|
|
PING_WARN = 500
|
|
|
|
|
|
|
|
# Contient des stats sur les salles les plus mises en favoris,
|
|
|
|
# et sur les départements les plus recherchés :
|
|
|
|
LOG_FILE = "log.json"
|
|
|
|
|
|
|
|
# Contexte constant pour les templates Jinja :
|
|
|
|
GLOBAL_CONTEXT = {}
|
|
|
|
|
|
|
|
# Le lien vers le code source :
|
|
|
|
GLOBAL_CONTEXT["SOURCE"] = "https://forge.chapril.org/Wantoo/UniSquat_Python"
|
|
|
|
|
|
|
|
# Le lien vers le site de l'organisation :
|
|
|
|
GLOBAL_CONTEXT["CREDITSLINK"] = "https://forge.chapril.org/Wantoo"
|
|
|
|
|
|
|
|
# Le nom de l'organisation :
|
|
|
|
GLOBAL_CONTEXT["CREDITSNAME"] = "Wantoo"
|
|
|
|
|
|
|
|
# Mode bavard :
|
|
|
|
GLOBAL_CONTEXT["DEBUG"] = False
|
|
|
|
|
|
|
|
# Le domaine sur lequel est hébergée l'instance officielle :
|
|
|
|
GLOBAL_CONTEXT["DOMAIN"] = "https://unisquat.alwaysdata.net"
|
|
|
|
|
|
|
|
# Timezone du serveur :
|
|
|
|
TIMEZONE = pytz.timezone("Europe/Paris")
|
|
|
|
|
|
|
|
# Globales :
|
2022-05-12 14:27:16 +02:00
|
|
|
app = Flask(__name__)
|
2023-09-23 04:28:19 +02:00
|
|
|
|
|
|
|
# Stocke les différentes requêtes faites sur /free_rooms/
|
|
|
|
# sous la forme {"timestamp":timestamp,"depts":[]} :
|
|
|
|
logs = []
|
2022-09-07 16:49:43 +02:00
|
|
|
if os.path.isfile(LOG_FILE):
|
|
|
|
with open(LOG_FILE,"r") as f:
|
2024-03-31 17:53:14 +02:00
|
|
|
try:
|
|
|
|
logs = json.loads(f.read())
|
|
|
|
# On supprime le fichier s'il est invalide :
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
os.remove(LOG_FILE)
|
2022-05-12 14:27:16 +02:00
|
|
|
|
2022-06-17 19:12:29 +02:00
|
|
|
# Fonctions :
|
2022-09-07 16:49:43 +02:00
|
|
|
def save_logs(logs):
|
2022-09-16 10:22:22 +02:00
|
|
|
"""
|
|
|
|
Sauvegarde les logs dans un fichier.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
logs : list
|
|
|
|
Liste des logs.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
None.
|
|
|
|
|
|
|
|
"""
|
2022-09-07 16:49:43 +02:00
|
|
|
with open(LOG_FILE,"w") as f:
|
|
|
|
f.write(json.dumps(logs))
|
2022-06-17 19:12:29 +02:00
|
|
|
|
2022-05-10 20:55:19 +02:00
|
|
|
@app.route("/")
|
2022-05-12 14:27:16 +02:00
|
|
|
def home() :
|
2022-05-12 22:30:10 +02:00
|
|
|
"""
|
|
|
|
Page d'accueil du site Web.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-06-17 19:12:29 +02:00
|
|
|
None
|
2022-05-12 22:30:10 +02:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
flask.render_template
|
2023-09-23 04:28:19 +02:00
|
|
|
|
2022-05-12 22:30:10 +02:00
|
|
|
"""
|
2022-06-14 12:00:33 +02:00
|
|
|
return render_template("index.html", **GLOBAL_CONTEXT)
|
2022-05-12 14:27:16 +02:00
|
|
|
|
|
|
|
@app.route("/app")
|
2022-05-10 20:55:19 +02:00
|
|
|
def select_dept() :
|
|
|
|
"""
|
|
|
|
Permet de sélectionner un ou plusieurs départements dans lesquels
|
|
|
|
chercher des salles libres.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-06-17 19:12:29 +02:00
|
|
|
None
|
2022-05-10 20:55:19 +02:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
flask.render_template
|
2023-09-23 04:28:19 +02:00
|
|
|
|
2022-05-10 20:55:19 +02:00
|
|
|
"""
|
|
|
|
dept_filen = "data/dept_list.txt"
|
|
|
|
|
|
|
|
dept_list = ro.get_depts(dept_filen)
|
2022-05-10 22:35:16 +02:00
|
|
|
|
2022-06-17 20:05:41 +02:00
|
|
|
context = {"dept_list":dept_list}
|
2022-05-10 20:55:19 +02:00
|
|
|
|
|
|
|
url_for("static", filename="style.css")
|
2022-06-17 20:05:41 +02:00
|
|
|
return render_template("dept-select.html", **context, **GLOBAL_CONTEXT)
|
2022-05-10 20:55:19 +02:00
|
|
|
|
2022-06-14 11:53:17 +02:00
|
|
|
@app.route("/stats")
|
|
|
|
def stats():
|
|
|
|
"""
|
2023-09-23 04:28:19 +02:00
|
|
|
Statistiques d'utilisation de l'instance :
|
|
|
|
Salles marquées comme favorites
|
|
|
|
Départements les plus cherchés
|
2022-06-14 11:53:17 +02:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2023-09-23 04:28:19 +02:00
|
|
|
None
|
2022-06-14 11:53:17 +02:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
flask.render_template
|
2023-09-23 04:28:19 +02:00
|
|
|
|
2022-06-14 11:53:17 +02:00
|
|
|
"""
|
|
|
|
pings = 0
|
|
|
|
counts = {}
|
2022-09-07 19:02:26 +02:00
|
|
|
counts_favs = {}
|
2023-09-23 04:28:19 +02:00
|
|
|
|
|
|
|
for log in logs :
|
|
|
|
if log["type"] == "deptcount" :
|
|
|
|
for dept in log["depts"] :
|
|
|
|
pings += 1
|
|
|
|
if dept in counts.keys() :
|
|
|
|
counts[dept] += 1
|
|
|
|
else :
|
|
|
|
counts[dept] = 1
|
|
|
|
|
|
|
|
elif log["type"] == "favs" :
|
|
|
|
for fav in log["favs"] :
|
|
|
|
fav_map = fav[0] + ";" + fav[1]
|
|
|
|
if fav_map in counts_favs.keys() :
|
|
|
|
counts_favs[fav_map][2] += 1
|
2022-09-07 19:02:26 +02:00
|
|
|
else:
|
|
|
|
fav.append(1)
|
2023-09-23 04:28:19 +02:00
|
|
|
counts_favs[fav_map] = fav
|
|
|
|
|
|
|
|
sort = [[x, counts[x]] for x in counts.keys()]
|
2022-06-14 11:53:17 +02:00
|
|
|
|
2023-09-23 04:28:19 +02:00
|
|
|
# Tri selon la valeur du deuxieme élément de la liste :
|
|
|
|
sort.sort(key = lambda x: x[1], reverse = True)
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2023-09-23 04:28:19 +02:00
|
|
|
sort_favs = [counts_favs[x] for x in counts_favs.keys()]
|
|
|
|
sort_favs.sort(key = lambda x: x[2], reverse = True)
|
2022-06-14 11:53:17 +02:00
|
|
|
|
2023-09-23 04:28:19 +02:00
|
|
|
context = {
|
|
|
|
"MAX_LOG_DAYS":MAX_LOG_DAYS,
|
|
|
|
"PING_WARN":PING_WARN,
|
|
|
|
"depts":sort[:MAX_LOG_DEPT],
|
|
|
|
"favs":sort_favs[:MAX_LOG_FAVS],
|
|
|
|
"nbping":pings
|
|
|
|
}
|
|
|
|
|
|
|
|
return render_template("stats.html", **context, **GLOBAL_CONTEXT)
|
2022-06-14 11:53:17 +02:00
|
|
|
|
2022-05-12 14:27:16 +02:00
|
|
|
@app.route("/app/free-rooms", methods=["POST", "GET"])
|
2022-09-16 10:22:22 +02:00
|
|
|
def free_rooms(api = False, rq = None) :
|
2022-05-10 20:55:19 +02:00
|
|
|
"""
|
2023-09-23 04:28:19 +02:00
|
|
|
Affiche les salles libres dans les départements sélectionnés.
|
2022-05-10 20:55:19 +02:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-09-16 10:22:22 +02:00
|
|
|
api : bool
|
|
|
|
Indique si la page est accédée par l'API.
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2022-09-16 10:22:22 +02:00
|
|
|
rq : requests.request
|
|
|
|
Requête.
|
2022-05-10 20:55:19 +02:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
flask.render_template
|
2023-09-23 04:28:19 +02:00
|
|
|
|
2022-05-10 20:55:19 +02:00
|
|
|
"""
|
2022-09-16 10:22:22 +02:00
|
|
|
if not api :
|
|
|
|
rq = request
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2022-09-02 13:29:46 +02:00
|
|
|
if GLOBAL_CONTEXT["DEBUG"]:
|
2022-09-16 10:22:22 +02:00
|
|
|
print(f"dept:\n\t{rq.args.getlist('dept')}")
|
|
|
|
print(f"favs:\n\t{rq.args.getlist('favs')}")
|
|
|
|
print(f"date:\n\t{rq.args.get('date')}\t{rq.args.get('time')}")
|
2023-09-23 04:28:19 +02:00
|
|
|
|
2022-05-10 22:35:16 +02:00
|
|
|
# Récupération des ID des départements depuis le formulaire :
|
2022-09-16 10:22:22 +02:00
|
|
|
dident_list = list(rq.args.getlist("dept"))
|
2022-06-18 13:12:43 +02:00
|
|
|
if len(dident_list) > MAX_DEPT :
|
2023-09-23 04:28:19 +02:00
|
|
|
return render_template("error.html",
|
|
|
|
error="Trop de départements sélectionnés ! Vous pouvez en sélectionner "+str(MAX_DEPT)+" au maximum.")
|
2022-06-18 13:12:43 +02:00
|
|
|
if len(dident_list) == 0 :
|
2023-09-23 04:28:19 +02:00
|
|
|
return render_template("error.html",
|
|
|
|
error="Il faut choisir au moins un département !")
|
2022-05-12 22:30:10 +02:00
|
|
|
|
2023-09-23 04:28:19 +02:00
|
|
|
# Récupération de l'éventuelle date personnalisée :
|
2022-09-16 10:22:22 +02:00
|
|
|
date_uf = str(rq.args.get("date"))
|
2022-06-18 13:12:43 +02:00
|
|
|
date_uf_sav = date_uf
|
2023-09-23 04:28:19 +02:00
|
|
|
|
|
|
|
date_uf = date_tools.d_t_format(date_uf, False)
|
2022-05-12 22:30:10 +02:00
|
|
|
|
2022-09-16 12:26:06 +02:00
|
|
|
if date_uf != [""] and not (date_tools.check_date(date_uf)) :
|
|
|
|
return render_template("error.html", error="Date incorrecte !")
|
|
|
|
|
2022-09-16 10:22:22 +02:00
|
|
|
time_uf = str(rq.args.get("time"))
|
2022-06-18 13:12:43 +02:00
|
|
|
time_uf_sav = time_uf
|
2023-09-23 04:28:19 +02:00
|
|
|
|
|
|
|
time_uf = date_tools.d_t_format(time_uf, True)
|
2022-05-12 22:30:10 +02:00
|
|
|
|
2022-09-16 12:26:06 +02:00
|
|
|
if time_uf != [""] and not (date_tools.check_time(time_uf)) :
|
|
|
|
return render_template("error.html", error="Heure incorrecte !")
|
|
|
|
|
2023-09-23 04:28:19 +02:00
|
|
|
date = dti.datetime.now(TIMEZONE)
|
2022-05-12 22:30:10 +02:00
|
|
|
|
2022-05-13 16:27:21 +02:00
|
|
|
date_str = "" # Date affichée sur la page (si personnalisée)
|
2022-05-12 22:30:10 +02:00
|
|
|
|
|
|
|
if date_uf != [""] :
|
|
|
|
date = date.replace(year = int(date_uf[0]), month = int(date_uf[1]), day = int(date_uf[2]))
|
2022-05-13 16:27:21 +02:00
|
|
|
date_str += date_uf[2] + "/" + date_uf[1] + "/" + date_uf[0]
|
2022-05-12 22:30:10 +02:00
|
|
|
|
2022-05-13 16:27:21 +02:00
|
|
|
if time_uf != [""] :
|
|
|
|
date = date.replace(hour = int(time_uf[0]), minute = int(time_uf[1]))
|
|
|
|
date_str += ", à " + time_uf[0] + ":" + time_uf[1]
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2022-06-18 13:12:43 +02:00
|
|
|
# Récupération des IDs des salles favorites :
|
2022-09-16 10:22:22 +02:00
|
|
|
favs_ids = list(rq.args.getlist("favs"))
|
2022-06-18 13:12:43 +02:00
|
|
|
if favs_ids == [None] :
|
|
|
|
favs_ids = []
|
2022-05-12 22:30:10 +02:00
|
|
|
|
2022-06-18 13:12:43 +02:00
|
|
|
# Récupération de la liste des départements existants :
|
2022-05-10 22:35:16 +02:00
|
|
|
dept_filen = "data/dept_list.txt"
|
|
|
|
dept_list = ro.get_depts(dept_filen)
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2023-09-23 04:28:19 +02:00
|
|
|
# Vérification qu'il n'y a pas de mauvais départements sélectionnés :
|
2022-06-18 13:12:43 +02:00
|
|
|
for d in dident_list :
|
|
|
|
try :
|
2022-06-15 20:01:22 +02:00
|
|
|
int(d)
|
|
|
|
except:
|
2023-09-23 04:28:19 +02:00
|
|
|
return render_template("error.html",
|
|
|
|
error="Identifiant de département invalide !",
|
|
|
|
**GLOBAL_CONTEXT)
|
2022-06-17 19:12:29 +02:00
|
|
|
if int(d) < 0 or int(d) >= len(dept_list) :
|
2023-09-23 04:28:19 +02:00
|
|
|
return render_template("error.html",
|
|
|
|
error="Identifiant de département invalide !",
|
|
|
|
**GLOBAL_CONTEXT)
|
2022-06-15 20:01:22 +02:00
|
|
|
dident_list.sort()
|
2022-05-10 20:55:19 +02:00
|
|
|
|
2022-05-10 22:35:16 +02:00
|
|
|
# Récupération des départements choisis à partir des données du formulaire :
|
|
|
|
i = 0
|
|
|
|
depts = list()
|
2022-05-12 16:53:02 +02:00
|
|
|
depts_str = "" # Noms des départements pour l'affichage
|
2022-05-10 22:35:16 +02:00
|
|
|
for d in dept_list :
|
|
|
|
if i < len(dident_list) and d.ident == int(dident_list[i]) :
|
|
|
|
depts.append(d)
|
2022-05-12 16:53:02 +02:00
|
|
|
depts_str += d.name
|
|
|
|
if (i + 1) < len(dident_list) :
|
|
|
|
depts_str += ", "
|
2022-05-10 22:35:16 +02:00
|
|
|
i += 1
|
2022-05-10 20:55:19 +02:00
|
|
|
|
2022-05-10 22:35:16 +02:00
|
|
|
ignore_list = ["salle non définie", "salle en Distanciel"]
|
2022-05-10 20:55:19 +02:00
|
|
|
|
2022-09-16 12:26:06 +02:00
|
|
|
try :
|
|
|
|
free_rooms = ro.getrooms(date, depts, ignore_list)
|
|
|
|
except ValueError as err :
|
2024-02-11 18:25:16 +01:00
|
|
|
errdetails = str(''.join(traceback.format_exception(None, err, err.__traceback__)))
|
|
|
|
if GLOBAL_CONTEXT["DEBUG"] :
|
|
|
|
print(errdetails)
|
|
|
|
return render_template("error.html",
|
2024-03-31 17:55:49 +02:00
|
|
|
error="Désolé, une erreur est survenue. UniSquat ne peut pas continuer.")
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2022-06-18 13:12:43 +02:00
|
|
|
# Création d'un dictionnaire avec les infos des salles :
|
2022-05-12 16:53:02 +02:00
|
|
|
frooms_disp = dict() # Mise en forme des infos pour la page Web
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2022-05-12 16:53:02 +02:00
|
|
|
i = 0
|
|
|
|
for r in free_rooms :
|
|
|
|
remain_time_str = ""
|
|
|
|
if r.is_free :
|
|
|
|
remain_time_str = date_tools.remain_time(date, r.end)
|
|
|
|
else :
|
|
|
|
remain_time_str = date_tools.remain_time(date, r.start)
|
|
|
|
|
|
|
|
frooms_disp[r.name] = {"start":date_tools.hour_disp(r.start),
|
|
|
|
"end":date_tools.hour_disp(r.end),
|
|
|
|
"rtime":remain_time_str}
|
|
|
|
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2022-06-18 13:12:43 +02:00
|
|
|
# Ajout des arguments favoris, et départements à l'URL :
|
2022-05-14 21:24:14 +02:00
|
|
|
change_date_str = "?"
|
2022-06-17 20:22:32 +02:00
|
|
|
if favs_ids != [] :
|
|
|
|
i = 0
|
|
|
|
for f in favs_ids :
|
|
|
|
change_date_str += "favs=" + str(f)
|
|
|
|
if i < len(favs_ids) - 1:
|
|
|
|
change_date_str += "&"
|
|
|
|
i+=1
|
2022-06-18 13:12:43 +02:00
|
|
|
change_date_str += "&"
|
2022-05-14 21:24:14 +02:00
|
|
|
for v in dident_list:
|
2022-09-02 13:29:46 +02:00
|
|
|
i = 0
|
2022-05-14 21:24:14 +02:00
|
|
|
change_date_str += "dept="+str(v)
|
|
|
|
if i<len(dident_list)-1:
|
|
|
|
change_date_str += "&"
|
|
|
|
i+=1
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2022-06-15 20:23:53 +02:00
|
|
|
|
2022-06-18 13:12:43 +02:00
|
|
|
# Génération du lien pour enlever les favoris séléctionnés :
|
2022-06-15 20:23:53 +02:00
|
|
|
nofavslink = "/app/free-rooms?"
|
|
|
|
for dept in dident_list:
|
|
|
|
nofavslink+="dept="+str(dept)+"&"
|
2022-06-18 13:12:43 +02:00
|
|
|
# Ajout des éventuels date et heure :
|
|
|
|
if date_uf_sav != "None" :
|
|
|
|
nofavslink += "date=" + date_uf_sav + "&"
|
|
|
|
if time_uf_sav != "None" :
|
|
|
|
nofavslink += "time=" + time_uf_sav
|
|
|
|
# Suppression de l'éventuel '&' en trop :
|
2022-09-02 13:29:46 +02:00
|
|
|
if nofavslink[-1] == "&":
|
2022-06-18 13:12:43 +02:00
|
|
|
nofavslink = nofavslink[:-1]
|
|
|
|
|
|
|
|
# Tri des salles selon leurs catégories :
|
2022-06-15 23:34:38 +02:00
|
|
|
favs_free_rooms = []
|
|
|
|
favs_soon_rooms = []
|
|
|
|
soon_rooms = []
|
|
|
|
final_rooms = []
|
|
|
|
|
|
|
|
for r in free_rooms:
|
|
|
|
[[soon_rooms,final_rooms],[favs_soon_rooms,favs_free_rooms]][r.id in favs_ids][r.is_free].append(r)
|
|
|
|
|
2022-09-02 11:39:38 +02:00
|
|
|
# Tri des salles bientôt disponibles en fonction du temps d'attente
|
|
|
|
sortfunc = lambda x: x.start-date.astimezone(pytz.timezone('Europe/Paris'))
|
|
|
|
soon_rooms.sort(key=sortfunc)
|
|
|
|
favs_soon_rooms.sort(key=sortfunc)
|
|
|
|
|
2022-06-17 20:05:41 +02:00
|
|
|
context = {"favs_free_rooms":favs_free_rooms, "favs_soon_rooms":favs_soon_rooms,
|
|
|
|
"free_rooms":final_rooms, "soon_rooms":soon_rooms, "frooms_disp":frooms_disp,
|
|
|
|
"depts_str":depts_str, "dident_list":dident_list, "date_str":date_str,
|
2022-06-18 13:12:43 +02:00
|
|
|
"date_uf_sav":date_uf_sav, "time_uf_sav":time_uf_sav, "change_date_str":change_date_str,
|
|
|
|
"favs":len(favs_ids)>0,"nofavslink":nofavslink}
|
2022-05-10 20:55:19 +02:00
|
|
|
|
2022-06-18 13:12:43 +02:00
|
|
|
# Création d'un log de la date et des départements demandés (pour les stats du site) :
|
2023-09-23 04:28:19 +02:00
|
|
|
ctimestamp = dti.datetime.now(TIMEZONE).timestamp()
|
2022-06-13 22:49:31 +02:00
|
|
|
log = {}
|
2022-09-07 19:02:26 +02:00
|
|
|
log["timestamp"] = ctimestamp
|
2022-09-07 16:49:43 +02:00
|
|
|
log["depts"] = [ x.name for x in depts ] # Liste les noms de départements
|
|
|
|
log["type"] = "deptcount" # Type du log
|
2022-06-13 22:49:31 +02:00
|
|
|
logs.append(log)
|
2022-09-07 19:02:26 +02:00
|
|
|
# Création d'un log de la date et des salles favorites
|
|
|
|
favs = favs_soon_rooms+favs_free_rooms
|
|
|
|
if len(favs)>0:
|
|
|
|
log = {}
|
|
|
|
log["timestamp"] = ctimestamp
|
|
|
|
log["favs"] = [ [x.name,x.dept_name] for x in favs] # Liste les noms des salles favorites
|
|
|
|
log["type"] = "favs" # Type du log
|
|
|
|
logs.append(log)
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2022-06-18 13:12:43 +02:00
|
|
|
# Suppression des logs vieux de MAX_LOG_DAYS :
|
2022-09-07 19:02:26 +02:00
|
|
|
while (ctimestamp - logs[0]["timestamp"]) / (60*60*24) > MAX_LOG_DAYS :
|
2022-06-13 22:49:31 +02:00
|
|
|
del(logs[0])
|
|
|
|
|
2022-09-07 16:49:43 +02:00
|
|
|
# Sauvegarde les logs dans un fichier cache
|
|
|
|
save_logs(logs)
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2022-09-16 10:22:22 +02:00
|
|
|
if api :
|
|
|
|
serial_context = context
|
|
|
|
for i in range(len(serial_context["free_rooms"])) :
|
|
|
|
fr_start = serial_context["free_rooms"][i].start
|
|
|
|
serial_context["free_rooms"][i].start = [fr_start.year, fr_start.month, fr_start.day, fr_start.hour, fr_start.minute, fr_start.second]
|
|
|
|
fr_end = serial_context["free_rooms"][i].end
|
|
|
|
serial_context["free_rooms"][i].end = [fr_end.year, fr_end.month, fr_end.day, fr_end.hour, fr_end.minute, fr_end.second]
|
|
|
|
serial_context["free_rooms"][i] = vars(serial_context["free_rooms"][i])
|
|
|
|
for i in range(len(serial_context["soon_rooms"])) :
|
|
|
|
fr_start = serial_context["soon_rooms"][i].start
|
|
|
|
serial_context["soon_rooms"][i].start = [fr_start.year, fr_start.month, fr_start.day, fr_start.hour, fr_start.minute, fr_start.second]
|
|
|
|
fr_end = serial_context["soon_rooms"][i].end
|
|
|
|
serial_context["soon_rooms"][i].end = [fr_end.year, fr_end.month, fr_end.day, fr_end.hour, fr_end.minute, fr_end.second]
|
|
|
|
serial_context["soon_rooms"][i] = vars(serial_context["soon_rooms"][i])
|
|
|
|
print(serial_context)
|
|
|
|
response = make_response(json.dumps(context))
|
|
|
|
response.headers["Content-Type"] = "application/json"
|
|
|
|
return response
|
|
|
|
else :
|
|
|
|
url_for("static", filename="style.css")
|
2024-03-20 19:28:40 +01:00
|
|
|
# Vérifie si le mode billboard est demandé :
|
|
|
|
billboard = str(rq.args.get("billboard"))
|
|
|
|
if billboard in ("1", "true", "True") :
|
|
|
|
return render_template("billboard.html", **context, **GLOBAL_CONTEXT)
|
2024-02-12 15:36:07 +01:00
|
|
|
else:
|
|
|
|
return render_template("free-rooms.html", **context, **GLOBAL_CONTEXT)
|
2022-05-12 17:58:06 +02:00
|
|
|
|
2022-05-12 22:30:10 +02:00
|
|
|
@app.route("/app/date-select", methods=["POST", "GET"])
|
2022-05-12 17:58:06 +02:00
|
|
|
def date_select() :
|
2022-05-12 22:30:10 +02:00
|
|
|
"""
|
|
|
|
Permet de sélectionner une date à laquelle
|
|
|
|
chercher des salles libres.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
flask.render_template
|
2023-09-23 04:28:19 +02:00
|
|
|
|
2022-05-12 22:30:10 +02:00
|
|
|
"""
|
2022-06-18 13:12:43 +02:00
|
|
|
dident_list = list(request.args.getlist("dept"))
|
|
|
|
favs_ids = list(request.args.getlist("favs"))
|
2022-05-12 22:30:10 +02:00
|
|
|
|
2022-09-02 13:29:46 +02:00
|
|
|
if GLOBAL_CONTEXT["DEBUG"]:
|
|
|
|
print(f"dept:{dident_list}")
|
|
|
|
print(f"favs:{favs_ids}")
|
|
|
|
|
2022-06-17 20:22:32 +02:00
|
|
|
context = {"dident_list":dident_list, "favs_ids":favs_ids}
|
2022-05-12 22:30:10 +02:00
|
|
|
|
2022-06-14 12:00:33 +02:00
|
|
|
return render_template("date-select.html", **context, **GLOBAL_CONTEXT)
|
2022-05-13 15:17:23 +02:00
|
|
|
|
2022-09-16 10:22:22 +02:00
|
|
|
@app.route("/api/depts_list")
|
|
|
|
def api_depts_get() :
|
|
|
|
"""
|
|
|
|
Renvoie la liste des départements (pour l'API).
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
None.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Récupération de la liste des départements existants :
|
|
|
|
dept_filen = "data/dept_list.txt"
|
|
|
|
dept_list = ro.get_depts(dept_filen)
|
|
|
|
serial_dlist = [vars(d) for d in dept_list]
|
2023-09-22 22:15:12 +02:00
|
|
|
|
2022-09-16 10:22:22 +02:00
|
|
|
response = make_response(json.dumps(serial_dlist))
|
|
|
|
response.headers["Content-Type"] = "application/json"
|
|
|
|
return response
|
|
|
|
|
|
|
|
@app.route("/api/free_rooms")
|
|
|
|
def api_frooms() :
|
|
|
|
"""
|
|
|
|
Redirige vers le JSON des salles (pour l'API).
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
None.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return free_rooms(True, request)
|
|
|
|
|
2022-09-06 13:49:14 +02:00
|
|
|
@app.route("/sitemap.xml")
|
|
|
|
def sitemap():
|
|
|
|
sitemap_xml = render_template("sitemap.xml", **GLOBAL_CONTEXT)
|
2022-09-16 10:22:22 +02:00
|
|
|
response = make_response(sitemap_xml)
|
|
|
|
response.headers["Content-Type"] = "application/xml"
|
2022-09-06 13:49:14 +02:00
|
|
|
return response
|
|
|
|
|
2022-05-13 15:17:23 +02:00
|
|
|
@app.errorhandler(404)
|
|
|
|
def error(e):
|
|
|
|
"""
|
2023-09-23 04:28:19 +02:00
|
|
|
Affiche la page d'erreur.
|
2022-05-13 15:17:23 +02:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2023-09-23 04:28:19 +02:00
|
|
|
None
|
2022-05-13 15:17:23 +02:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
flask.render_template
|
2023-09-23 04:28:19 +02:00
|
|
|
|
2022-05-13 15:17:23 +02:00
|
|
|
"""
|
2024-02-12 19:20:27 +01:00
|
|
|
template = render_template("error.html", error="Page non trouvée !", **GLOBAL_CONTEXT)
|
|
|
|
response = make_response(template, 404)
|
|
|
|
return response
|