Skip to content
Snippets Groups Projects
Verified Commit 57dbd3db authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Add confirmation page for register absence form

parent cf3182ab
No related branches found
No related tags found
1 merge request!109Resolve "Add "Are you sure?" page for register absence form"
Pipeline #4748 passed
...@@ -26,12 +26,18 @@ def mark_absent( ...@@ -26,12 +26,18 @@ def mark_absent(
excuse_type: Optional[ExcuseType] = None, excuse_type: Optional[ExcuseType] = None,
remarks: str = "", remarks: str = "",
to_period: Optional[int] = None, to_period: Optional[int] = None,
dry_run: bool = False,
): ):
"""Mark a person absent for all lessons in a day, optionally starting with a selected period number. """Mark a person absent for all lessons in a day, optionally starting with a selected period number.
This function creates `PersonalNote` objects for every `LessonPeriod` the person This function creates `PersonalNote` objects for every `LessonPeriod` the person
participates in on the selected day and marks them as absent/excused. participates in on the selected day and marks them as absent/excused.
:param dry_run: With this activated, the function won't change any data
and just return the count of affected lessons
:return: Count of affected lesson periods
..note:: Only available when AlekSIS-App-Alsijil is installed. ..note:: Only available when AlekSIS-App-Alsijil is installed.
:Date: 2019-11-10 :Date: 2019-11-10
...@@ -51,32 +57,39 @@ def mark_absent( ...@@ -51,32 +57,39 @@ def mark_absent(
lesson_periods = lesson_periods.filter(period__period__lte=to_period) lesson_periods = lesson_periods.filter(period__period__lte=to_period)
# Create and update all personal notes for the discovered lesson periods # Create and update all personal notes for the discovered lesson periods
for lesson_period in lesson_periods: if not dry_run:
sub = lesson_period.get_substitution() for lesson_period in lesson_periods:
if sub and sub.cancelled: sub = lesson_period.get_substitution()
continue if sub and sub.cancelled:
continue
with reversion.create_revision():
set_user(get_request().user) with reversion.create_revision():
personal_note, created = ( set_user(get_request().user)
PersonalNote.objects.select_related(None) personal_note, created = (
.prefetch_related(None) PersonalNote.objects.select_related(None)
.update_or_create( .prefetch_related(None)
person=self, .update_or_create(
lesson_period=lesson_period, person=self,
week=wanted_week.week, lesson_period=lesson_period,
year=wanted_week.year, week=wanted_week.week,
defaults={"absent": absent, "excused": excused, "excuse_type": excuse_type,}, year=wanted_week.year,
defaults={
"absent": absent,
"excused": excused,
"excuse_type": excuse_type,
},
)
) )
) personal_note.groups_of_person.set(self.member_of.all())
personal_note.groups_of_person.set(self.member_of.all())
if remarks:
if remarks: if personal_note.remarks:
if personal_note.remarks: personal_note.remarks += "; %s" % remarks
personal_note.remarks += "; %s" % remarks else:
else: personal_note.remarks = remarks
personal_note.remarks = remarks personal_note.save()
personal_note.save()
return lesson_periods.count()
@LessonPeriod.method @LessonPeriod.method
......
{# -*- engine:django -*- #}
{% extends "core/base.html" %}
{% load material_form i18n static %}
{% block browser_title %}{% blocktrans %}Confirm: Register absence{% endblocktrans %}{% endblock %}
{% block page_title %}{% blocktrans %}Confirm: Register absence{% endblocktrans %}{% endblock %}
{% block content %}
<p class="flow-text">
{% blocktrans %}
Do you really want to register the following absence?
{% endblocktrans %}
</p>
<div class="card">
<div class="card-content">
<div class="card-title">
{{ person }}
</div>
<div class="collection">
<div class="collection-item">
<i class="material-icons left">date_range</i>
{{ form_data.date_start }}, {{ form_data.from_period }}. – {{ form_data.date_end }}, {{ form_data.to_period }}.
{% if form_data.date_start != form_data.date_end %}
<div class="alert warning">
<div>
<i class="material-icons left">warning</i>
{% blocktrans %}
As the length of this absence is longer than one day,
please double check the correctness of your entry.
{% endblocktrans %}
</div>
</div>
{% endif %}
</div>
<div class="collection-item">
<i class="material-icons left">list</i>
{% blocktrans with count=affected_lessons %} {{ count }} affected lessons {% endblocktrans %}
{% if affected_lessons == 0 %}
<div class="alert error">
<div>
<i class="material-icons left">error</i>
{% blocktrans %}
There are no affected lessons. Registering this absence won't have any effect.
{% endblocktrans %}
</div>
</div>
{% endif %}
</div>
<div class="collection-item">
<i class="material-icons left">label</i>
{% if form_data.absent %}
<span class="chip red white-text">{% trans "Absent" %}</span>
{% if form_data.excused and form_data.excuse_type %}
<span class="chip green white-text">{{ form_data.excuse_type.name }}</span>
{% elif form_data.excused %}
<span class="chip green white-text">{% trans "Excused" %}</span>
{% endif %}
{% else %}
{% trans "Reset status to 'not absent'" %}
{% endif %}
</div>
{% if form_data.remarks %}
<div class="collection-item">
<i class="material-icons left">edit</i>
{{ form_data.remarks }}
</div>
{% endif %}
</div>
</div>
</div>
<form method="post">
{% csrf_token %}
<div class="hide">
{% form form=form %}{% endform %}
</div>
<input type="hidden" name="confirmed" value="1">
{% include "core/partials/save_button.html" %}
<a class="btn red waves-effect waves-light" href="{% url "register_absence" person.pk %}">
<i class="material-icons left">cancel</i>
{% trans "Cancel" %}
</a>
</form>
{% endblock %}
...@@ -677,6 +677,8 @@ def register_absence(request: HttpRequest, id_: int) -> HttpResponse: ...@@ -677,6 +677,8 @@ def register_absence(request: HttpRequest, id_: int) -> HttpResponse:
register_absence_form = RegisterAbsenceForm(request.POST or None) register_absence_form = RegisterAbsenceForm(request.POST or None)
if request.method == "POST" and register_absence_form.is_valid(): if request.method == "POST" and register_absence_form.is_valid():
confirmed = request.POST.get("confirmed", "0") == "1"
# Get data from form # Get data from form
# person = register_absence_form.cleaned_data["person"] # person = register_absence_form.cleaned_data["person"]
start_date = register_absence_form.cleaned_data["date_start"] start_date = register_absence_form.cleaned_data["date_start"]
...@@ -689,18 +691,35 @@ def register_absence(request: HttpRequest, id_: int) -> HttpResponse: ...@@ -689,18 +691,35 @@ def register_absence(request: HttpRequest, id_: int) -> HttpResponse:
remarks = register_absence_form.cleaned_data["remarks"] remarks = register_absence_form.cleaned_data["remarks"]
# Mark person as absent # Mark person as absent
affected_count = 0
delta = end_date - start_date delta = end_date - start_date
for i in range(delta.days + 1): for i in range(delta.days + 1):
from_period_on_day = from_period if i == 0 else TimePeriod.period_min from_period_on_day = from_period if i == 0 else TimePeriod.period_min
to_period_on_day = to_period if i == delta.days else TimePeriod.period_max to_period_on_day = to_period if i == delta.days else TimePeriod.period_max
day = start_date + timedelta(days=i) day = start_date + timedelta(days=i)
person.mark_absent( affected_count += person.mark_absent(
day, from_period_on_day, absent, excused, excuse_type, remarks, to_period_on_day, day,
from_period_on_day,
absent,
excused,
excuse_type,
remarks,
to_period_on_day,
dry_run=not confirmed,
) )
messages.success(request, _("The absence has been saved.")) if not confirmed:
return redirect("overview_person", person.pk) # Show confirmation page
context = {}
context["affected_lessons"] = affected_count
context["person"] = person
context["form_data"] = register_absence_form.cleaned_data
context["form"] = register_absence_form
return render(request, "alsijil/absences/register_confirm.html", context)
else:
messages.success(request, _("The absence has been saved."))
return redirect("overview_person", person.pk)
context["person"] = person context["person"] = person
context["register_absence_form"] = register_absence_form context["register_absence_form"] = register_absence_form
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment