Corriger les champs et le calcul des horaires
Some checks are pending
CI - Tests et Syntax / lint-and-test (push) Waiting to run

This commit is contained in:
root 2026-08-20 20:10:58 +00:00
parent e815f031f1
commit f9537e74f1
2 changed files with 16 additions and 6 deletions

View file

@ -26,8 +26,8 @@
</div></div>
<div class="card mb-3"><div class="card-header d-flex justify-content-between"><strong>Horaires types (maximum 6)</strong><span class="badge bg-primary">{{ templates|length }}/6</span></div><div class="card-body">
{% for template in templates %}<div class="border-bottom py-2"><div class="d-flex justify-content-between"><span><strong>{{ template.name }}</strong><br><small>{{ template.start_time.strftime('%H:%M') }}{{ template.end_time.strftime('%H:%M') }}{% if template.lunch_start %} · pause {{ template.lunch_start.strftime('%H:%M') }}{{ template.lunch_end.strftime('%H:%M') }}{% endif %} · <strong>durée : {{ template.duration_minutes // 60 }} h {{ '%02d'|format(template.duration_minutes % 60) }}</strong></small></span><form method="post" action="{{ url_for('planning.delete_time_template', id=template.id) }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><button class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i></button></form></div><form method="post" action="{{ url_for('planning.edit_time_template', id=template.id) }}" class="row g-1 mt-1"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><div class="col-4"><input class="form-control form-control-sm" name="name" value="{{ template.name }}"></div><div class="col-2"><input class="form-control form-control-sm" type="time" name="start_time" value="{{ template.start_time.strftime('%H:%M') }}"></div><div class="col-2"><input class="form-control form-control-sm" type="time" name="end_time" value="{{ template.end_time.strftime('%H:%M') }}"></div><div class="col-2"><input class="form-control form-control-sm" type="time" name="lunch_start" value="{{ template.lunch_start.strftime('%H:%M') if template.lunch_start else '' }}"></div><div class="col-2"><button class="btn btn-sm btn-outline-primary w-100">Modifier</button></div></form></div>{% else %}<p class="text-muted">Aucun profil défini.</p>{% endfor %}
{% if templates|length < 6 %}<form method="post" action="{{ url_for('planning.save_time_template') }}" class="mt-3"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input class="form-control mb-2" name="name" placeholder="Nom : journée normale, été…" required><div class="row g-2"><div class="col-6"><input class="form-control" type="time" name="start_time" required></div><div class="col-6"><input class="form-control" type="time" name="end_time" required></div><div class="col-6"><input class="form-control" type="time" name="lunch_start" placeholder="Début pause"></div><div class="col-6"><input class="form-control" type="time" name="lunch_end" placeholder="Fin pause"></div></div><button class="btn btn-outline-primary btn-sm mt-2">Ajouter lhoraire type</button></form>{% endif %}
{% for template in templates %}<div class="border-bottom py-2"><div class="d-flex justify-content-between"><span><strong>{{ template.name }}</strong><br><small>{{ template.start_time.strftime('%H:%M') }}{{ template.end_time.strftime('%H:%M') }}{% if template.lunch_start %} · pause {{ template.lunch_start.strftime('%H:%M') }}{{ template.lunch_end.strftime('%H:%M') }}{% endif %} · <strong>durée : {{ template.duration_minutes // 60 }} h {{ '%02d'|format(template.duration_minutes % 60) }}</strong></small></span><form method="post" action="{{ url_for('planning.delete_time_template', id=template.id) }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><button class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i></button></form></div><form method="post" action="{{ url_for('planning.edit_time_template', id=template.id) }}" class="row g-1 mt-1"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><div class="col-3"><label class="form-label small mb-0">Nom</label><input class="form-control form-control-sm" name="name" value="{{ template.name }}"></div><div class="col-2"><label class="form-label small mb-0">Début</label><input class="form-control form-control-sm" type="time" name="start_time" value="{{ template.start_time.strftime('%H:%M') }}"></div><div class="col-2"><label class="form-label small mb-0">Fin</label><input class="form-control form-control-sm" type="time" name="end_time" value="{{ template.end_time.strftime('%H:%M') }}"></div><div class="col-2"><label class="form-label small mb-0">Début pause</label><input class="form-control form-control-sm" type="time" name="lunch_start" value="{{ template.lunch_start.strftime('%H:%M') if template.lunch_start else '' }}"></div><div class="col-2"><label class="form-label small mb-0">Fin pause</label><input class="form-control form-control-sm" type="time" name="lunch_end" value="{{ template.lunch_end.strftime('%H:%M') if template.lunch_end else '' }}"></div><div class="col-1 d-flex align-items-end"><button class="btn btn-sm btn-outline-primary w-100" title="Modifier">OK</button></div></form></div>{% else %}<p class="text-muted">Aucun profil défini.</p>{% endfor %}
{% if templates|length < 6 %}<form method="post" action="{{ url_for('planning.save_time_template') }}" class="mt-3"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input class="form-control mb-2" name="name" placeholder="Nom : journée normale, été…" required><div class="row g-2"><div class="col-6"><label class="form-label small mb-0">Début du travail</label><input class="form-control" type="time" name="start_time" required></div><div class="col-6"><label class="form-label small mb-0">Fin du travail</label><input class="form-control" type="time" name="end_time" required></div><div class="col-6"><label class="form-label small mb-0">Début de pause</label><input class="form-control" type="time" name="lunch_start"></div><div class="col-6"><label class="form-label small mb-0">Fin de pause</label><input class="form-control" type="time" name="lunch_end"></div></div><button class="btn btn-outline-primary btn-sm mt-2">Ajouter lhoraire type</button></form>{% endif %}
</div></div>
</div>

View file

@ -25,6 +25,16 @@ def _minutes(start, end, lunch_start=None, lunch_end=None):
return max(value, 0)
def _valid_schedule(start, end, lunch_start=None, lunch_end=None):
if not start or not end or end <= start:
return False
if bool(lunch_start) != bool(lunch_end):
return False
if lunch_start and (lunch_start < start or lunch_end > end or lunch_end <= lunch_start):
return False
return _minutes(start, end, lunch_start, lunch_end) > 0
def _redirect(year=None):
return redirect(url_for("planning.time_tracking", year=year or _current_year()))
@ -196,8 +206,8 @@ def save_time_template():
return _redirect()
start, end = _time(request.form.get("start_time")), _time(request.form.get("end_time"))
lunch_start, lunch_end = _time(request.form.get("lunch_start")), _time(request.form.get("lunch_end"))
if _minutes(start, end, lunch_start, lunch_end) <= 0:
flash("Les horaires saisis sont invalides.", "danger")
if not _valid_schedule(start, end, lunch_start, lunch_end):
flash("Horaires invalides : la fin doit être après le début et la pause doit être complète et comprise dans la journée.", "danger")
return _redirect()
template = WorkScheduleTemplate(
user_id=current_user.id, name=(request.form.get("name") or f"Horaire {count + 1}").strip(),
@ -228,8 +238,8 @@ def edit_time_template(id):
start, end = _time(request.form.get("start_time")), _time(request.form.get("end_time"))
lunch_start = _time(request.form.get("lunch_start"))
lunch_end = _time(request.form.get("lunch_end")) or template.lunch_end
if _minutes(start, end, lunch_start, lunch_end) <= 0:
flash("Les horaires saisis sont invalides.", "danger")
if not _valid_schedule(start, end, lunch_start, lunch_end):
flash("Horaires invalides : la fin doit être après le début et la pause doit être complète et comprise dans la journée.", "danger")
return _redirect()
template.name = (request.form.get("name") or template.name).strip()
template.start_time, template.end_time = start, end