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

Merge branch 'master' into 73-add-rules-and-permissions

parents 256bf855 c7a847a2
No related branches found
No related tags found
1 merge request!49Resolve "Add rules and permissions"
...@@ -4,6 +4,7 @@ from typing import Dict, Optional, Union ...@@ -4,6 +4,7 @@ from typing import Dict, Optional, Union
from django.db.models import Exists, OuterRef, QuerySet from django.db.models import Exists, OuterRef, QuerySet
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
import reversion
from calendarweek import CalendarWeek from calendarweek import CalendarWeek
from aleksis.apps.chronos.models import LessonPeriod from aleksis.apps.chronos.models import LessonPeriod
...@@ -37,8 +38,10 @@ def mark_absent( ...@@ -37,8 +38,10 @@ def mark_absent(
wanted_week = CalendarWeek.from_date(day) wanted_week = CalendarWeek.from_date(day)
# Get all lessons of this person on the specified day # Get all lessons of this person on the specified day
lesson_periods = self.lesson_periods_as_participant.on_day(day).filter( lesson_periods = (
period__period__gte=from_period self.lesson_periods_as_participant.on_day(day)
.filter(period__period__gte=from_period)
.annotate_week(wanted_week)
) )
if to_period: if to_period:
...@@ -46,21 +49,30 @@ def mark_absent( ...@@ -46,21 +49,30 @@ def mark_absent(
# 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: for lesson_period in lesson_periods:
personal_note, created = PersonalNote.objects.update_or_create( sub = lesson_period.get_substitution()
person=self, if sub and sub.is_cancelled:
lesson_period=lesson_period, continue
week=wanted_week.week,
year=wanted_week.year, with reversion.create_revision():
defaults={"absent": absent, "excused": excused, "excuse_type": excuse_type}, personal_note, created = PersonalNote.objects.update_or_create(
) person=self,
personal_note.groups_of_person.set(self.member_of.all()) lesson_period=lesson_period,
week=wanted_week.week,
if remarks: year=wanted_week.year,
if personal_note.remarks: defaults={
personal_note.remarks += "; %s" % remarks "absent": absent,
else: "excused": excused,
personal_note.remarks = remarks "excuse_type": excuse_type,
personal_note.save() },
)
personal_note.groups_of_person.set(self.member_of.all())
if remarks:
if personal_note.remarks:
personal_note.remarks += "; %s" % remarks
else:
personal_note.remarks = remarks
personal_note.save()
@LessonPeriod.method @LessonPeriod.method
......
from django.db import models from django.db import models
from django.utils.formats import date_format
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from calendarweek import CalendarWeek from calendarweek import CalendarWeek
...@@ -82,6 +83,25 @@ class PersonalNote(ExtensibleModel, WeekRelatedMixin): ...@@ -82,6 +83,25 @@ class PersonalNote(ExtensibleModel, WeekRelatedMixin):
self.excuse_type = None self.excuse_type = None
super().save(*args, **kwargs) super().save(*args, **kwargs)
def reset_values(self):
"""Reset all saved data to default values.
.. warning ::
This won't save the data, please execute ``save`` extra.
"""
defaults = PersonalNote()
self.absent = defaults.absent
self.late = defaults.late
self.excused = defaults.excused
self.excuse_type = defaults.excuse_type
self.remarks = defaults.remarks
self.extra_marks.clear()
def __str__(self):
return f"{date_format(self.date)}, {self.lesson_period}, {self.person}"
class Meta: class Meta:
verbose_name = _("Personal note") verbose_name = _("Personal note")
verbose_name_plural = _("Personal notes") verbose_name_plural = _("Personal notes")
......
...@@ -32,6 +32,10 @@ ...@@ -32,6 +32,10 @@
{% trans "Mark as" %} {% trans "Mark as" %}
<input type="hidden" value="{{ note.pk }}" name="personal_note"> <input type="hidden" value="{{ note.pk }}" name="personal_note">
{% include "alsijil/partials/mark_as_buttons.html" %} {% include "alsijil/partials/mark_as_buttons.html" %}
<a class="btn-flat red-text" title="{% trans "Delete note" %}"
href="{% url "delete_personal_note" note.pk %}">
<i class="material-icons center">cancel</i>
</a>
</form> </form>
{% endif %} {% endif %}
<i class="material-icons left red-text">warning</i> <i class="material-icons left red-text">warning</i>
...@@ -47,6 +51,10 @@ ...@@ -47,6 +51,10 @@
{% trans "Mark as" %} {% trans "Mark as" %}
<input type="hidden" value="{{ note.pk }}" name="personal_note"> <input type="hidden" value="{{ note.pk }}" name="personal_note">
{% include "alsijil/partials/mark_as_buttons.html" %} {% include "alsijil/partials/mark_as_buttons.html" %}
<a class="btn-flat red-text" title="{% trans "Delete note" %}"
href="{% url "delete_personal_note" note.pk %}">
<i class="material-icons center">cancel</i>
</a>
</form> </form>
{% endif %} {% endif %}
</li> </li>
...@@ -167,6 +175,10 @@ ...@@ -167,6 +175,10 @@
{% trans "Mark as" %} {% trans "Mark as" %}
<input type="hidden" value="{{ note.pk }}" name="personal_note"> <input type="hidden" value="{{ note.pk }}" name="personal_note">
{% include "alsijil/partials/mark_as_buttons.html" %} {% include "alsijil/partials/mark_as_buttons.html" %}
<a class="btn-flat red-text" title="{% trans "Delete note" %}"
href="{% url "delete_personal_note" note.pk %}">
<i class="material-icons center">cancel</i>
</a>
</form> </form>
{% endif %} {% endif %}
...@@ -205,6 +217,10 @@ ...@@ -205,6 +217,10 @@
{% trans "Mark as" %} {% trans "Mark as" %}
<input type="hidden" value="{{ note.pk }}" name="personal_note"> <input type="hidden" value="{{ note.pk }}" name="personal_note">
{% include "alsijil/partials/mark_as_buttons.html" %} {% include "alsijil/partials/mark_as_buttons.html" %}
<a class="btn-flat red-text" title="{% trans "Delete note" %}"
href="{% url "delete_personal_note" note.pk %}">
<i class="material-icons center">cancel</i>
</a>
</form> </form>
{% endif %} {% endif %}
</div> </div>
......
...@@ -29,6 +29,11 @@ urlpatterns = [ ...@@ -29,6 +29,11 @@ urlpatterns = [
path("persons/", views.my_students, name="my_students"), path("persons/", views.my_students, name="my_students"),
path("persons/<int:id_>/", views.overview_person, name="overview_person"), path("persons/<int:id_>/", views.overview_person, name="overview_person"),
path("me/", views.overview_person, name="overview_me"), path("me/", views.overview_person, name="overview_me"),
path(
"notes/<int:pk>/delete/",
views.DeletePersonalNoteView.as_view(),
name="delete_personal_note",
),
path("absence/new", views.register_absence, name="register_absence"), path("absence/new", views.register_absence, name="register_absence"),
path("extra_marks/", views.ExtraMarkListView.as_view(), name="extra_marks"), path("extra_marks/", views.ExtraMarkListView.as_view(), name="extra_marks"),
path( path(
......
...@@ -7,7 +7,9 @@ from django.http import Http404, HttpRequest, HttpResponse, HttpResponseNotFound ...@@ -7,7 +7,9 @@ from django.http import Http404, HttpRequest, HttpResponse, HttpResponseNotFound
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.views.generic import DetailView
import reversion
from calendarweek import CalendarWeek from calendarweek import CalendarWeek
from django_tables2 import SingleTableView from django_tables2 import SingleTableView
from reversion.views import RevisionMixin from reversion.views import RevisionMixin
...@@ -131,7 +133,8 @@ def lesson( ...@@ -131,7 +133,8 @@ def lesson(
if personal_note_formset.is_valid() and request.user.has_perm( if personal_note_formset.is_valid() and request.user.has_perm(
"alsijil.edit_lesson_personalnote", lesson_period "alsijil.edit_lesson_personalnote", lesson_period
): ):
instances = personal_note_formset.save() with reversion.create_revision():
instances = personal_note_formset.save()
# Iterate over personal notes and carry changed absences to following lessons # Iterate over personal notes and carry changed absences to following lessons
for instance in instances: for instance in instances:
...@@ -530,7 +533,12 @@ def overview_person(request: HttpRequest, id_: Optional[int] = None) -> HttpResp ...@@ -530,7 +533,12 @@ def overview_person(request: HttpRequest, id_: Optional[int] = None) -> HttpResp
absent=True, absent=True,
excused=False, excused=False,
) )
notes.update(excused=True, excuse_type=excuse_type) for note in notes:
note.excused = True
note.excuse_type = excuse_type
with reversion.create_revision():
note.save()
messages.success( messages.success(
request, _("The absences have been marked as excused.") request, _("The absences have been marked as excused.")
) )
...@@ -547,7 +555,8 @@ def overview_person(request: HttpRequest, id_: Optional[int] = None) -> HttpResp ...@@ -547,7 +555,8 @@ def overview_person(request: HttpRequest, id_: Optional[int] = None) -> HttpResp
if note.absent: if note.absent:
note.excused = True note.excused = True
note.excuse_type = excuse_type note.excuse_type = excuse_type
note.save() with reversion.create_revision():
note.save()
messages.success( messages.success(
request, _("The absence has been marked as excused.") request, _("The absence has been marked as excused.")
) )
...@@ -678,6 +687,19 @@ def register_absence(request: HttpRequest) -> HttpResponse: ...@@ -678,6 +687,19 @@ def register_absence(request: HttpRequest) -> HttpResponse:
return render(request, "alsijil/absences/register.html", context) return render(request, "alsijil/absences/register.html", context)
class DeletePersonalNoteView(DetailView):
model = PersonalNote
template_name = "core/pages/delete.html"
def post(self, request, *args, **kwargs):
note = self.get_object()
with reversion.create_revision():
note.reset_values()
note.save()
messages.success(request, _("The personal note has been deleted."))
return redirect("overview_person", note.person.pk)
class ExtraMarkListView(PermissionRequiredMixin, SingleTableView): class ExtraMarkListView(PermissionRequiredMixin, SingleTableView):
"""Table of all extra marks.""" """Table of all extra marks."""
......
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