nk20/apps/note/api/serializers.py

172 lines
4.9 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, \
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__'
read_only_fields = [f.name for f in model._meta.get_fields()] # Notes 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__'
read_only_fields = ('note', 'club', )
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__'
read_only_fields = ('note', )
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__'
read_only_fields = ('note', 'user', )
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__'
read_only_fields = ('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
class Meta:
model = Note
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__'
class RecurrentTransactionSerializer(serializers.ModelSerializer):
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-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,
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
}
class Meta:
model = Transaction