mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
feat: Création nouveau style / pagination profils annuaires
This commit is contained in:
20
Back-End/Auth/pagination.py
Normal file
20
Back-End/Auth/pagination.py
Normal file
@ -0,0 +1,20 @@
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
|
||||
from N3wtSchool import settings
|
||||
|
||||
class CustomProfilesPagination(PageNumberPagination):
|
||||
page_size_query_param = 'page_size'
|
||||
max_page_size = settings.NB_MAX_PAGE
|
||||
page_size = settings.NB_RESULT_PROFILES_PER_PAGE
|
||||
|
||||
def get_paginated_response(self, data):
|
||||
return ({
|
||||
'links': {
|
||||
'next': self.get_next_link(),
|
||||
'previous': self.get_previous_link()
|
||||
},
|
||||
'count': self.page.paginator.count,
|
||||
'page_size': self.page_size,
|
||||
'max_page_size' : self.max_page_size,
|
||||
'profilesRoles': data }
|
||||
)
|
||||
@ -8,6 +8,7 @@ from django.middleware.csrf import get_token
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.parsers import JSONParser
|
||||
from rest_framework import status
|
||||
from Auth.pagination import CustomProfilesPagination
|
||||
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
from drf_yasg import openapi
|
||||
@ -20,13 +21,14 @@ import json
|
||||
from . import validator
|
||||
from .models import Profile, ProfileRole
|
||||
from rest_framework.decorators import action, api_view
|
||||
from django.db.models import Q
|
||||
|
||||
from Auth.serializers import ProfileSerializer, ProfileRoleSerializer
|
||||
from Subscriptions.models import RegistrationForm, Guardian
|
||||
import Subscriptions.mailManager as mailer
|
||||
import Subscriptions.util as util
|
||||
import logging
|
||||
from N3wtSchool import bdd, error
|
||||
from N3wtSchool import bdd, error, settings
|
||||
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
|
||||
@ -509,20 +511,55 @@ class ResetPasswordView(APIView):
|
||||
return JsonResponse({'message': retour, "errorMessage": retourErreur, "errorFields": errorFields}, safe=False)
|
||||
|
||||
class ProfileRoleView(APIView):
|
||||
pagination_class = CustomProfilesPagination
|
||||
@swagger_auto_schema(
|
||||
operation_description="Obtenir la liste des profile_roles",
|
||||
responses={200: ProfileRoleSerializer(many=True)}
|
||||
)
|
||||
def get(self, request):
|
||||
filter = request.GET.get('filter', '').strip()
|
||||
page_size = request.GET.get('page_size', None)
|
||||
establishment_id = request.GET.get('establishment_id', None)
|
||||
|
||||
# Gestion du page_size
|
||||
if page_size is not None:
|
||||
try:
|
||||
page_size = int(page_size)
|
||||
except ValueError:
|
||||
page_size = settings.NB_RESULT_PROFILES_PER_PAGE
|
||||
|
||||
if establishment_id is None:
|
||||
return JsonResponse({'error': 'establishment_id est requis'}, safe=False, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
profiles_roles_List = bdd.getAllObjects(_objectName=ProfileRole)
|
||||
if profiles_roles_List:
|
||||
profiles_roles_List = profiles_roles_List.filter(establishment=establishment_id).distinct().order_by('-updated_date')
|
||||
profile_roles_serializer = ProfileRoleSerializer(profiles_roles_List, many=True)
|
||||
return JsonResponse(profile_roles_serializer.data, safe=False)
|
||||
# Récupérer les ProfileRole en fonction du filtre
|
||||
profiles_roles_List = ProfileRole.objects.filter(establishment_id=establishment_id)
|
||||
|
||||
if filter == 'parents':
|
||||
profiles_roles_List = profiles_roles_List.filter(role_type=ProfileRole.RoleType.PROFIL_PARENT)
|
||||
elif filter == 'school':
|
||||
profiles_roles_List = profiles_roles_List.filter(
|
||||
Q(role_type=ProfileRole.RoleType.PROFIL_ECOLE) |
|
||||
Q(role_type=ProfileRole.RoleType.PROFIL_ADMIN)
|
||||
)
|
||||
else:
|
||||
return JsonResponse({'error': 'Filtre invalide'}, safe=False, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Trier les résultats par date de mise à jour
|
||||
profiles_roles_List = profiles_roles_List.distinct().order_by('-updated_date')
|
||||
|
||||
if not profiles_roles_List:
|
||||
return JsonResponse({'error': 'aucune donnée trouvée', 'count': 0}, safe=False)
|
||||
|
||||
# Pagination
|
||||
paginator = self.pagination_class()
|
||||
page = paginator.paginate_queryset(profiles_roles_List, request)
|
||||
|
||||
if page is not None:
|
||||
profile_roles_serializer = ProfileRoleSerializer(page, many=True)
|
||||
response_data = paginator.get_paginated_response(profile_roles_serializer.data)
|
||||
return JsonResponse(response_data, safe=False)
|
||||
|
||||
return JsonResponse({'error': 'aucune donnée trouvée', 'count': 0}, safe=False)
|
||||
|
||||
@swagger_auto_schema(
|
||||
operation_description="Créer un nouveau profile_role",
|
||||
|
||||
@ -282,12 +282,13 @@ DATE_FORMAT = '%d-%m-%Y %H:%M'
|
||||
|
||||
EXPIRATION_SESSION_NB_SEC = 10
|
||||
|
||||
NB_RESULT_PER_PAGE = 8
|
||||
NB_RESULT_SUBSCRIPTIONS_PER_PAGE = 8
|
||||
NB_RESULT_PROFILES_PER_PAGE = 15
|
||||
NB_MAX_PAGE = 100
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_PAGINATION_CLASS': 'Subscriptions.pagination.CustomPagination',
|
||||
'PAGE_SIZE': NB_RESULT_PER_PAGE,
|
||||
'DEFAULT_PAGINATION_CLASS': 'Subscriptions.pagination.CustomSubscriptionPagination',
|
||||
'PAGE_SIZE': NB_RESULT_SUBSCRIPTIONS_PER_PAGE,
|
||||
'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||
'rest_framework_simplejwt.authentication.JWTAuthentication',
|
||||
),
|
||||
|
||||
@ -32,7 +32,7 @@ class Teacher(models.Model):
|
||||
last_name = models.CharField(max_length=100)
|
||||
first_name = models.CharField(max_length=100)
|
||||
specialities = models.ManyToManyField(Speciality, blank=True)
|
||||
profile_role = models.OneToOneField(ProfileRole, on_delete=models.CASCADE, related_name='teacher_profile', blank=True)
|
||||
profile_role = models.OneToOneField(ProfileRole, on_delete=models.CASCADE, related_name='teacher_profile', null=True, blank=True)
|
||||
updated_date = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
|
||||
@ -31,7 +31,7 @@ class Guardian(models.Model):
|
||||
address = models.CharField(max_length=200, default="", blank=True)
|
||||
phone = models.CharField(max_length=200, default="", blank=True)
|
||||
profession = models.CharField(max_length=200, default="", blank=True)
|
||||
profile_role = models.OneToOneField(ProfileRole, on_delete=models.CASCADE, related_name='guardian_profile', blank=True)
|
||||
profile_role = models.OneToOneField(ProfileRole, on_delete=models.CASCADE, related_name='guardian_profile', null=True, blank=True)
|
||||
|
||||
@property
|
||||
def email(self):
|
||||
|
||||
@ -2,10 +2,10 @@ from rest_framework.pagination import PageNumberPagination
|
||||
|
||||
from N3wtSchool import settings
|
||||
|
||||
class CustomPagination(PageNumberPagination):
|
||||
class CustomSubscriptionPagination(PageNumberPagination):
|
||||
page_size_query_param = 'page_size'
|
||||
max_page_size = settings.NB_MAX_PAGE
|
||||
page_size = settings.NB_RESULT_PER_PAGE
|
||||
page_size = settings.NB_RESULT_SUBSCRIPTIONS_PER_PAGE
|
||||
|
||||
def get_paginated_response(self, data):
|
||||
return ({
|
||||
|
||||
@ -137,9 +137,19 @@ class StudentSerializer(serializers.ModelSerializer):
|
||||
def create_or_update_guardians(self, guardians_data):
|
||||
guardians_ids = []
|
||||
for guardian_data in guardians_data:
|
||||
guardian_id = guardian_data.get('id', None)
|
||||
profile_role_data = guardian_data.pop('profile_role_data', None)
|
||||
profile_role = guardian_data.pop('profile_role', None)
|
||||
|
||||
if guardian_id:
|
||||
# Si un ID est fourni, récupérer ou mettre à jour le Guardian existant
|
||||
guardian_instance, created = Guardian.objects.update_or_create(
|
||||
id=guardian_id,
|
||||
defaults=guardian_data
|
||||
)
|
||||
guardians_ids.append(guardian_instance.id)
|
||||
continue
|
||||
|
||||
if profile_role_data:
|
||||
# Vérifiez si 'profile_data' est fourni pour créer un nouveau profil
|
||||
profile_data = profile_role_data.pop('profile_data', None)
|
||||
@ -410,4 +420,4 @@ class NotificationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Notification
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ import Subscriptions.mailManager as mailer
|
||||
import Subscriptions.util as util
|
||||
|
||||
from Subscriptions.serializers import RegistrationFormSerializer, RegistrationSchoolFileTemplateSerializer, RegistrationParentFileTemplateSerializer
|
||||
from Subscriptions.pagination import CustomPagination
|
||||
from Subscriptions.pagination import CustomSubscriptionPagination
|
||||
from Subscriptions.models import Student, Guardian, RegistrationForm, RegistrationSchoolFileTemplate, RegistrationFileGroup, RegistrationParentFileTemplate
|
||||
from Subscriptions.automate import updateStateMachine
|
||||
|
||||
@ -29,7 +29,7 @@ class RegisterFormView(APIView):
|
||||
"""
|
||||
Gère la liste des dossiers d’inscription, lecture et création.
|
||||
"""
|
||||
pagination_class = CustomPagination
|
||||
pagination_class = CustomSubscriptionPagination
|
||||
|
||||
@swagger_auto_schema(
|
||||
manual_parameters=[
|
||||
@ -82,7 +82,7 @@ class RegisterFormView(APIView):
|
||||
try:
|
||||
page_size = int(page_size)
|
||||
except ValueError:
|
||||
page_size = settings.NB_RESULT_PER_PAGE
|
||||
page_size = settings.NB_RESULT_SUBSCRIPTIONS_PER_PAGE
|
||||
|
||||
# Récupérer les années scolaires
|
||||
current_year = util.getCurrentSchoolYear()
|
||||
@ -179,7 +179,7 @@ class RegisterFormWithIdView(APIView):
|
||||
"""
|
||||
Gère la lecture, création, modification et suppression d’un dossier d’inscription.
|
||||
"""
|
||||
pagination_class = CustomPagination
|
||||
pagination_class = CustomSubscriptionPagination
|
||||
|
||||
@swagger_auto_schema(
|
||||
responses={200: RegistrationFormSerializer()},
|
||||
|
||||
Reference in New Issue
Block a user