From 8db28b983864e9f3553788b885e69b476424aecb Mon Sep 17 00:00:00 2001 From: Faraphel Date: Thu, 14 Dec 2023 22:56:22 +0100 Subject: [PATCH] added create absence page (missing attachments) --- Palto/Palto/admin.py | 2 +- Palto/Palto/api/v1/serializers.py | 2 +- Palto/Palto/forms.py | 20 +++++++ Palto/Palto/models.py | 2 +- Palto/Palto/templates/Palto/absence_new.html | 11 ++++ .../Palto/{absence.html => absence_view.html} | 6 +- Palto/Palto/templates/Palto/login.html | 6 +- Palto/Palto/templates/Palto/profile.html | 4 +- .../Palto/teaching_session_list.html | 4 +- ...ession.html => teaching_session_view.html} | 4 +- ...hing_unit.html => teaching_unit_view.html} | 2 +- Palto/Palto/urls.py | 8 +-- Palto/Palto/views.py | 56 +++++++++++++++---- Palto/settings.py | 2 +- 14 files changed, 99 insertions(+), 30 deletions(-) create mode 100644 Palto/Palto/templates/Palto/absence_new.html rename Palto/Palto/templates/Palto/{absence.html => absence_view.html} (76%) rename Palto/Palto/templates/Palto/{teaching_session.html => teaching_session_view.html} (90%) rename Palto/Palto/templates/Palto/{teaching_unit.html => teaching_unit_view.html} (94%) diff --git a/Palto/Palto/admin.py b/Palto/Palto/admin.py index 10d3d9f..67d2e10 100644 --- a/Palto/Palto/admin.py +++ b/Palto/Palto/admin.py @@ -9,7 +9,7 @@ from django.contrib import admin from . import models -# TODO: plus de list_filter sur "department" ? +# TODO(Faraphel): plus de list_filter sur "department" ? # Register your models here. diff --git a/Palto/Palto/api/v1/serializers.py b/Palto/Palto/api/v1/serializers.py index f0aadef..fb9ef23 100644 --- a/Palto/Palto/api/v1/serializers.py +++ b/Palto/Palto/api/v1/serializers.py @@ -12,7 +12,7 @@ from rest_framework.exceptions import PermissionDenied from Palto.Palto import models -# TODO: voir les relations inversées ? +# TODO(Faraphel): voir les relations inversées ? class ModelSerializerContrains(serializers.ModelSerializer): diff --git a/Palto/Palto/forms.py b/Palto/Palto/forms.py index 98c65bc..eb9aebd 100644 --- a/Palto/Palto/forms.py +++ b/Palto/Palto/forms.py @@ -1,6 +1,26 @@ from django import forms +from Palto.Palto import models + + +# Users + class LoginForm(forms.Form): username = forms.CharField() password = forms.CharField(widget=forms.PasswordInput) + + +# Objects + + +class NewAbsenceForm(forms.Form): + department = forms.ModelChoiceField(queryset=None) + start = forms.DateTimeField(widget=forms.TextInput(attrs=dict(type='datetime-local'))) + end = forms.DateTimeField(widget=forms.TextInput(attrs=dict(type='datetime-local'))) + message = forms.CharField(widget=forms.Textarea) + + def __init__(self, student: models.User, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.fields["department"].queryset = student.studying_departments.all() diff --git a/Palto/Palto/models.py b/Palto/Palto/models.py index ba54285..28ad2f6 100644 --- a/Palto/Palto/models.py +++ b/Palto/Palto/models.py @@ -15,7 +15,7 @@ from django.db import models from django.db.models import QuerySet, Q, F -# TODO(Raphaël): split permissions from models for readability +# TODO(Faraphel): split permissions from models for readability class ModelPermissionHelper: diff --git a/Palto/Palto/templates/Palto/absence_new.html b/Palto/Palto/templates/Palto/absence_new.html new file mode 100644 index 0000000..f9dcb66 --- /dev/null +++ b/Palto/Palto/templates/Palto/absence_new.html @@ -0,0 +1,11 @@ +{% extends "Palto/base.html" %} + +{% block body %} +
+ {% csrf_token %} + + {{ form_new_absence.as_table }} +
+ +
+{% endblock %} diff --git a/Palto/Palto/templates/Palto/absence.html b/Palto/Palto/templates/Palto/absence_view.html similarity index 76% rename from Palto/Palto/templates/Palto/absence.html rename to Palto/Palto/templates/Palto/absence_view.html index 0ade496..6f93217 100644 --- a/Palto/Palto/templates/Palto/absence.html +++ b/Palto/Palto/templates/Palto/absence_view.html @@ -9,11 +9,11 @@ Département - {{ absence.department }} + {{ absence.department }} - Etudiant - {{ absence.student }} + Étudiant + {{ absence.student }} Période diff --git a/Palto/Palto/templates/Palto/login.html b/Palto/Palto/templates/Palto/login.html index 8672a95..d14cca1 100644 --- a/Palto/Palto/templates/Palto/login.html +++ b/Palto/Palto/templates/Palto/login.html @@ -3,7 +3,9 @@ {% block body %}
{% csrf_token %} - {{ form_login }} - + + {{ form_login.as_table }} +
+
{% endblock %} \ No newline at end of file diff --git a/Palto/Palto/templates/Palto/profile.html b/Palto/Palto/templates/Palto/profile.html index aaf8ad5..d2b55a3 100644 --- a/Palto/Palto/templates/Palto/profile.html +++ b/Palto/Palto/templates/Palto/profile.html @@ -29,7 +29,7 @@ Responsable d'UE {% for managing_unit in managing_units %} - + {{ managing_unit.name }} {% if not forloop.last %}
{% endif %} @@ -45,7 +45,7 @@ Enseignant {% for teaching_unit in teaching_units %} - + {{ teaching_unit.name }} {% if not forloop.last %}
{% endif %} diff --git a/Palto/Palto/templates/Palto/teaching_session_list.html b/Palto/Palto/templates/Palto/teaching_session_list.html index 538d830..715d933 100644 --- a/Palto/Palto/templates/Palto/teaching_session_list.html +++ b/Palto/Palto/templates/Palto/teaching_session_list.html @@ -16,8 +16,8 @@ {# show the information for every session #} {% for session in sessions %} - {{ session.short_id }} - {{ session.unit.name }} + {{ session.short_id }} + {{ session.unit.name }} {{ session.start }}
{{ session.end }} {{ session.teacher }} {{ session.attendances.all|length }} / {{ session.group.students.all|length }} diff --git a/Palto/Palto/templates/Palto/teaching_session.html b/Palto/Palto/templates/Palto/teaching_session_view.html similarity index 90% rename from Palto/Palto/templates/Palto/teaching_session.html rename to Palto/Palto/templates/Palto/teaching_session_view.html index e61df75..3a26783 100644 --- a/Palto/Palto/templates/Palto/teaching_session.html +++ b/Palto/Palto/templates/Palto/teaching_session_view.html @@ -18,7 +18,7 @@ Unité d'Enseignement - {{ session.unit }} + {{ session.unit }} Enseignant @@ -53,7 +53,7 @@ {% with absence=session_student_data|dict_get:"absence" %} {% if absence != None %} - Détails + Détails {% endif %} {% endwith %} diff --git a/Palto/Palto/templates/Palto/teaching_unit.html b/Palto/Palto/templates/Palto/teaching_unit_view.html similarity index 94% rename from Palto/Palto/templates/Palto/teaching_unit.html rename to Palto/Palto/templates/Palto/teaching_unit_view.html index bb8f173..084b84e 100644 --- a/Palto/Palto/templates/Palto/teaching_unit.html +++ b/Palto/Palto/templates/Palto/teaching_unit_view.html @@ -14,7 +14,7 @@ Département - {{ unit.department.name }} + {{ unit.department.name }} Mail diff --git a/Palto/Palto/urls.py b/Palto/Palto/urls.py index e848bbf..513d372 100644 --- a/Palto/Palto/urls.py +++ b/Palto/Palto/urls.py @@ -20,13 +20,13 @@ urlpatterns = [ path("profile//", views.profile_view, name="profile"), # Units - path("teaching_units//", views.teaching_unit_view, name="teaching_unit"), + path("teaching_units/view//", views.teaching_unit_view, name="teaching_unit_view"), # Sessions path("teaching_sessions/", views.teaching_session_list_view, name="teaching_session_list"), - path("teaching_sessions//", views.teaching_session_view, name="teaching_session"), + path("teaching_sessions/view//", views.teaching_session_view, name="teaching_session_view"), # Absences - path("absences//", views.absence_view, name="absence"), - # TODO: new absence + path("absences/view//", views.absence_view, name="absence_view"), + path("absences/new/", views.new_absence_view, name="absence_new"), ] diff --git a/Palto/Palto/views.py b/Palto/Palto/views.py index f7886f5..7e0bbb7 100644 --- a/Palto/Palto/views.py +++ b/Palto/Palto/views.py @@ -8,11 +8,11 @@ from django.contrib.auth import login, authenticate, logout from django.contrib.auth.decorators import login_required from django.core.handlers.wsgi import WSGIRequest from django.core.paginator import Paginator +from django.db import IntegrityError from django.http import HttpResponseForbidden from django.shortcuts import render, get_object_or_404, redirect -from Palto.Palto import models -from Palto.Palto.forms import LoginForm +from Palto.Palto import models, forms from Palto.Palto.utils import get_object_or_none ELEMENT_PER_PAGE: int = 30 @@ -25,7 +25,7 @@ def homepage_view(request: WSGIRequest): def login_view(request: WSGIRequest): # create a login form - form_login = LoginForm(request.POST) + form_login = forms.LoginForm(request.POST) if form_login.is_valid(): # try to authenticate this user with the credentials @@ -125,13 +125,13 @@ def teaching_unit_view(request: WSGIRequest, unit_id: uuid.UUID): # check if the user is allowed to see this specific object if unit not in models.TeachingUnit.all_visible_by_user(request.user): - # TODO: syntaxic sugar session.visible_by_user(request.user) + # TODO(Faraphel): syntaxic sugar session.visible_by_user(request.user) return HttpResponseForbidden() # render the page return render( request, - "Palto/teaching_unit.html", + "Palto/teaching_unit_view.html", context=dict( unit=unit, ) @@ -144,7 +144,7 @@ def teaching_session_view(request: WSGIRequest, session_id: uuid.UUID): # check if the user is allowed to see this specific object if session not in models.TeachingSession.all_visible_by_user(request.user): - # TODO: syntaxic sugar session.visible_by_user(request.user) + # TODO(Faraphel): syntaxic sugar session.visible_by_user(request.user) return HttpResponseForbidden() # prepare the data and the "complex" query for the template @@ -159,7 +159,7 @@ def teaching_session_view(request: WSGIRequest, session_id: uuid.UUID): models.Absence.objects, student=student, start__lte=session.start, end__gte=session.end - ), # TODO: property ? + ), # TODO(Faraphel): property ? } for student in session.group.students.all() @@ -168,7 +168,7 @@ def teaching_session_view(request: WSGIRequest, session_id: uuid.UUID): # render the page return render( request, - "Palto/teaching_session.html", + "Palto/teaching_session_view.html", context=dict( session=session, session_students_data=session_students_data, @@ -182,14 +182,50 @@ def absence_view(request: WSGIRequest, absence_id: uuid.UUID): # check if the user is allowed to see this specific object if absence not in models.Absence.all_visible_by_user(request.user): - # TODO: syntaxic sugar session.visible_by_user(request.user) + # TODO(Faraphel): syntaxic sugar session.visible_by_user(request.user) return HttpResponseForbidden() # render the page return render( request, - "Palto/absence.html", + "Palto/absence_view.html", context=dict( absence=absence, ) ) + + +@login_required +def new_absence_view(request: WSGIRequest): + # check if the user can create an absence + if not models.Absence.can_user_create(request.user): + return HttpResponseForbidden() + + # create a form for the new absence + form_new_absence = forms.NewAbsenceForm(request.user, request.POST) + + if form_new_absence.is_valid(): + try: + models.Absence.objects.create( + student=request.user, + start=form_new_absence.cleaned_data["start"], + end=form_new_absence.cleaned_data["end"], + department=form_new_absence.cleaned_data["department"], + message=form_new_absence.cleaned_data["message"], + ) + except IntegrityError: + form_new_absence.add_error(None, "This absence already exists.") + + else: + return redirect("Palto:homepage") # TODO(Faraphel): redirect to absence list + + # TODO(Faraphel): add attachments to the forms + + # render the page + return render( + request, + "Palto/absence_new.html", + context=dict( + form_new_absence=form_new_absence, + ) + ) diff --git a/Palto/settings.py b/Palto/settings.py index 32f44ff..efe4b7a 100644 --- a/Palto/settings.py +++ b/Palto/settings.py @@ -191,7 +191,7 @@ AUTH_USER_MODEL = "Palto.User" # CORS settings -# TODO(Raphaël): Only in debug ! +# TODO(Faraphel): Only in debug ! CORS_ORIGIN_ALLOW_ALL = True