authentication works, navbar to adjust, models started not done

This commit is contained in:
yann 2025-04-22 12:23:50 +02:00
parent ee6120147c
commit f227705e28
16 changed files with 288 additions and 7 deletions

View File

@ -56,7 +56,9 @@ ROOT_URLCONF = 'LITReview.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [
BASE_DIR.joinpath('templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
@ -123,6 +125,14 @@ STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_REDIRECT_URL = 'home'
LOGIN_URL = 'login'
LOGOUT_REDIRECT_URL = 'login'
AUTH_USER_MODEL = 'authentication.User'

View File

@ -16,7 +16,24 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from django.contrib.auth.views import LoginView, LogoutView, PasswordChangeView, PasswordChangeDoneView
import authentication.views, reviews.views
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', reviews.views.home, name='home'),
# path('', LoginView.as_view(
path('', authentication.views.login_page, name='login'),
# template_name='authentication/login.html',
# redirect_authenticated_user=True), name='login'),
path('pwd-change/', PasswordChangeView.as_view(
template_name='authentication/pwd_change.html'), name='pwd-change'),
path('pwd-change-done/', PasswordChangeDoneView.as_view(
template_name='authentication/pwd_change_done.html'), name='pwd-change-done'),
path('logout/', LogoutView.as_view(next_page='login'), name='logout'),
path('register/', authentication.views.register_page, name='register'),
path('flux/', reviews.views.flux, name='flux'),
path('posts/', reviews.views.posts, name='posts'),
path('subscribed/', reviews.views.subscribed, name='subscribed'),
]

View File

@ -0,0 +1,14 @@
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
class LoginForm(forms.Form):
username = forms.CharField(max_length=63, label='Nom dutilisateur')
password = forms.CharField(max_length=63, widget=forms.PasswordInput, label='Mot de passe')
class RegisterForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = get_user_model()
fields = ('username',)

View File

@ -0,0 +1,17 @@
# Generated by Django 5.2 on 2025-04-18 13:34
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('authentication', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='user',
name='account_id',
),
]

View File

@ -3,5 +3,4 @@ from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
account_id = models.CharField(max_length=10, unique=True)
pass

View File

@ -0,0 +1,41 @@
{% extends 'base.html' %}
{% block content %}
<div class="container" style="height: 100vh">
<div class="row h-100 align-items-center">
<div class="col d-flex align-items-center justify-content-center">
<div class="justify-content-center">
<div class="align-self-center">
<h2>Inscrivez-vous</h2>
</div>
<div class="justify-content-end">
<a href="{% url 'register' %}" type="button" class="btn btn-primary">S'inscrire</a>
</div>
</div>
</div>
<div class="col d-flex align-items-center justify-content-center">
<div class="row">
<div class="col">
<h2>Connectez-vous</h2>
<form method='post'>
{% csrf_token %}
<div class="form-group p-3">
{{ form.username }}
</div>
<div class="form-group p-3">
{{ form.password }}
</div>
<button type="submit" class="btn btn-primary self-align-right">Se connecter</button>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -0,0 +1,22 @@
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="row justify-content-center mt-10">
<div class="col text-center">
<h2>Inscrivez-vous</h2>
</div>
</div>
<div class="row justify-content-center">
<div class="col-3 d-flex align-items-end">
<a href="{% url 'login' %}" class="btn btn-primary">Retourner</a>
</div>
<div class="col-3 d-flex align-items-end">
<form method='post'>
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">S'inscrire</button>
</form>
</div>
</div>
</div>
{% endblock %}

View File

@ -1,3 +1,40 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.contrib.auth import login, logout, authenticate
from django.conf import settings
from django import forms
from authentication.forms import LoginForm, RegisterForm
# Create your views here.
def login_page(request):
form = LoginForm()
message = ""
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
user = authenticate(
username=form.cleaned_data['username'],
password=form.cleaned_data['password'],
)
if user is not None:
login(request, user)
return redirect('home')
else:
message = "Identifiants invalides"
print(request.POST)
return render(request,
'authentication/login.html',
{'form': form})
def register_page(request):
form = RegisterForm()
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect(settings.LOGIN_REDIRECT_URL)
return render(request, 'authentication/register.html', {'form': form})

View File

@ -1,3 +1,38 @@
from django.core.validators import MinValueValidator, MaxValueValidator
from django.conf import settings
from django.db import models
# Create your models here.
class Ticket(models.Model):
# Your Ticket model definition goes here
title = models.CharField(max_length=100)
topic = models.CharField(max_length=100)
body = models.CharField(max_length=8192)
user = models.ForeignKey(
to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
image = models.ImageField(upload_to="uploads/")
time_created = models.DateTimeField(auto_now_add=True)
class Review(models.Model):
ticket = models.ForeignKey(to=Ticket, on_delete=models.CASCADE)
rating = models.PositiveSmallIntegerField(
# validates that rating must be between 0 and 5
validators=[MinValueValidator(0), MaxValueValidator(5)])
headline = models.CharField(max_length=128)
body = models.CharField(max_length=8192, blank=True)
user = models.ForeignKey(
to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
time_created = models.DateTimeField(auto_now_add=True)
class UserFollows(models.Model):
# Your UserFollows model definition goes here
class Meta:
# ensures we don't get multiple UserFollows instances
# for unique user-user_followed pairs
pass
#unique_together = ('user', 'followed_user', )
pass

View File

@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% block nav %}
<nav>
<a href="{% url 'flux' %}">Flux</a>
<a href="{% url 'posts' %}">Posts</a>
<a href="{% url 'subscribed' %}">Abonnements</a>
<a href="{% url 'logout' %}">Se déconnecter</a>
</nav>
{% endblock %}
{% block content %}
<h2> Flux </h2>
{% endblock %}

View File

@ -0,0 +1,17 @@
{% extends 'base.html' %}
{% block nav %}
<nav class="navbar navbar-light ml-auto">
<a href="{% url 'flux' %}">Flux</a>
<a href="{% url 'posts' %}">Posts</a>
<a href="{% url 'subscribed' %}">Abonnements</a>
<form class="form-inline" method="post" action="{% url 'logout' %}">
{% csrf_token %}
<button type="submit" class="btn btn-light">Se déconnecter</button>
</form>
</nav>
{% endblock %}
{% block content %}
<h2> Bienvenue {{ user.name }}</h2>
{% endblock %}

View File

@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% block nav %}
<nav>
<a href="{% url 'flux' %}">Flux</a>
<a href="{% url 'posts' %}">Posts</a>
<a href="{% url 'subscribed' %}">Abonnements</a>
<a href="{% url 'logout' %}">Se déconnecter</a>
</nav>
{% endblock %}
{% block content %}
<h2> Posts </h2>
{% endblock %}

View File

@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% block nav %}
<nav>
<a href="{% url 'flux' %}">Flux</a>
<a href="{% url 'posts' %}">Posts</a>
<a href="{% url 'subscribed' %}">Abonnements</a>
<a href="{% url 'logout' %}">Se déconnecter</a>
</nav>
{% endblock %}
{% block content %}
<h2> Subscribed </h2>
{% endblock %}

View File

@ -1,3 +1,15 @@
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'reviews/home.html')
def flux(request):
return render(request, 'reviews/flux.html')
def posts(request):
return render(request, 'reviews/posts.html')
def subscribed(request):
return render(request, 'reviews/subscribed.html')

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> LITReview </title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.5/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-SgOJa3DmI69IUzQ2PVdRZhwQ+dy64/BUtbMJw1MZ8t5HZApcHrRKUc4W0kG879m7" crossorigin="anonymous">
<style>
</style>
</head>
<body>
<header>
<div class="container-fluid border border-3 border-secondary-subtle">
<div class="row text-center">
<h1> LITReview </h1>
</div>
</div>
{% block nav %}{% endblock %}
</header>
{% block content %}{% endblock %}
</body>
</html>