Commit 38a80799 authored by Hamza Dely's avatar Hamza Dely

[comptes] Amélioration du code de l'application

parent cf823f2a
......@@ -118,13 +118,25 @@ class Alias(models.Model):
super(cls, alias_obj).save()
return alias_obj
@property
def is_pseudo(self):
"""
Indique si l'alias courant est le pseudo d'un adhérent.
"""
if self.proprietaire is None or not self.proprietaire.pseudo == self.alias:
return False
else:
return True
@transaction.atomic
def save(self, *args, **kwargs):
"""
Enregistre un alias.
"""
super().save()
super().save(*args, **kwargs)
Historique.objects.create(alias=self, suivant=self.proprietaire)
@transaction.atomic
def delete(self, *args, **kwargs):
"""
Supprime un alias.
......@@ -134,7 +146,7 @@ class Alias(models.Model):
"""
ancien_proprio = self.proprietaire
self.proprietaire = None
super().save()
super().save(*args, **kwargs)
Historique.objects.create(alias=self, precedent=ancien_proprio)
class Historique(models.Model):
......
......@@ -8,6 +8,7 @@ from rest_framework import serializers
from note_kfet.serializers import mixins
from note_kfet.serializers import Base64ImageField
from comptes.models import Section, Alias, Historique, Droit, Accreditation, Adhesion, Adherent
class SectionSerializer(serializers.ModelSerializer):
......@@ -50,13 +51,14 @@ class AdhesionSerializer(serializers.ModelSerializer):
### Sérialiseurs pour le modèle Alias
class AliasSerializer(serializers.ModelSerializer):
class AliasSerializer(mixins.DynamicFieldsMixin, serializers.ModelSerializer):
"""
Sérialiseur pour le modèle Alias
"""
class Meta:
model = Alias
fields = ['alias', 'proprietaire']
default_empty = False
def validate_alias(self, value):
"""
......@@ -68,6 +70,16 @@ class AliasSerializer(serializers.ModelSerializer):
raise serializers.ValidationError("Cet alias est déjà pris")
return normalized_alias
def update(self, instance, validated_data):
"""
Effectue le changement de propriétaire d'un alias
"""
if instance.is_pseudo:
raise serializers.ValidationError(
"Cet alias ne peut être abandonné, il s'agit d'un pseudo"
)
return super().update(instance, validated_data)
### Sérialiseurs pour le modèle Historique
class HistoriqueSerializer(serializers.ModelSerializer):
......@@ -85,7 +97,7 @@ class AdherentSerializer(mixins.DynamicFieldsMixin, serializers.ModelSerializer)
Sérialiseur pour le modèle Adhérent
"""
accreditations = AccreditationSerializer(many=True, read_only=True, fields=['droit', 'meta'])
aliases = AliasSerializer(many=True, read_only=True)
aliases = serializers.SlugRelatedField(many=True, read_only=True, slug_field='alias')
adhesions = AdhesionSerializer(many=True, read_only=True)
last_adhesion = AdhesionSerializer(read_only=True)
section = serializers.CharField(max_length=10)
......
This diff is collapsed.
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