1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-12-25 08:52:23 +00:00
nk20/apps/note/api/serializers.py

265 lines
8.6 KiB
Python
Raw Normal View History

# Copyright (C) 2018-2021 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
from member.api.serializers import MembershipSerializer
from member.models import Membership
from note_kfet.middlewares import get_current_request
from permission.backends import PermissionBackend
from rest_framework.utils import model_meta
2020-02-07 16:02:07 +00:00
2020-02-18 20:14:29 +00:00
from ..models.notes import Note, NoteClub, NoteSpecial, NoteUser, Alias
2020-03-11 10:15:03 +00:00
from ..models.transactions import TransactionTemplate, Transaction, MembershipTransaction, TemplateCategory, \
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)
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
class Meta:
model = Note
2020-03-28 16:42:29 +00:00
class ConsumerSerializer(serializers.ModelSerializer):
"""
REST API Nested Serializer for Consumers.
return Alias, and the note Associated to it in
2020-03-28 16:42:29 +00:00
"""
note = serializers.SerializerMethodField()
email_confirmed = serializers.SerializerMethodField()
membership = serializers.SerializerMethodField()
2020-03-28 16:42:29 +00:00
class Meta:
model = Alias
fields = '__all__'
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)\
if PermissionBackend.check_perm(get_current_request(), "note.view_note", obj.note)\
else dict(
id=obj.note.id,
name=str(obj.note),
is_active=obj.note.is_active,
display_image=obj.note.display_image.url,
)
def get_email_confirmed(self, obj):
if isinstance(obj.note, NoteUser):
return obj.note.user.profile.email_confirmed
return True
def get_membership(self, obj):
if isinstance(obj.note, NoteUser):
memberships = Membership.objects.filter(
PermissionBackend.filter_queryset(get_current_request(), Membership, "view")).filter(
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):
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."))
return value
2020-08-31 18:15:48 +00:00
def validate_destination(self, value):
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."))
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.
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:
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__'
# noinspection PyUnresolvedReferences
2020-03-11 10:15:03 +00:00
class TransactionPolymorphicSerializer(PolymorphicSerializer):
model_serializer_mapping = {
Transaction: TransactionSerializer,
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-09-01 13:54:56 +00:00
if "activity" in settings.INSTALLED_APPS:
from activity.models import GuestTransaction
from activity.api.serializers import GuestTransactionSerializer
model_serializer_mapping[GuestTransaction] = GuestTransactionSerializer
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()
else:
2020-08-09 10:31:06 +00:00
serializer.Meta.model(**attrs).validate()
attrs[self.resource_type_field_name] = resource_type
return super().validate(attrs)
class Meta:
model = Transaction