diff --git a/app_new/planning/schedules.py b/app_new/planning/schedules.py index b57e532..fd0c0e5 100644 --- a/app_new/planning/schedules.py +++ b/app_new/planning/schedules.py @@ -23,10 +23,22 @@ def index(): from datetime import date, timedelta from calendar import monthrange from ..core.services.planning_service import PlanningService - from ..core.models.planning import PlanningDay, PlanningItem + from ..core.models.planning import PlanningDay, PlanningItem, ClosureWorkDay + from ..core.models.settings import AppSettings + from ..core.services.planning_service import get_french_public_holidays # Récupérer l'année demandée ou année courante + mode = request.args.get('mode') + saved_mode = AppSettings.get(f'planning_calendar_mode_{current_user.id}', 'calendar') + if mode not in ('calendar', 'school'): + mode = saved_mode if saved_mode in ('calendar', 'school') else 'calendar' + else: + AppSettings.set(f'planning_calendar_mode_{current_user.id}', mode) year = request.args.get('year', date.today().year, type=int) + if mode == 'school' and 'year' not in request.args: + year = date.today().year if date.today().month >= 9 else date.today().year - 1 + display_start = date(year, 9, 1) if mode == 'school' else date(year, 1, 1) + display_end = date(year + 1, 8, 31) if mode == 'school' else date(year, 12, 31) # Préparer les données du calendrier. Le calendrier principal est la # source unique : il rassemble les interventions et les échéances @@ -37,9 +49,13 @@ def index(): # Récupérer les vacances closures = CollegeClosure.query.filter( - CollegeClosure.start_date <= date(year, 12, 31), - CollegeClosure.end_date >= date(year, 1, 1) + CollegeClosure.start_date <= display_end, + CollegeClosure.end_date >= display_start ).all() + permanence_dates = {item.work_date for item in ClosureWorkDay.query.filter(ClosureWorkDay.work_date >= display_start, ClosureWorkDay.work_date <= display_end).all()} + holiday_dates = {item[0] for item in get_french_public_holidays(year)} + if mode == 'school': + holiday_dates |= {item[0] for item in get_french_public_holidays(year + 1)} # Fonction pour vérifier si une date est en vacances def is_vacation(d): @@ -50,13 +66,13 @@ def index(): # Récupérer les congés personnels leaves = TechnicianAvailability.query.filter( - TechnicianAvailability.date >= date(year, 1, 1), - TechnicianAvailability.date <= date(year, 12, 31) + TechnicianAvailability.date >= display_start, + TechnicianAvailability.date <= display_end ).all() leave_dates = {l.date: l for l in leaves} - year_start = date(year, 1, 1) - year_end = date(year, 12, 31) + year_start = display_start + year_end = display_end terminal_intervention_statuses = ('terminee', 'cloturee', 'annulee', 'ignoree') interventions = Intervention.query.filter( Intervention.scheduled_date >= year_start, @@ -108,18 +124,20 @@ def index(): 'color': 'info', }) - for month in range(1, 13): + month_order = list(range(9, 13)) + list(range(1, 9)) if mode == 'school' else list(range(1, 13)) + for month in month_order: + actual_year = year if mode != 'school' or month >= 9 else year + 1 # Nombre de jours dans le mois - days_in_month = monthrange(year, month)[1] + days_in_month = monthrange(actual_year, month)[1] # Jour de la semaine du premier jour (0=lundi, 6=dimanche) - first_weekday = date(year, month, 1).weekday() + first_weekday = date(actual_year, month, 1).weekday() weeks = [] week = [None] * first_weekday # Cases vides avant le 1er jour for day in range(1, days_in_month + 1): - d = date(year, month, day) + d = date(actual_year, month, day) vacation = is_vacation(d) leave = leave_dates.get(d) weekday = d.weekday() @@ -130,6 +148,8 @@ def index(): 'is_weekend': weekday >= 5, 'is_vacation': vacation is not None, 'vacation_name': vacation, + 'is_holiday': d in holiday_dates, + 'is_permanence': d in permanence_dates, 'is_leave': leave is not None, 'leave_type': leave.availability_type if leave else None, 'events': events_by_date.get(d, []), @@ -145,6 +165,7 @@ def index(): months_data.append({ 'name': months_names[month - 1], + 'year': actual_year, 'weeks': weeks }) @@ -158,6 +179,7 @@ def index(): return render_template('planning/calendar.html', year=year, + mode=mode, months=months_data, stats=stats ) diff --git a/app_new/templates/planning/calendar.html b/app_new/templates/planning/calendar.html index 2c7890d..035d166 100644 --- a/app_new/templates/planning/calendar.html +++ b/app_new/templates/planning/calendar.html @@ -6,7 +6,7 @@