Compare commits
4 Commits
159ff9bcf7
...
6d4326e30c
Author | SHA1 | Date | |
---|---|---|---|
6d4326e30c | |||
19b70f0d1b | |||
b507199c2a | |||
a702385c21 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,3 +4,5 @@ __pycache__/
|
||||
cache/
|
||||
push_to_server.sh
|
||||
log.json
|
||||
venv
|
||||
venv/*
|
||||
|
39
app.py
39
app.py
@ -16,6 +16,7 @@ import datetime as dti
|
||||
import pytz
|
||||
import json
|
||||
import os
|
||||
import traceback
|
||||
|
||||
from flask import Flask
|
||||
from flask import render_template
|
||||
@ -141,7 +142,7 @@ def stats():
|
||||
|
||||
sort = [ [x,counts[x]] for x in counts.keys() ]
|
||||
sort.sort(key = lambda x: x[1],reverse = True ) # Trie selon la valeur du deuxieme élément de la liste
|
||||
|
||||
|
||||
sort_favs = [ counts_favs[x] for x in counts_favs.keys() ]
|
||||
sort_favs.sort(key = lambda x: x[2],reverse = True )
|
||||
|
||||
@ -158,7 +159,7 @@ def free_rooms(api = False, rq = None) :
|
||||
----------
|
||||
api : bool
|
||||
Indique si la page est accédée par l'API.
|
||||
|
||||
|
||||
rq : requests.request
|
||||
Requête.
|
||||
|
||||
@ -168,7 +169,7 @@ def free_rooms(api = False, rq = None) :
|
||||
"""
|
||||
if not api :
|
||||
rq = request
|
||||
|
||||
|
||||
if GLOBAL_CONTEXT["DEBUG"]:
|
||||
print(f"dept:\n\t{rq.args.getlist('dept')}")
|
||||
print(f"favs:\n\t{rq.args.getlist('favs')}")
|
||||
@ -188,6 +189,9 @@ def free_rooms(api = False, rq = None) :
|
||||
else :
|
||||
date_uf = date_uf.split("-")
|
||||
|
||||
if date_uf != [""] and not (date_tools.check_date(date_uf)) :
|
||||
return render_template("error.html", error="Date incorrecte !")
|
||||
|
||||
time_uf = str(rq.args.get("time"))
|
||||
time_uf_sav = time_uf
|
||||
if time_uf == "None" :
|
||||
@ -195,6 +199,9 @@ def free_rooms(api = False, rq = None) :
|
||||
else :
|
||||
time_uf = time_uf.split(":")
|
||||
|
||||
if time_uf != [""] and not (date_tools.check_time(time_uf)) :
|
||||
return render_template("error.html", error="Heure incorrecte !")
|
||||
|
||||
|
||||
date = dti.datetime.now()
|
||||
|
||||
@ -207,8 +214,8 @@ def free_rooms(api = False, rq = None) :
|
||||
if time_uf != [""] :
|
||||
date = date.replace(hour = int(time_uf[0]), minute = int(time_uf[1]))
|
||||
date_str += ", à " + time_uf[0] + ":" + time_uf[1]
|
||||
|
||||
|
||||
|
||||
|
||||
# Récupération des IDs des salles favorites :
|
||||
favs_ids = list(rq.args.getlist("favs"))
|
||||
if favs_ids == [None] :
|
||||
@ -218,7 +225,7 @@ def free_rooms(api = False, rq = None) :
|
||||
# Récupération de la liste des départements existants :
|
||||
dept_filen = "data/dept_list.txt"
|
||||
dept_list = ro.get_depts(dept_filen)
|
||||
|
||||
|
||||
# Vérification qu'il n'y a pas de mauvais départements demandés :
|
||||
for d in dident_list :
|
||||
try :
|
||||
@ -243,11 +250,15 @@ def free_rooms(api = False, rq = None) :
|
||||
|
||||
ignore_list = ["salle non définie", "salle en Distanciel"]
|
||||
|
||||
free_rooms = ro.getrooms(date, depts, ignore_list)
|
||||
|
||||
try :
|
||||
free_rooms = ro.getrooms(date, depts, ignore_list)
|
||||
except ValueError as err :
|
||||
return render_template("error.html", error="Le serveur Unistra a rencontré une erreur ! Veuillez réessayer plus tard.")
|
||||
#return render_template("error.html", error="Le serveur Unistra a rencontré une erreur ! Détails de l'erreur : " + str(''.join(traceback.format_exception(None, err, err.__traceback__))))
|
||||
|
||||
# Création d'un dictionnaire avec les infos des salles :
|
||||
frooms_disp = dict() # Mise en forme des infos pour la page Web
|
||||
|
||||
|
||||
i = 0
|
||||
for r in free_rooms :
|
||||
remain_time_str = ""
|
||||
@ -260,7 +271,7 @@ def free_rooms(api = False, rq = None) :
|
||||
"end":date_tools.hour_disp(r.end),
|
||||
"rtime":remain_time_str}
|
||||
|
||||
|
||||
|
||||
# Ajout des arguments favoris, et départements à l'URL :
|
||||
change_date_str = "?"
|
||||
if favs_ids != [] :
|
||||
@ -277,7 +288,7 @@ def free_rooms(api = False, rq = None) :
|
||||
if i<len(dident_list)-1:
|
||||
change_date_str += "&"
|
||||
i+=1
|
||||
|
||||
|
||||
|
||||
# Génération du lien pour enlever les favoris séléctionnés :
|
||||
nofavslink = "/app/free-rooms?"
|
||||
@ -327,14 +338,14 @@ def free_rooms(api = False, rq = None) :
|
||||
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)
|
||||
|
||||
|
||||
# Suppression des logs vieux de MAX_LOG_DAYS :
|
||||
while (ctimestamp - logs[0]["timestamp"]) / (60*60*24) > MAX_LOG_DAYS :
|
||||
del(logs[0])
|
||||
|
||||
# Sauvegarde les logs dans un fichier cache
|
||||
save_logs(logs)
|
||||
|
||||
|
||||
if api :
|
||||
serial_context = context
|
||||
for i in range(len(serial_context["free_rooms"])) :
|
||||
@ -396,7 +407,7 @@ def api_depts_get() :
|
||||
dept_filen = "data/dept_list.txt"
|
||||
dept_list = ro.get_depts(dept_filen)
|
||||
serial_dlist = [vars(d) for d in dept_list]
|
||||
|
||||
|
||||
response = make_response(json.dumps(serial_dlist))
|
||||
response.headers["Content-Type"] = "application/json"
|
||||
return response
|
||||
|
@ -175,4 +175,80 @@ def remain_time(date, rdate) :
|
||||
if deltasec[1] > 0 :
|
||||
remain_time_str += " " + str(deltasec[1]) + " minutes"
|
||||
|
||||
return remain_time_str
|
||||
return remain_time_str
|
||||
|
||||
def check_date(date) :
|
||||
"""
|
||||
Vérifie que la date est correcte.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
date : list
|
||||
Date à vérifier.
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool
|
||||
'True' si la date est correcte, 'False' sinon.
|
||||
"""
|
||||
year = 1
|
||||
month = 1
|
||||
day = 1
|
||||
|
||||
if len(date) != 3 :
|
||||
return False
|
||||
|
||||
try :
|
||||
year = int(date[0])
|
||||
month = int(date[1])
|
||||
day = int(date[2])
|
||||
except ValueError :
|
||||
return False
|
||||
|
||||
if year < 2 or month < 1 or day < 1 :
|
||||
return False
|
||||
|
||||
if month > 12 :
|
||||
return False
|
||||
|
||||
if day > month_days(month, year) :
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def check_time(time) :
|
||||
"""
|
||||
Vérifie que l'heure est correcte.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
time : list
|
||||
Heure à vérifier.
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool
|
||||
'True' si l'heure est correcte, 'False' sinon.
|
||||
"""
|
||||
hour = 0
|
||||
minute = 0
|
||||
|
||||
if len(time) != 2 :
|
||||
return False
|
||||
|
||||
try :
|
||||
hour = int(time[0])
|
||||
minute = int(time[1])
|
||||
except ValueError :
|
||||
return False
|
||||
|
||||
if hour < 0 or minute < 0 :
|
||||
return False
|
||||
|
||||
if hour > 23 :
|
||||
return False
|
||||
|
||||
if minute > 59 :
|
||||
return False
|
||||
|
||||
return True
|
@ -94,4 +94,4 @@ class Dept :
|
||||
self.rooms = rooms
|
||||
|
||||
def genlink(self,link):
|
||||
return "https://adecons.unistra.fr/jsp/custom/modules/plannings/anonymous_cal.jsp?resources="+link.strip()+"&projectId=5&calType=ical"
|
||||
return "https://adecons.unistra.fr/jsp/custom/modules/plannings/anonymous_cal.jsp?resources="+link.strip()+"&projectId=1&calType=ical"
|
||||
|
@ -135,7 +135,7 @@ def sched_get(date, link, enddate = None, nocache = False) :
|
||||
finallink = finallink.replace("$DAY2$", day1)
|
||||
finallink = finallink.replace("$MONTH2$", month1)
|
||||
finallink = finallink.replace("$YEAR2$", year1)
|
||||
|
||||
|
||||
if nocache:
|
||||
return requests.get(finallink).content
|
||||
else :
|
||||
|
Loading…
Reference in New Issue
Block a user