Commit b0776e05 authored by Hamza Dely's avatar Hamza Dely

[consos/serializers] Implémente TransactionSerializer.create()

parent 1b3f4697
......@@ -2,6 +2,8 @@
Sérialiseurs de l'application « Consos »
"""
import itertools
from rest_framework import serializers
from note_kfet.serializers import mixins
......@@ -47,10 +49,78 @@ class TransactionSerializer(serializers.ModelSerializer):
"""
Sérialiseur pour les transactions
"""
emetteurs = serializers.PrimaryKeyRelatedField(
many=True, allow_null=True, write_only=True,
queryset=Note.objects.filter(soft_lock=False, hard_lock=False),
)
destinataires = serializers.PrimaryKeyRelatedField(
many=True, allow_null=True, write_only=True,
queryset=Note.objects.filter(soft_lock=False, hard_lock=False),
)
bouton = serializers.PrimaryKeyRelatedField(
write_only=True, queryset=Bouton.objects.filter(actif=True),
)
class Meta:
model = Transaction
fields = [
'id', 'date', 'action', 'moyen', 'quantite', 'montant',
'categorie', 'description', 'valide', 'emetteur', 'destinataire',
'id', 'date', 'action', 'moyen', 'quantite',
'montant', 'categorie', 'description', 'valide', 'emetteur',
'destinataire', 'emetteurs', 'destinataires', 'bouton',
]
read_only_fields = ['id']
read_only_fields = ['date', 'emetteur', 'destinataire']
def create(self, validated_data):
params = {
'action' : validated_data.get('action'),
'quantite' : validated_data.get('quantite'),
'description' : validated_data.get('description'),
'valide' : validated_data.get('valide'),
}
emetteurs = []
destinataires = []
if params['action'] == Transaction.VENTE:
bouton = Bouton.objects.get(id=validated_data.get('bouton'))
params['moyen'] = Transaction.NOTE
params['montant'] = bouton.montant
params['categorie'] = bouton.categorie
params['description'] = bouton.etiquette
emetteurs = validated_data.get('emetteurs')
destinataires = [bouton.credite]
elif params['action'] == Transaction.CREDIT:
params['moyen'] = validated_data.get('moyen')
params['montant'] = validated_data.get('montant')
params['categorie'] = "Opération de gestion"
params['description'] = "Crédit Note"
emetteurs = [Note.ASSOCIATION]
destinataires = validated_data.get('destinataires')
elif params['action'] == Transaction.RETRAIT:
params['moyen'] = validated_data.get('moyen')
params['montant'] = validated_data.get('montant')
params['categorie'] = "Opération de gestion"
params['description'] = "Retrait Note"
emetteurs = validated_data.get('emetteurs')
destinataires = [Note.ASSOCIATION]
elif params['action'] == Transaction.TRANSFERT:
params['moyen'] = validated_data.get('moyen')
params['montant'] = validated_data.get('montant')
params['categorie'] = "Opération de gestion"
params['description'] = "Retrait Note"
emetteurs = validated_data.get('emetteurs')
destinataires = validated_data.get('destinataires')
else:
raise serializers.ValidationError(
{'action' : "Action %r non prise en charge" % params['action']},
)
created = []
for emetteur, destinataire in itertools.product(emetteurs, destinataires):
transaction = Transaction(emetteur=emetteur, destinataire=destinataire, **params)
transaction.full_clean(exclude=['id'])
transaction.save()
created.append(transaction)
return created
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