2024-02-07 01:26:49 +00:00
|
|
|
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
2020-02-06 23:12:00 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-08-06 10:50:24 +00:00
|
|
|
|
2020-09-01 13:54:56 +00:00
|
|
|
from django.conf import settings
|
2020-08-31 18:15:48 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-02-07 16:02:07 +00:00
|
|
|
from rest_framework import serializers
|
2020-08-31 18:15:48 +00:00
|
|
|
from rest_framework.exceptions import ValidationError
|
2020-02-07 19:47:49 +00:00
|
|
|
from rest_polymorphic.serializers import PolymorphicSerializer
|
2020-08-05 18:40:30 +00:00
|
|
|
from member.api.serializers import MembershipSerializer
|
|
|
|
from member.models import Membership
|
2021-06-15 12:40:32 +00:00
|
|
|
from note_kfet.middlewares import get_current_request
|
2020-04-09 22:02:22 +00:00
|
|
|
from permission.backends import PermissionBackend
|
2020-08-05 14:23:32 +00:00
|
|
|
from rest_framework.utils import model_meta
|
2023-09-18 15:12:31 +00:00
|
|
|
from rest_framework.validators import UniqueTogetherValidator
|
2020-02-07 16:02:07 +00:00
|
|
|
|
2021-09-05 19:55:22 +00:00
|
|
|
from ..models.notes import Note, NoteClub, NoteSpecial, NoteUser, Alias, Trust
|
2020-03-11 10:15:03 +00:00
|
|
|
from ..models.transactions import TransactionTemplate, Transaction, MembershipTransaction, TemplateCategory, \
|
2020-03-19 19:37:48 +00:00
|
|
|
RecurrentTransaction, SpecialTransaction
|
2020-02-18 20:14:29 +00:00
|
|
|
|
2020-02-06 23:12:00 +00:00
|
|
|
|
2020-02-07 19:47:49 +00:00
|
|
|
class NoteSerializer(serializers.ModelSerializer):
|
2020-02-06 23:29:04 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for Notes.
|
|
|
|
The djangorestframework plugin will analyse the model `Note` and parse all fields in the API.
|
|
|
|
"""
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-02-06 23:12:00 +00:00
|
|
|
class Meta:
|
|
|
|
model = Note
|
2020-02-07 19:47:49 +00:00
|
|
|
fields = '__all__'
|
2020-08-31 18:15:48 +00:00
|
|
|
read_only_fields = ('balance', 'last_negative', 'created_at', ) # Note balances are read-only protected
|
2020-02-06 23:12:00 +00:00
|
|
|
|
|
|
|
|
2020-02-07 19:47:49 +00:00
|
|
|
class NoteClubSerializer(serializers.ModelSerializer):
|
2020-02-06 23:29:04 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for Club's notes.
|
|
|
|
The djangorestframework plugin will analyse the model `NoteClub` and parse all fields in the API.
|
|
|
|
"""
|
2020-03-12 10:12:21 +00:00
|
|
|
name = serializers.SerializerMethodField()
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-02-06 23:12:00 +00:00
|
|
|
class Meta:
|
|
|
|
model = NoteClub
|
2020-02-07 19:47:49 +00:00
|
|
|
fields = '__all__'
|
2020-08-31 18:15:48 +00:00
|
|
|
read_only_fields = ('note', 'club', 'balance', 'last_negative', 'created_at', )
|
2020-02-06 23:12:00 +00:00
|
|
|
|
2020-03-12 10:12:21 +00:00
|
|
|
def get_name(self, obj):
|
|
|
|
return str(obj)
|
|
|
|
|
2020-02-06 23:12:00 +00:00
|
|
|
|
2020-02-07 19:47:49 +00:00
|
|
|
class NoteSpecialSerializer(serializers.ModelSerializer):
|
2020-02-06 23:29:04 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for special notes.
|
|
|
|
The djangorestframework plugin will analyse the model `NoteSpecial` and parse all fields in the API.
|
|
|
|
"""
|
2020-03-12 10:12:21 +00:00
|
|
|
name = serializers.SerializerMethodField()
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-02-06 23:12:00 +00:00
|
|
|
class Meta:
|
|
|
|
model = NoteSpecial
|
2020-02-07 19:47:49 +00:00
|
|
|
fields = '__all__'
|
2020-08-31 18:15:48 +00:00
|
|
|
read_only_fields = ('note', 'balance', 'last_negative', 'created_at', )
|
2020-02-06 23:12:00 +00:00
|
|
|
|
2020-03-12 10:12:21 +00:00
|
|
|
def get_name(self, obj):
|
|
|
|
return str(obj)
|
|
|
|
|
2020-02-06 23:12:00 +00:00
|
|
|
|
2020-02-07 19:47:49 +00:00
|
|
|
class NoteUserSerializer(serializers.ModelSerializer):
|
2020-02-06 23:29:04 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for User's notes.
|
|
|
|
The djangorestframework plugin will analyse the model `NoteUser` and parse all fields in the API.
|
|
|
|
"""
|
2020-03-12 10:12:21 +00:00
|
|
|
name = serializers.SerializerMethodField()
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-02-06 23:12:00 +00:00
|
|
|
class Meta:
|
|
|
|
model = NoteUser
|
2020-02-07 19:47:49 +00:00
|
|
|
fields = '__all__'
|
2020-08-31 18:15:48 +00:00
|
|
|
read_only_fields = ('note', 'user', 'balance', 'last_negative', 'created_at', )
|
2020-02-06 23:12:00 +00:00
|
|
|
|
2020-03-12 10:12:21 +00:00
|
|
|
def get_name(self, obj):
|
|
|
|
return str(obj)
|
|
|
|
|
2021-09-05 19:55:22 +00:00
|
|
|
|
|
|
|
class TrustSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
REST API Serializer for Trusts.
|
|
|
|
The djangorestframework plugin will analyse the model `Trust` and parse all fields in the API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Trust
|
|
|
|
fields = '__all__'
|
2023-09-18 15:12:31 +00:00
|
|
|
validators = [UniqueTogetherValidator(
|
|
|
|
queryset=Trust.objects.all(), fields=('trusting', 'trusted'),
|
2023-09-18 15:29:52 +00:00
|
|
|
message=_("This friendship already exists"))]
|
2021-09-05 19:55:22 +00:00
|
|
|
|
2020-02-08 14:08:55 +00:00
|
|
|
|
|
|
|
class AliasSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
REST API Serializer for Aliases.
|
|
|
|
The djangorestframework plugin will analyse the model `Alias` and parse all fields in the API.
|
|
|
|
"""
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-02-08 14:08:55 +00:00
|
|
|
class Meta:
|
|
|
|
model = Alias
|
|
|
|
fields = '__all__'
|
2020-03-26 16:45:24 +00:00
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
instance = Alias(**attrs)
|
|
|
|
instance.clean()
|
|
|
|
return attrs
|
2020-02-08 14:08:55 +00:00
|
|
|
|
|
|
|
|
2020-02-07 19:47:49 +00:00
|
|
|
class NotePolymorphicSerializer(PolymorphicSerializer):
|
|
|
|
model_serializer_mapping = {
|
|
|
|
Note: NoteSerializer,
|
|
|
|
NoteUser: NoteUserSerializer,
|
|
|
|
NoteClub: NoteClubSerializer,
|
2020-03-30 23:03:30 +00:00
|
|
|
NoteSpecial: NoteSpecialSerializer,
|
2020-02-07 19:47:49 +00:00
|
|
|
}
|
2020-02-06 23:12:00 +00:00
|
|
|
|
2020-03-18 13:42:35 +00:00
|
|
|
class Meta:
|
|
|
|
model = Note
|
|
|
|
|
2020-04-09 22:02:22 +00:00
|
|
|
|
2020-03-28 16:42:29 +00:00
|
|
|
class ConsumerSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
REST API Nested Serializer for Consumers.
|
2020-04-09 22:02:22 +00:00
|
|
|
return Alias, and the note Associated to it in
|
2020-03-28 16:42:29 +00:00
|
|
|
"""
|
2020-04-09 22:02:22 +00:00
|
|
|
note = serializers.SerializerMethodField()
|
|
|
|
|
2020-04-10 00:56:56 +00:00
|
|
|
email_confirmed = serializers.SerializerMethodField()
|
|
|
|
|
2020-08-05 18:40:30 +00:00
|
|
|
membership = serializers.SerializerMethodField()
|
|
|
|
|
2020-03-28 16:42:29 +00:00
|
|
|
class Meta:
|
|
|
|
model = Alias
|
|
|
|
fields = '__all__'
|
|
|
|
|
2020-04-09 22:02:22 +00:00
|
|
|
def get_note(self, obj):
|
2020-04-09 22:09:10 +00:00
|
|
|
"""
|
|
|
|
Display information about the associated note
|
|
|
|
"""
|
|
|
|
# If the user has no right to see the note, then we only display the note identifier
|
2020-09-01 13:54:56 +00:00
|
|
|
return NotePolymorphicSerializer().to_representation(obj.note)\
|
2021-06-15 12:40:32 +00:00
|
|
|
if PermissionBackend.check_perm(get_current_request(), "note.view_note", obj.note)\
|
2020-09-06 07:10:57 +00:00
|
|
|
else dict(
|
|
|
|
id=obj.note.id,
|
|
|
|
name=str(obj.note),
|
|
|
|
is_active=obj.note.is_active,
|
|
|
|
display_image=obj.note.display_image.url,
|
|
|
|
)
|
2020-04-09 22:02:22 +00:00
|
|
|
|
2020-04-10 00:56:56 +00:00
|
|
|
def get_email_confirmed(self, obj):
|
|
|
|
if isinstance(obj.note, NoteUser):
|
|
|
|
return obj.note.user.profile.email_confirmed
|
|
|
|
return True
|
|
|
|
|
2020-08-05 18:40:30 +00:00
|
|
|
def get_membership(self, obj):
|
|
|
|
if isinstance(obj.note, NoteUser):
|
|
|
|
memberships = Membership.objects.filter(
|
2021-06-15 12:40:32 +00:00
|
|
|
PermissionBackend.filter_queryset(get_current_request(), Membership, "view")).filter(
|
2020-08-05 18:40:30 +00:00
|
|
|
user=obj.note.user,
|
|
|
|
club=2, # Kfet
|
|
|
|
).order_by("-date_start")
|
|
|
|
if memberships.exists():
|
|
|
|
return MembershipSerializer().to_representation(memberships.first())
|
|
|
|
return None
|
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-03-11 00:03:15 +00:00
|
|
|
class TemplateCategorySerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
REST API Serializer for Transaction templates.
|
|
|
|
The djangorestframework plugin will analyse the model `TemplateCategory` and parse all fields in the API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = TemplateCategory
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
2020-02-07 19:47:49 +00:00
|
|
|
class TransactionTemplateSerializer(serializers.ModelSerializer):
|
2020-02-06 23:29:04 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for Transaction templates.
|
|
|
|
The djangorestframework plugin will analyse the model `TransactionTemplate` and parse all fields in the API.
|
|
|
|
"""
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-02-06 23:12:00 +00:00
|
|
|
class Meta:
|
|
|
|
model = TransactionTemplate
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
2020-02-07 19:47:49 +00:00
|
|
|
class TransactionSerializer(serializers.ModelSerializer):
|
2020-02-06 23:29:04 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for Transactions.
|
|
|
|
The djangorestframework plugin will analyse the model `Transaction` and parse all fields in the API.
|
|
|
|
"""
|
2020-08-31 18:15:48 +00:00
|
|
|
def validate_source(self, value):
|
2020-08-31 21:06:21 +00:00
|
|
|
if not value.is_active:
|
2020-08-31 18:15:48 +00:00
|
|
|
raise ValidationError(_("The transaction can't be saved since the source note "
|
|
|
|
"or the destination note is not active."))
|
2020-08-31 21:06:21 +00:00
|
|
|
return value
|
2020-08-31 18:15:48 +00:00
|
|
|
|
|
|
|
def validate_destination(self, value):
|
2020-08-31 21:06:21 +00:00
|
|
|
if not value.is_active:
|
2020-08-31 18:15:48 +00:00
|
|
|
raise ValidationError(_("The transaction can't be saved since the source note "
|
|
|
|
"or the destination note is not active."))
|
2020-08-31 21:06:21 +00:00
|
|
|
return value
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-02-06 23:12:00 +00:00
|
|
|
class Meta:
|
|
|
|
model = Transaction
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
2020-08-31 18:15:48 +00:00
|
|
|
class RecurrentTransactionSerializer(TransactionSerializer):
|
2020-03-11 10:15:03 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for Transactions.
|
2020-03-19 19:37:48 +00:00
|
|
|
The djangorestframework plugin will analyse the model `RecurrentTransaction` and parse all fields in the API.
|
2020-03-11 10:15:03 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
2020-03-19 19:37:48 +00:00
|
|
|
model = RecurrentTransaction
|
2020-03-11 10:15:03 +00:00
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
2020-08-31 18:15:48 +00:00
|
|
|
class MembershipTransactionSerializer(TransactionSerializer):
|
2020-02-06 23:29:04 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for Membership transactions.
|
|
|
|
The djangorestframework plugin will analyse the model `MembershipTransaction` and parse all fields in the API.
|
|
|
|
"""
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-02-06 23:12:00 +00:00
|
|
|
class Meta:
|
|
|
|
model = MembershipTransaction
|
|
|
|
fields = '__all__'
|
2020-03-11 10:15:03 +00:00
|
|
|
|
|
|
|
|
2020-08-31 18:15:48 +00:00
|
|
|
class SpecialTransactionSerializer(TransactionSerializer):
|
2020-03-14 14:13:58 +00:00
|
|
|
"""
|
|
|
|
REST API Serializer for Special transactions.
|
|
|
|
The djangorestframework plugin will analyse the model `SpecialTransaction` and parse all fields in the API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = SpecialTransaction
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
2020-03-28 15:52:58 +00:00
|
|
|
# noinspection PyUnresolvedReferences
|
2020-03-11 10:15:03 +00:00
|
|
|
class TransactionPolymorphicSerializer(PolymorphicSerializer):
|
|
|
|
model_serializer_mapping = {
|
|
|
|
Transaction: TransactionSerializer,
|
2020-03-19 19:37:48 +00:00
|
|
|
RecurrentTransaction: RecurrentTransactionSerializer,
|
2020-03-11 10:15:03 +00:00
|
|
|
MembershipTransaction: MembershipTransactionSerializer,
|
2020-03-14 14:13:58 +00:00
|
|
|
SpecialTransaction: SpecialTransactionSerializer,
|
2020-03-11 10:15:03 +00:00
|
|
|
}
|
2020-03-18 13:42:35 +00:00
|
|
|
|
2020-09-01 13:54:56 +00:00
|
|
|
if "activity" in settings.INSTALLED_APPS:
|
2020-03-28 15:52:58 +00:00
|
|
|
from activity.models import GuestTransaction
|
|
|
|
from activity.api.serializers import GuestTransactionSerializer
|
|
|
|
model_serializer_mapping[GuestTransaction] = GuestTransactionSerializer
|
|
|
|
|
2020-08-05 14:23:32 +00:00
|
|
|
def validate(self, attrs):
|
|
|
|
resource_type = attrs.pop(self.resource_type_field_name)
|
|
|
|
serializer = self._get_serializer_from_resource_type(resource_type)
|
|
|
|
if self.instance:
|
|
|
|
instance = self.instance
|
|
|
|
info = model_meta.get_field_info(instance)
|
|
|
|
for attr, value in attrs.items():
|
|
|
|
if attr in info.relations and info.relations[attr].to_many:
|
|
|
|
field = getattr(instance, attr)
|
|
|
|
field.set(value)
|
|
|
|
else:
|
|
|
|
setattr(instance, attr, value)
|
2020-08-09 10:31:06 +00:00
|
|
|
instance.validate()
|
2020-08-05 14:23:32 +00:00
|
|
|
else:
|
2020-08-09 10:31:06 +00:00
|
|
|
serializer.Meta.model(**attrs).validate()
|
2020-08-05 14:23:32 +00:00
|
|
|
attrs[self.resource_type_field_name] = resource_type
|
|
|
|
return super().validate(attrs)
|
|
|
|
|
2020-03-18 13:42:35 +00:00
|
|
|
class Meta:
|
|
|
|
model = Transaction
|