Commit d05a0da8 authored by Hamza Dely's avatar Hamza Dely
Browse files

[comptes/views] Mise à niveau du sérialiseur pour les accréditations

parent 57a6f724
......@@ -12,6 +12,7 @@ from django.utils import timezone
from rest_framework import serializers
from note_kfet.mixins import DynamicFieldsMixin
from note_kfet.droits import Acl
from note_kfet.environnement import ACCREDITATIONS_DEFAUT
from comptes.models import (
......@@ -27,14 +28,24 @@ class SectionSerializer(serializers.ModelSerializer):
fields = ['sigle', 'nom']
read_only_fields = ['sigle', 'nom']
class AccreditationSerializer(serializers.ModelSerializer):
class DroitSerializer(serializers.ModelSerializer):
"""
Sérialiseur pour le modèle Droit
"""
class Meta:
model = Droit
fields = ['id', 'description', 'niveau']
read_only_fields = ['id', 'description', 'niveau']
class AccreditationSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
"""
Sérialiseur pour le modèle Accréditation
"""
droit = DroitSerializer(read_only=True)
class Meta:
model = Accreditation
fields = ['id', 'niveau', 'description']
read_only_fields = ['id', 'niveau', 'description']
fields = ['id', 'adherent', 'droit', 'meta']
read_only_fields = ['id', 'adherent', 'droit']
class AdhesionSerializer(serializers.ModelSerializer):
"""
......@@ -172,23 +183,14 @@ class AdherentListSerializer(serializers.ModelSerializer):
fields = ['id', 'pseudo', 'nom', 'prenom', 'type', 'aliases', 'adhesion']
read_only_fields = fields
class AdherentSerializer(serializers.ModelSerializer):
class AdherentSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
"""
Sérialiseur pour le modèle Adhérent
"""
accreditations = AccreditationSerializer(many=True, read_only=True)
accreditations = AccreditationSerializer(many=True, read_only=True, fields=['droit', 'meta'])
aliases = AliasSerializer(many=True, read_only=True)
adhesions = AdhesionSerializer(many=True, read_only=True)
class Meta:
model = Adherent
fields = Adherent.CHAMPS_VISIBLES[Acl.TOTAL]
read_only_fields = ['id']
def __init__(self, *args, **kwargs):
"""
Modifie dynamiquement les champs utilisés dans le sérialiseur
"""
fields = set(kwargs.pop('fields', []))
super().__init__(*args, **kwargs)
for field in set(self.fields) - fields:
self.fields.pop(field)
......@@ -79,3 +79,14 @@ class NoteMixin(object):
})
return context
class DynamicFieldsMixin(object):
"""
Un mixin permettant de modifier de manière dynamique les champs
utilisés dans un sérialiseur.
"""
def __init__(self, *args, **kwargs):
fields = set(kwargs.pop('fields', []))
super().__init__(*args, **kwargs)
for field in set(self.fields) - fields:
self.fields.pop(field)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment