From f227705e283d290e8e34f9bbfc9a9c945c1e3053 Mon Sep 17 00:00:00 2001 From: yann Date: Tue, 22 Apr 2025 12:23:50 +0200 Subject: [PATCH] authentication works, navbar to adjust, models started not done --- LITReview/LITReview/settings.py | 12 +++++- LITReview/LITReview/urls.py | 17 ++++++++ LITReview/authentication/forms.py | 14 +++++++ .../migrations/0002_remove_user_account_id.py | 17 ++++++++ LITReview/authentication/models.py | 3 +- .../templates/authentication/login.html | 41 +++++++++++++++++++ .../templates/authentication/logout.html | 0 .../templates/authentication/register.html | 22 ++++++++++ LITReview/authentication/views.py | 41 ++++++++++++++++++- LITReview/reviews/models.py | 37 ++++++++++++++++- LITReview/reviews/templates/reviews/flux.html | 12 ++++++ LITReview/reviews/templates/reviews/home.html | 17 ++++++++ .../reviews/templates/reviews/posts.html | 12 ++++++ .../reviews/templates/reviews/subscribed.html | 12 ++++++ LITReview/reviews/views.py | 14 ++++++- LITReview/templates/base.html | 24 +++++++++++ 16 files changed, 288 insertions(+), 7 deletions(-) create mode 100644 LITReview/authentication/forms.py create mode 100644 LITReview/authentication/migrations/0002_remove_user_account_id.py create mode 100644 LITReview/authentication/templates/authentication/login.html create mode 100644 LITReview/authentication/templates/authentication/logout.html create mode 100644 LITReview/authentication/templates/authentication/register.html create mode 100644 LITReview/reviews/templates/reviews/flux.html create mode 100644 LITReview/reviews/templates/reviews/home.html create mode 100644 LITReview/reviews/templates/reviews/posts.html create mode 100644 LITReview/reviews/templates/reviews/subscribed.html create mode 100644 LITReview/templates/base.html diff --git a/LITReview/LITReview/settings.py b/LITReview/LITReview/settings.py index 514b2d8..cbdeeda 100644 --- a/LITReview/LITReview/settings.py +++ b/LITReview/LITReview/settings.py @@ -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' + + diff --git a/LITReview/LITReview/urls.py b/LITReview/LITReview/urls.py index cbeba0f..5e99172 100644 --- a/LITReview/LITReview/urls.py +++ b/LITReview/LITReview/urls.py @@ -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'), ] diff --git a/LITReview/authentication/forms.py b/LITReview/authentication/forms.py new file mode 100644 index 0000000..bb5019d --- /dev/null +++ b/LITReview/authentication/forms.py @@ -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 d’utilisateur') + 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',) + diff --git a/LITReview/authentication/migrations/0002_remove_user_account_id.py b/LITReview/authentication/migrations/0002_remove_user_account_id.py new file mode 100644 index 0000000..b60a08a --- /dev/null +++ b/LITReview/authentication/migrations/0002_remove_user_account_id.py @@ -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', + ), + ] diff --git a/LITReview/authentication/models.py b/LITReview/authentication/models.py index 3ccfc56..696b770 100644 --- a/LITReview/authentication/models.py +++ b/LITReview/authentication/models.py @@ -3,5 +3,4 @@ from django.contrib.auth.models import AbstractUser class User(AbstractUser): - account_id = models.CharField(max_length=10, unique=True) - + pass diff --git a/LITReview/authentication/templates/authentication/login.html b/LITReview/authentication/templates/authentication/login.html new file mode 100644 index 0000000..724ed81 --- /dev/null +++ b/LITReview/authentication/templates/authentication/login.html @@ -0,0 +1,41 @@ +{% extends 'base.html' %} + +{% block content %} + +
+
+
+ +
+
+

Inscrivez-vous

+
+ + +
+
+ +
+
+
+

Connectez-vous

+
+ {% csrf_token %} +
+ {{ form.username }} +
+
+ {{ form.password }} +
+ +
+
+
+
+ +
+
+ +{% endblock %} diff --git a/LITReview/authentication/templates/authentication/logout.html b/LITReview/authentication/templates/authentication/logout.html new file mode 100644 index 0000000..e69de29 diff --git a/LITReview/authentication/templates/authentication/register.html b/LITReview/authentication/templates/authentication/register.html new file mode 100644 index 0000000..d6e909e --- /dev/null +++ b/LITReview/authentication/templates/authentication/register.html @@ -0,0 +1,22 @@ +{% extends 'base.html' %} +{% block content %} +
+
+
+

Inscrivez-vous

+
+
+
+
+ Retourner +
+
+
+ {% csrf_token %} + {{ form.as_p }} + +
+
+
+
+{% endblock %} diff --git a/LITReview/authentication/views.py b/LITReview/authentication/views.py index 91ea44a..c3eeceb 100644 --- a/LITReview/authentication/views.py +++ b/LITReview/authentication/views.py @@ -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}) diff --git a/LITReview/reviews/models.py b/LITReview/reviews/models.py index 71a8362..c6130aa 100644 --- a/LITReview/reviews/models.py +++ b/LITReview/reviews/models.py @@ -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 diff --git a/LITReview/reviews/templates/reviews/flux.html b/LITReview/reviews/templates/reviews/flux.html new file mode 100644 index 0000000..db4e9f7 --- /dev/null +++ b/LITReview/reviews/templates/reviews/flux.html @@ -0,0 +1,12 @@ +{% extends 'base.html' %} +{% block nav %} + +{% endblock %} +{% block content %} +

Flux

+{% endblock %} diff --git a/LITReview/reviews/templates/reviews/home.html b/LITReview/reviews/templates/reviews/home.html new file mode 100644 index 0000000..f92c699 --- /dev/null +++ b/LITReview/reviews/templates/reviews/home.html @@ -0,0 +1,17 @@ +{% extends 'base.html' %} +{% block nav %} + + + +{% endblock %} +{% block content %} +

Bienvenue {{ user.name }}

+{% endblock %} diff --git a/LITReview/reviews/templates/reviews/posts.html b/LITReview/reviews/templates/reviews/posts.html new file mode 100644 index 0000000..1d79920 --- /dev/null +++ b/LITReview/reviews/templates/reviews/posts.html @@ -0,0 +1,12 @@ +{% extends 'base.html' %} +{% block nav %} + +{% endblock %} +{% block content %} +

Posts

+{% endblock %} diff --git a/LITReview/reviews/templates/reviews/subscribed.html b/LITReview/reviews/templates/reviews/subscribed.html new file mode 100644 index 0000000..abe6b15 --- /dev/null +++ b/LITReview/reviews/templates/reviews/subscribed.html @@ -0,0 +1,12 @@ +{% extends 'base.html' %} +{% block nav %} + +{% endblock %} +{% block content %} +

Subscribed

+{% endblock %} diff --git a/LITReview/reviews/views.py b/LITReview/reviews/views.py index 91ea44a..04c2d57 100644 --- a/LITReview/reviews/views.py +++ b/LITReview/reviews/views.py @@ -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') + + diff --git a/LITReview/templates/base.html b/LITReview/templates/base.html new file mode 100644 index 0000000..2012205 --- /dev/null +++ b/LITReview/templates/base.html @@ -0,0 +1,24 @@ + + + + + + LITReview + + + + +
+
+
+

LITReview

+
+
+ {% block nav %}{% endblock %} +
+ + {% block content %}{% endblock %} + + +