From 9bf9c5f62df1a6482ba27b897da498592b57e04f Mon Sep 17 00:00:00 2001 From: Luc SORIGNET Date: Thu, 13 Feb 2025 17:06:09 +0100 Subject: [PATCH] refactor: Modification de l'url de l'api Auth --- Back-End/Auth/urls.py | 9 ++-- Back-End/Auth/views.py | 73 +++++++++------------------- Back-End/GestionMessagerie/urls.py | 8 +-- Back-End/GestionMessagerie/views.py | 24 ++++----- Back-End/GestionNotification/urls.py | 2 +- Front-End/src/app/lib/authAction.js | 6 +-- Front-End/src/utils/Url.js | 5 +- 7 files changed, 52 insertions(+), 75 deletions(-) diff --git a/Back-End/Auth/urls.py b/Back-End/Auth/urls.py index 2616d2c..32b1331 100644 --- a/Back-End/Auth/urls.py +++ b/Back-End/Auth/urls.py @@ -2,7 +2,7 @@ from django.urls import path, re_path from . import views import Auth.views -from Auth.views import ProfileView, ProfileListView, SessionView, LoginView, SubscribeView, NewPasswordView, ResetPasswordView +from Auth.views import ProfileSimpleView, ProfileView, SessionView, LoginView, SubscribeView, NewPasswordView, ResetPasswordView urlpatterns = [ re_path(r'^csrf$', Auth.views.csrf, name='csrf'), @@ -10,12 +10,11 @@ urlpatterns = [ re_path(r'^login$', LoginView.as_view(), name="login"), re_path(r'^subscribe$', SubscribeView.as_view(), name='subscribe'), re_path(r'^newPassword$', NewPasswordView.as_view(), name='newPassword'), - re_path(r'^resetPassword/([a-zA-Z]+)$', ResetPasswordView.as_view(), name='resetPassword'), + re_path(r'^resetPassword/(?P[a-zA-Z]+)$', ResetPasswordView.as_view(), name='resetPassword'), re_path(r'^infoSession$', Auth.views.infoSession, name='infoSession'), - re_path(r'^profiles$', ProfileListView.as_view(), name="profile"), - re_path(r'^profile$', ProfileView.as_view(), name="profile"), - re_path(r'^profile/([0-9]+)$', ProfileView.as_view(), name="profile"), + re_path(r'^profiles$', ProfileView.as_view(), name="profile"), + re_path(r'^profiles/(?P[0-9]+)$', ProfileSimpleView.as_view(), name="profile"), # Test SESSION VIEW re_path(r'^session$', SessionView.as_view(), name="session"), diff --git a/Back-End/Auth/views.py b/Back-End/Auth/views.py index eb14f77..52536b2 100644 --- a/Back-End/Auth/views.py +++ b/Back-End/Auth/views.py @@ -10,6 +10,9 @@ from rest_framework.views import APIView from rest_framework.parsers import JSONParser from rest_framework import status +from drf_yasg.utils import swagger_auto_schema +from drf_yasg import openapi + from datetime import datetime import jwt import json @@ -53,20 +56,12 @@ class SessionView(APIView): except jwt.InvalidTokenError: return JsonResponse({"error": "Invalid token"}, status=status.HTTP_401_UNAUTHORIZED) -class ProfileListView(APIView): +class ProfileView(APIView): def get(self, request): profilsList = bdd.getAllObjects(_objectName=Profile) profils_serializer = ProfileSerializer(profilsList, many=True) return JsonResponse(profils_serializer.data, safe=False) -@method_decorator(csrf_protect, name='dispatch') -@method_decorator(ensure_csrf_cookie, name='dispatch') -class ProfileView(APIView): - def get(self, request, _id): - profil=bdd.getObject(Profile, "id", _id) - profil_serializer=ProfileSerializer(profil) - return JsonResponse(profil_serializer.data, safe=False) - def post(self, request): profil_data=JSONParser().parse(request) print(f'{profil_data}') @@ -74,12 +69,20 @@ class ProfileView(APIView): if profil_serializer.is_valid(): profil_serializer.save() - + return JsonResponse(profil_serializer.data, safe=False) - + return JsonResponse(profil_serializer.errors, safe=False, status=status.HTTP_400_BAD_REQUEST) - - def put(self, request, _id): + +@method_decorator(csrf_protect, name='dispatch') +@method_decorator(ensure_csrf_cookie, name='dispatch') +class ProfileSimpleView(APIView): + def get(self, request, id): + profil=bdd.getObject(Profile, "id", id) + profil_serializer=ProfileSerializer(profil) + return JsonResponse(profil_serializer.data, safe=False) + + def put(self, request, id): data=JSONParser().parse(request) profil = Profile.objects.get(id=_id) profil_serializer = ProfilUpdateSerializer(profil, data=data) @@ -89,8 +92,8 @@ class ProfileView(APIView): return JsonResponse(profil_serializer.errors, safe=False, status=status.HTTP_400_BAD_REQUEST) - def delete(self, request, _id): - return bdd.delete_object(Profile, _id) + def delete(self, request, id): + return bdd.delete_object(Profile, id) def infoSession(request): profilCache = cache.get('session_cache') @@ -102,14 +105,6 @@ def infoSession(request): @method_decorator(csrf_protect, name='dispatch') @method_decorator(ensure_csrf_cookie, name='dispatch') class LoginView(APIView): - - def get(self, request): - return JsonResponse({ - 'errorFields':'', - 'errorMessage':'', - 'profil':0, - }, safe=False) - def post(self, request): data=JSONParser().parse(request) validatorAuthentication = validator.ValidatorAuthentication(data=data) @@ -153,22 +148,15 @@ class LoginView(APIView): @method_decorator(ensure_csrf_cookie, name='dispatch') class SubscribeView(APIView): - def get(self, request): - return JsonResponse({ - 'message':'', - 'errorFields':'', - 'errorMessage':'' - }, safe=False) - def post(self, request): retourErreur = error.returnMessage[error.BAD_URL] retour = '' newProfilConnection=JSONParser().parse(request) - + validatorSubscription = validator.ValidatorSubscription(data=newProfilConnection) validationOk, errorFields = validatorSubscription.validate() - + if validationOk: # On vérifie que l'email existe : si ce n'est pas le cas, on retourne une erreur @@ -200,13 +188,7 @@ class SubscribeView(APIView): @method_decorator(csrf_protect, name='dispatch') @method_decorator(ensure_csrf_cookie, name='dispatch') class NewPasswordView(APIView): - def get(self, request): - return JsonResponse({ - 'message':'', - 'errorFields':'', - 'errorMessage':'' - }, safe=False) - + def post(self, request): retourErreur = error.returnMessage[error.BAD_URL] retour = '' @@ -234,22 +216,15 @@ class NewPasswordView(APIView): @method_decorator(csrf_protect, name='dispatch') @method_decorator(ensure_csrf_cookie, name='dispatch') class ResetPasswordView(APIView): - def get(self, request, _uuid): - return JsonResponse({ - 'message':'', - 'errorFields':'', - 'errorMessage':'' - }, safe=False) - - def post(self, request, _uuid): + def post(self, request, code): retourErreur = error.returnMessage[error.BAD_URL] retour = '' newProfilConnection=JSONParser().parse(request) validatorResetPassword = validator.ValidatorResetPassword(data=newProfilConnection) validationOk, errorFields = validatorResetPassword.validate() - - profil = bdd.getObject(Profile, "code", _uuid) + + profil = bdd.getObject(Profile, "code", code) if profil: if datetime.strptime(util.convertToStr(util._now(), '%d-%m-%Y %H:%M'), '%d-%m-%Y %H:%M') > datetime.strptime(profil.datePeremption, '%d-%m-%Y %H:%M'): diff --git a/Back-End/GestionMessagerie/urls.py b/Back-End/GestionMessagerie/urls.py index 9636936..cdbd50b 100644 --- a/Back-End/GestionMessagerie/urls.py +++ b/Back-End/GestionMessagerie/urls.py @@ -1,9 +1,9 @@ from django.urls import path, re_path -from GestionMessagerie.views import MessagerieView, MessageView +from GestionMessagerie.views import MessagerieView, MessageView, MessageSimpleView urlpatterns = [ - re_path(r'^messagerie/([0-9]+)$', MessagerieView.as_view(), name="messagerie"), - re_path(r'^message$', MessageView.as_view(), name="message"), - re_path(r'^message/([0-9]+)$', MessageView.as_view(), name="message"), + re_path(r'^messagerie/(?P[0-9]+)$', MessagerieView.as_view(), name="messagerie"), + re_path(r'^messages$', MessageView.as_view(), name="messages"), + re_path(r'^messages/(?P[0-9]+)$', MessageSimpleView.as_view(), name="messages"), ] \ No newline at end of file diff --git a/Back-End/GestionMessagerie/views.py b/Back-End/GestionMessagerie/views.py index 40c7297..9b51042 100644 --- a/Back-End/GestionMessagerie/views.py +++ b/Back-End/GestionMessagerie/views.py @@ -9,24 +9,26 @@ from GestionMessagerie.serializers import MessageSerializer from N3wtSchool import bdd class MessagerieView(APIView): - def get(self, request, _idProfile): - messagesList = bdd.getObjects(_objectName=Messagerie, _columnName='destinataire__id', _value=_idProfile) + def get(self, request, profile_id): + messagesList = bdd.getObjects(_objectName=Messagerie, _columnName='destinataire__id', _value=profile_id) messages_serializer = MessageSerializer(messagesList, many=True) - return JsonResponse(messages_serializer.data, safe=False) + return JsonResponse(messages_serializer.data, safe=False) class MessageView(APIView): - def get(self, request, _id): - message=bdd.getObject(Messagerie, "id", _id) - message_serializer=MessageSerializer(message) - return JsonResponse(message_serializer.data, safe=False) - def post(self, request): message_data=JSONParser().parse(request) message_serializer = MessageSerializer(data=message_data) if message_serializer.is_valid(): - message_serializer.save() - + message_serializer.save() + return JsonResponse('Nouveau Message ajouté', safe=False) - return JsonResponse(message_serializer.errors, safe=False) \ No newline at end of file + return JsonResponse(message_serializer.errors, safe=False) + +class MessageSimpleView(APIView): + def get(self, request, id): + message=bdd.getObject(Messagerie, "id", id) + message_serializer=MessageSerializer(message) + return JsonResponse(message_serializer.data, safe=False) + diff --git a/Back-End/GestionNotification/urls.py b/Back-End/GestionNotification/urls.py index 7619357..e966cc6 100644 --- a/Back-End/GestionNotification/urls.py +++ b/Back-End/GestionNotification/urls.py @@ -3,5 +3,5 @@ from django.urls import path, re_path from GestionNotification.views import NotificationView urlpatterns = [ - re_path(r'^notification$', NotificationView.as_view(), name="notification"), + re_path(r'^notifications$', NotificationView.as_view(), name="notifications"), ] \ No newline at end of file diff --git a/Front-End/src/app/lib/authAction.js b/Front-End/src/app/lib/authAction.js index f84b6af..6b01afb 100644 --- a/Front-End/src/app/lib/authAction.js +++ b/Front-End/src/app/lib/authAction.js @@ -2,7 +2,7 @@ import { BE_AUTH_LOGIN_URL, BE_AUTH_REGISTER_URL, - BE_AUTH_PROFILE_URL, + BE_AUTH_PROFILES_URL, BE_AUTH_RESET_PASSWORD_URL, BE_AUTH_NEW_PASSWORD_URL, FE_USERS_LOGIN_URL , @@ -67,7 +67,7 @@ export const disconnect = () => { export const createProfile = (data,csrfToken) => { const request = new Request( - `${BE_AUTH_PROFILE_URL}`, + `${BE_AUTH_PROFILES_URL}`, { method:'POST', headers: { @@ -83,7 +83,7 @@ const request = new Request( export const updateProfile = (id, data, csrfToken) => { const request = new Request( - `${BE_AUTH_PROFILE_URL}/${id}`, + `${BE_AUTH_PROFILES_URL}/${id}`, { method:'PUT', headers: { diff --git a/Front-End/src/utils/Url.js b/Front-End/src/utils/Url.js index d12dff4..8a6a04b 100644 --- a/Front-End/src/utils/Url.js +++ b/Front-End/src/utils/Url.js @@ -11,7 +11,7 @@ export const BE_AUTH_REGISTER_URL = `${BASE_URL}/Auth/subscribe` export const BE_AUTH_RESET_PASSWORD_URL = `${BASE_URL}/Auth/resetPassword` export const BE_AUTH_LOGIN_URL = `${BASE_URL}/Auth/login` export const BE_AUTH_LOGOUT_URL = `${BASE_URL}/Auth/logout` -export const BE_AUTH_PROFILE_URL = `${BASE_URL}/Auth/profile` +export const BE_AUTH_PROFILES_URL = `${BASE_URL}/Auth/profiles` export const BE_AUTH_CSRF_URL = `${BASE_URL}/Auth/csrf` // GESTION INSCRIPTION @@ -43,7 +43,8 @@ export const BE_SCHOOL_PAYMENT_MODE_URL = `${BASE_URL}/School/paymentMode`; export const BE_SCHOOL_PAYMENT_MODES_URL = `${BASE_URL}/School/paymentModes`; // GESTION MESSAGERIE -export const BE_GESTIONMESSAGERIE_MESSAGES_URL = `${BASE_URL}/GestionMessagerie/messagerie` +export const BE_GESTIONMESSAGERIE_MESSAGES_URL = `${BASE_URL}/GestionMessagerie/messages` +export const BE_GESTIONMESSAGERIE_MESSAGERIE_URL = `${BASE_URL}/GestionMessagerie/messagerie` // URL FRONT-END export const FE_HOME_URL = `/`