feat: Gestion des rattachements de Guardian à des RF déjà existants

This commit is contained in:
N3WT DE COMPET
2025-03-12 14:04:35 +01:00
parent 753a8d647e
commit 7d1b9c5657
10 changed files with 241 additions and 196 deletions

View File

@ -5,12 +5,17 @@ from Establishment.models import Establishment
class ProfileSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(required=False)
password = serializers.CharField(write_only=True)
roles = serializers.SerializerMethodField()
class Meta:
model = Profile
fields = ['id', 'password', 'email', 'code', 'datePeremption', 'username']
fields = ['id', 'password', 'email', 'code', 'datePeremption', 'username', 'roles']
extra_kwargs = {'password': {'write_only': True}}
def get_roles(self, obj):
roles = ProfileRole.objects.filter(profile=obj)
return [{'role_type': role.role_type, 'establishment': role.establishment.id, 'is_active': role.is_active} for role in roles]
def create(self, validated_data):
user = Profile(
username=validated_data['username'],
@ -37,70 +42,44 @@ class ProfileSerializer(serializers.ModelSerializer):
def to_representation(self, instance):
ret = super().to_representation(instance)
ret['password'] = '********'
ret['roles'] = self.get_roles(instance)
return ret
class ProfileRoleSerializer(serializers.ModelSerializer):
profile = ProfileSerializer()
establishment = serializers.PrimaryKeyRelatedField(queryset=Establishment.objects.all())
profile = serializers.PrimaryKeyRelatedField(queryset=Profile.objects.all(), required=False)
profile_data = ProfileSerializer(write_only=True, required=False)
class Meta:
model = ProfileRole
fields = ['role_type', 'establishment', 'is_active', 'profile']
fields = ['role_type', 'establishment', 'is_active', 'profile', 'profile_data']
def create(self, validated_data):
profile_data = validated_data.pop('profile')
profile_serializer = ProfileSerializer(data=profile_data)
profile_serializer.is_valid(raise_exception=True)
profile = profile_serializer.save()
profile_data = validated_data.pop('profile_data', None)
profile = validated_data.pop('profile', None)
if profile_data:
profile_serializer = ProfileSerializer(data=profile_data)
profile_serializer.is_valid(raise_exception=True)
profile = profile_serializer.save()
elif profile:
profile = Profile.objects.get(id=profile.id)
profile_role = ProfileRole.objects.create(profile=profile, **validated_data)
return profile_role
def update(self, instance, validated_data):
profile_data = validated_data.pop('profile')
profile_serializer = ProfileSerializer(instance.profile, data=profile_data)
profile_serializer.is_valid(raise_exception=True)
profile = profile_serializer.save()
profile_data = validated_data.pop('profile_data', None)
profile = validated_data.pop('profile', None)
if profile_data:
profile_serializer = ProfileSerializer(instance.profile, data=profile_data)
profile_serializer.is_valid(raise_exception=True)
profile = profile_serializer.save()
elif profile:
profile = Profile.objects.get(id=profile.id)
instance.role_type = validated_data.get('role_type', instance.role_type)
instance.establishment_id = validated_data.get('establishment', instance.establishment.id)
instance.is_active = validated_data.get('is_active', instance.is_active)
instance.save()
return instance
class ProfilUpdateSerializer(serializers.ModelSerializer):
roles = ProfileRoleSerializer(many=True, required=False)
class Meta:
model = Profile
fields = ['id', 'password', 'email', 'code', 'datePeremption', 'username', 'roles']
extra_kwargs = {
'password': {'write_only': True, 'required': False}
}
def update(self, instance, validated_data):
roles_data = validated_data.pop('roles', [])
password = validated_data.pop('password', None)
instance = super().update(instance, validated_data)
if password:
instance.set_password(password)
instance.full_clean()
instance.save()
for role_data in roles_data:
ProfileRole.objects.update_or_create(
profile=instance,
establishment_id=role_data.get('establishment_id'),
defaults={
'role_type': role_data.get('role_type'),
'is_active': role_data.get('is_active', True)
}
)
return instance
def to_representation(self, instance):
ret = super().to_representation(instance)
ret['password'] = '********'
return ret
return instance

View File

@ -2,7 +2,7 @@ from django.urls import path, re_path
from . import views
import Auth.views
from Auth.views import ProfileSimpleView, ProfileView, SessionView, LoginView, RefreshJWTView, SubscribeView, NewPasswordView, ResetPasswordView
from Auth.views import ProfileRoleView, ProfileRoleSimpleView, ProfileSimpleView, ProfileView, SessionView, LoginView, RefreshJWTView, SubscribeView, NewPasswordView, ResetPasswordView
urlpatterns = [
re_path(r'^csrf$', Auth.views.csrf, name='csrf'),
@ -16,4 +16,7 @@ urlpatterns = [
re_path(r'^profiles$', ProfileView.as_view(), name="profile"),
re_path(r'^profiles/(?P<id>[0-9]+)$', ProfileSimpleView.as_view(), name="profile"),
re_path(r'^profileRoles$', ProfileRoleView.as_view(), name="profileRoles"),
re_path(r'^profileRoles/(?P<id>[0-9]+)$', ProfileRoleSimpleView.as_view(), name="profileRoles"),
]

View File

@ -22,7 +22,7 @@ from . import validator
from .models import Profile, ProfileRole
from rest_framework.decorators import action, api_view
from Auth.serializers import ProfileSerializer, ProfilUpdateSerializer
from Auth.serializers import ProfileSerializer, ProfileRoleSerializer
from Subscriptions.models import RegistrationForm
from Subscriptions.signals import clear_cache
import Subscriptions.mailManager as mailer
@ -130,7 +130,7 @@ class ProfileSimpleView(APIView):
@swagger_auto_schema(
operation_description="Mettre à jour un profil",
request_body=ProfilUpdateSerializer,
request_body=ProfileSerializer,
responses={
200: 'Mise à jour réussie',
400: 'Données invalides'
@ -139,7 +139,7 @@ class ProfileSimpleView(APIView):
def put(self, request, id):
data = JSONParser().parse(request)
profil = Profile.objects.get(id=id)
profil_serializer = ProfilUpdateSerializer(profil, data=data)
profil_serializer = ProfileSerializer(profil, data=data)
if profil_serializer.is_valid():
profil_serializer.save()
return JsonResponse(profil_serializer.data, safe=False)
@ -529,3 +529,68 @@ class ResetPasswordView(APIView):
retourErreur = ''
return JsonResponse({'message': retour, "errorMessage": retourErreur, "errorFields": errorFields}, safe=False)
class ProfileRoleView(APIView):
@swagger_auto_schema(
operation_description="Obtenir la liste des profile_roles",
responses={200: ProfileRoleSerializer(many=True)}
)
def get(self, request):
profiles_roles_List = bdd.getAllObjects(_objectName=ProfileRole)
profile_roles_serializer = ProfileRoleSerializer(profiles_roles_List, many=True)
return JsonResponse(profile_roles_serializer.data, safe=False)
@swagger_auto_schema(
operation_description="Créer un nouveau profile_role",
request_body=ProfileRoleSerializer,
responses={
200: ProfileRoleSerializer,
400: 'Données invalides'
}
)
def post(self, request):
profile_role_data = JSONParser().parse(request)
profile_role_serializer = ProfileRoleSerializer(data=profile_role_data)
if profile_role_serializer.is_valid():
profile_role = profile_role_serializer.save()
return JsonResponse(profile_role_serializer.data, safe=False)
return JsonResponse(profile_role_serializer.errors, safe=False, status=status.HTTP_400_BAD_REQUEST)
@method_decorator(csrf_protect, name='dispatch')
@method_decorator(ensure_csrf_cookie, name='dispatch')
class ProfileRoleSimpleView(APIView):
@swagger_auto_schema(
operation_description="Obtenir un profile_role par son ID",
responses={200: ProfileRoleSerializer}
)
def get(self, request, id):
profile_role = bdd.getObject(ProfileRole, "id", id)
profile_role_serializer = ProfileRoleSerializer(profile_role)
return JsonResponse(profile_role_serializer.data, safe=False)
@swagger_auto_schema(
operation_description="Mettre à jour un profile_role",
request_body=ProfileRoleSerializer,
responses={
200: 'Mise à jour réussie',
400: 'Données invalides'
}
)
def put(self, request, id):
data = JSONParser().parse(request)
profile_role = ProfileRole.objects.get(id=id)
profile_role_serializer = ProfileRoleSerializer(profile_role, data=data)
if profile_role_serializer.is_valid():
profile_role_serializer.save()
return JsonResponse(profile_role_serializer.data, safe=False)
return JsonResponse(profile_role_serializer.errors, safe=False, status=status.HTTP_400_BAD_REQUEST)
@swagger_auto_schema(
operation_description="Supprimer un profile_role",
responses={200: 'Suppression réussie'}
)
def delete(self, request, id):
return bdd.delete_object(ProfileRole, id)