implemented very basic login, logout and profile pages

This commit is contained in:
Faraphel 2023-12-10 17:09:56 +01:00
parent 5c588b4cc9
commit cfa65daa19
11 changed files with 146 additions and 5 deletions

View file

@ -9,6 +9,8 @@ from rest_framework_simplejwt.views import TokenRefreshView, TokenObtainPairView
import Palto.Palto.api.v1.urls as v1_urls import Palto.Palto.api.v1.urls as v1_urls
app_name = "PaltoAPI"
urlpatterns = [ urlpatterns = [
# Authentification (JWT) # Authentification (JWT)
path('auth/jwt/token/', TokenObtainPairView.as_view(), name='token'), path('auth/jwt/token/', TokenObtainPairView.as_view(), name='token'),

View file

@ -9,6 +9,8 @@ from rest_framework import routers
from . import views from . import views
app_name = "PaltoAPIv1"
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet, basename="User") 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'absences', views.AbsenceViewSet, basename="Absence")
router.register(r'absence_attachments', views.AbsenceAttachmentViewSet, basename="AbsenceAttachment") router.register(r'absence_attachments', views.AbsenceAttachmentViewSet, basename="AbsenceAttachment")
urlpatterns = router.urls urlpatterns = router.urls

6
Palto/Palto/forms.py Normal file
View file

@ -0,0 +1,6 @@
from django import forms
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 6.3 KiB

View file

@ -0,0 +1,20 @@
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/png" href="{% static 'Palto/favicon.svg' %}"/>
<title>{% block title %}Palto{% endblock %}</title>
</head>
<body>
{# navigation #}
{% include "Palto/navigation.html" %}
{# body #}
{% block body %}
{% endblock %}
</body>
</html>

View file

@ -0,0 +1,9 @@
{% extends "Palto/base.html" %}
{% block body %}
<form method="POST">
{% csrf_token %}
{{ form_login }}
<input type="submit" value="Log in">
</form>
{% endblock %}

View file

@ -0,0 +1,3 @@
<nav>
Palto
</nav>

View file

@ -0,0 +1,9 @@
{% extends "Palto/base.html" %}
{% block body %}
<div>
{{ profile.username }}
{{ profile.email }}
{% if profile.is_superuser %}Administrator{% endif %}
</div>
{% endblock %}

21
Palto/Palto/urls.py Normal file
View file

@ -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/<uuid:profile_id>/", views.profile_view, name="profile"),
]

View file

@ -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. 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. # 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
)
)

View file

@ -19,16 +19,17 @@ from django.contrib import admin
from django.urls import path, re_path, include from django.urls import path, re_path, include
from django.views.static import serve from django.views.static import serve
from Palto.Palto.api import urls as api_urls
from Palto import settings from Palto import settings
from Palto.Palto import urls as palto_views_urls
from Palto.Palto.api import urls as palto_api_urls
urlpatterns = [ urlpatterns = [
# Application # Application
# ... path('', include(palto_views_urls)),
# API # API
path('api/', include(api_urls)), # Api REST path('api/', include(palto_api_urls)), # Api REST
# Debug # Debug
path('admin/', admin.site.urls), # Admin page path('admin/', admin.site.urls), # Admin page