diff --git a/Palto/Palto/api/urls.py b/Palto/Palto/api/urls.py index 23f5062..ff33307 100644 --- a/Palto/Palto/api/urls.py +++ b/Palto/Palto/api/urls.py @@ -9,6 +9,8 @@ from rest_framework_simplejwt.views import TokenRefreshView, TokenObtainPairView import Palto.Palto.api.v1.urls as v1_urls +app_name = "PaltoAPI" + urlpatterns = [ # Authentification (JWT) path('auth/jwt/token/', TokenObtainPairView.as_view(), name='token'), diff --git a/Palto/Palto/api/v1/urls.py b/Palto/Palto/api/v1/urls.py index 221ea5f..2c54563 100644 --- a/Palto/Palto/api/v1/urls.py +++ b/Palto/Palto/api/v1/urls.py @@ -9,6 +9,8 @@ from rest_framework import routers from . import views +app_name = "PaltoAPIv1" + router = routers.DefaultRouter() router.register(r'users', views.UserViewSet, basename="User") @@ -21,5 +23,4 @@ router.register(r'attendances', views.AttendanceViewSet, basename="Attendance") router.register(r'absences', views.AbsenceViewSet, basename="Absence") router.register(r'absence_attachments', views.AbsenceAttachmentViewSet, basename="AbsenceAttachment") - urlpatterns = router.urls diff --git a/Palto/Palto/forms.py b/Palto/Palto/forms.py new file mode 100644 index 0000000..98c65bc --- /dev/null +++ b/Palto/Palto/forms.py @@ -0,0 +1,6 @@ +from django import forms + + +class LoginForm(forms.Form): + username = forms.CharField() + password = forms.CharField(widget=forms.PasswordInput) diff --git a/Palto/Palto/static/Palto/favicon.svg b/Palto/Palto/static/Palto/favicon.svg new file mode 100644 index 0000000..212b8b1 --- /dev/null +++ b/Palto/Palto/static/Palto/favicon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Palto/Palto/templates/Palto/base.html b/Palto/Palto/templates/Palto/base.html new file mode 100644 index 0000000..f6d2f27 --- /dev/null +++ b/Palto/Palto/templates/Palto/base.html @@ -0,0 +1,20 @@ +{% load static %} + + + + + + + + + {% block title %}Palto{% endblock %} + + + {# navigation #} + {% include "Palto/navigation.html" %} + + {# body #} + {% block body %} + {% endblock %} + + diff --git a/Palto/Palto/templates/Palto/login.html b/Palto/Palto/templates/Palto/login.html new file mode 100644 index 0000000..8672a95 --- /dev/null +++ b/Palto/Palto/templates/Palto/login.html @@ -0,0 +1,9 @@ +{% extends "Palto/base.html" %} + +{% block body %} +
+ {% csrf_token %} + {{ form_login }} + +
+{% endblock %} \ No newline at end of file diff --git a/Palto/Palto/templates/Palto/navigation.html b/Palto/Palto/templates/Palto/navigation.html new file mode 100644 index 0000000..bb4c86f --- /dev/null +++ b/Palto/Palto/templates/Palto/navigation.html @@ -0,0 +1,3 @@ + diff --git a/Palto/Palto/templates/Palto/profile.html b/Palto/Palto/templates/Palto/profile.html new file mode 100644 index 0000000..177ba14 --- /dev/null +++ b/Palto/Palto/templates/Palto/profile.html @@ -0,0 +1,9 @@ +{% extends "Palto/base.html" %} + +{% block body %} +
+ {{ profile.username }} + {{ profile.email }} + {% if profile.is_superuser %}Administrator{% endif %} +
+{% endblock %} diff --git a/Palto/Palto/urls.py b/Palto/Palto/urls.py new file mode 100644 index 0000000..19c0b30 --- /dev/null +++ b/Palto/Palto/urls.py @@ -0,0 +1,21 @@ +""" +Urls for the Palto project's API. + +This file list all the urls for the Palto API. +""" + +from django.urls import path +from Palto.Palto import views + +app_name = "Palto" + +urlpatterns = [ + # Base + path("", views.homepage_view, name="homepage"), + + # User + path("login/", views.login_view, name="login"), + path("logout/", views.logout_view, name="logout"), + path("profile/", views.profile_view, name="profile"), + path("profile//", views.profile_view, name="profile"), +] diff --git a/Palto/Palto/views.py b/Palto/Palto/views.py index 49785f0..535aabc 100644 --- a/Palto/Palto/views.py +++ b/Palto/Palto/views.py @@ -3,7 +3,75 @@ Views for the Palto project. A view is what control the content of a page, prepare the correct data, react to a form, render the correct template. """ +import uuid +from django.contrib.auth import login, authenticate, logout +from django.contrib.auth.decorators import login_required +from django.http import HttpRequest, HttpResponse +from django.shortcuts import render, get_object_or_404, redirect + +from Palto.Palto.forms import LoginForm +from Palto.Palto.models import User -from django.shortcuts import render # Create your views here. +def homepage_view(request: HttpRequest): + # TODO: homepage + return HttpResponse("Hello there.") + + +def login_view(request: HttpRequest): + # create a login form + form_login = LoginForm(request.POST) + + if form_login.is_valid(): + # try to authenticate this user with the credentials + user = authenticate( + username=form_login.cleaned_data["username"], + password=form_login.cleaned_data["password"] + ) + + if user is not None: + # if the user was authenticated, log the user in. + login(request, user) + # redirect him to the main page + return redirect("Palto:homepage") + + else: + # otherwise the credentials were invalid. + form_login.add_error(field=None, error="Invalid credentials.") + + # return the page + return render( + request, + "Palto/login.html", + context=dict( + form_login=form_login + ) + ) + + +@login_required +def logout_view(request: HttpRequest): + # disconnect the user from the website + logout(request) + # redirect him to the main page + return redirect("Palto:homepage") + + +@login_required +def profile_view(request: HttpRequest, profile_id: uuid.UUID = None): + if profile_id is None: + # if the profile id is not given, redirect to the page of the current user. + return redirect("Palto:profile", request.user.id) + + # get the corresponding user from its id. + profile = get_object_or_404(User, id=profile_id) + + # render the page + return render( + request, + "Palto/profile.html", + context=dict( + profile=profile + ) + ) diff --git a/Palto/urls.py b/Palto/urls.py index 463604a..0697362 100644 --- a/Palto/urls.py +++ b/Palto/urls.py @@ -19,16 +19,17 @@ from django.contrib import admin from django.urls import path, re_path, include from django.views.static import serve -from Palto.Palto.api import urls as api_urls from Palto import settings +from Palto.Palto import urls as palto_views_urls +from Palto.Palto.api import urls as palto_api_urls urlpatterns = [ # Application - # ... + path('', include(palto_views_urls)), # API - path('api/', include(api_urls)), # Api REST + path('api/', include(palto_api_urls)), # Api REST # Debug path('admin/', admin.site.urls), # Admin page