Entraînement personnelMatt MLB2024-05-14T09:08:06+00:00
# -*- coding: utf-8 -*-
from flask import Flask, render_template, request, redirect, url_for, jsonify
import datetime
app = Flask(__name__)
# Variables pour le calendrier
schedule = {}
times = ["17h00-17h30", "17h30-18h00", "18h00-18h30", "18h30-19h00", "19h00-19h30", "19h30-20h00", "20h00-20h30", "20h30-21h00"]
days = ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi"]
# Fonction pour formater les dates
def format_date(date):
return date.strftime("%Y-%m-%d")
# Enregistrer format_date comme un filtre
app.jinja_env.filters['format_date'] = format_date
# Function to initialize week data
def initialize_week_data(start_date):
week_key = format_date(start_date)
if week_key not in schedule:
schedule[week_key] = {}
for day in days:
for time in times:
schedule[week_key][f"{day}-{time}"] = {
"withBasket": [{"status": "Libre"} for _ in range(2)],
"withoutBasket": [{"status": "Libre"} for _ in range(3)]
}
@app.route('/', methods=['GET', 'POST'])
def index():
# Determine the current week start
if 'current_week_start' in request.form:
current_week_start = datetime.datetime.strptime(request.form['current_week_start'], "%Y-%m-%d").date()
else:
current_week_start = datetime.date.today() - datetime.timedelta(days=datetime.date.today().weekday())
# Update the week based on the form input
if request.method == 'POST':
change_week = request.form.get('changeWeek')
if change_week == 'prev':
current_week_start -= datetime.timedelta(days=7)
elif change_week == 'next':
current_week_start += datetime.timedelta(days=7)
initialize_week_data(current_week_start)
week_dates = [(days[i], (current_week_start + datetime.timedelta(days=i)).strftime("%d/%m")) for i in range(5)]
week_key = format_date(current_week_start)
return render_template(
'calendrier_reservation_entrainement.html',
times=times,
week_dates=week_dates,
schedule=schedule[week_key],
current_week_start=current_week_start
)
@app.route('/reserve', methods=['POST'])
def reserve():
week_key = request.form['weekKey']
day = request.form['day']
time = request.form['time']
type_reservation = request.form['type']
last_name = request.form['lastName']
first_name = request.form['firstName']
category = request.form['category']
slot_key = f"{day}-{time}"
slot_list = schedule[week_key][slot_key][type_reservation]
for slot in slot_list:
if slot['status'] == "Libre":
slot['status'] = f"{last_name} {first_name}"
slot['category'] = category
break
return redirect(url_for('index'))
@app.route('/get_slot_info', methods=['POST'])
def get_slot_info():
week_key = request.json['weekKey']
slot_key = request.json['slotKey']
return jsonify(schedule[week_key][slot_key])
@app.route('/all_data')
def all_data():
return render_template('all_data.html', schedule=schedule, days=days, times=times)
if __name__ == '__main__':
app.run(debug=True)