nk20/apps/note/api/serializers.py

165 lines
4.7 KiB
Python
Raw Normal View History

2020-02-06 23:12:00 +00:00
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
2020-02-07 16:02:07 +00:00
from rest_framework import serializers
2020-02-07 19:47:49 +00:00
from rest_polymorphic.serializers import PolymorphicSerializer
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, \
2020-03-14 14:13:58 +00:00
TemplateTransaction, 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-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-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-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-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.
"""
note = serializers.SerializerMethodField()
2020-03-07 21:28:59 +00:00
2020-02-08 14:08:55 +00:00
class Meta:
model = Alias
fields = '__all__'
def get_note(self, alias):
return NotePolymorphicSerializer().to_representation(alias.note)
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,
NoteSpecial: NoteSpecialSerializer
}
2020-02-06 23:12:00 +00:00
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-03-07 21:28:59 +00:00
2020-02-06 23:12:00 +00:00
class Meta:
model = Transaction
fields = '__all__'
2020-03-11 10:15:03 +00:00
class TemplateTransactionSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Transactions.
The djangorestframework plugin will analyse the model `TemplateTransaction` and parse all fields in the API.
"""
class Meta:
model = TemplateTransaction
fields = '__all__'
2020-02-07 19:47:49 +00:00
class MembershipTransactionSerializer(serializers.ModelSerializer):
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-03-14 14:13:58 +00:00
class SpecialTransactionSerializer(serializers.ModelSerializer):
"""
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-11 10:15:03 +00:00
class TransactionPolymorphicSerializer(PolymorphicSerializer):
model_serializer_mapping = {
Transaction: TransactionSerializer,
TemplateTransaction: TemplateTransactionSerializer,
MembershipTransaction: MembershipTransactionSerializer,
2020-03-14 14:13:58 +00:00
SpecialTransaction: SpecialTransactionSerializer,
2020-03-11 10:15:03 +00:00
}