Polymorphic types

This commit is contained in:
Yohann D'ANELLO 2020-02-07 20:47:49 +01:00
parent e8e22541fa
commit f52a89637c
7 changed files with 46 additions and 24 deletions

View File

@ -5,7 +5,7 @@
from activity.models import ActivityType, Activity, Guest
from rest_framework import serializers
class ActivityTypeSerializer(serializers.HyperlinkedModelSerializer):
class ActivityTypeSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Activity types.
The djangorestframework plugin will analyse the model `ActivityType` and parse all fields in the API.
@ -15,7 +15,7 @@ class ActivityTypeSerializer(serializers.HyperlinkedModelSerializer):
fields = '__all__'
class ActivitySerializer(serializers.HyperlinkedModelSerializer):
class ActivitySerializer(serializers.ModelSerializer):
"""
REST API Serializer for Activities.
The djangorestframework plugin will analyse the model `Activity` and parse all fields in the API.
@ -25,7 +25,7 @@ class ActivitySerializer(serializers.HyperlinkedModelSerializer):
fields = '__all__'
class GuestSerializer(serializers.HyperlinkedModelSerializer):
class GuestSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Guests.
The djangorestframework plugin will analyse the model `Guest` and parse all fields in the API.

View File

@ -3,10 +3,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from member.models import Profile, Club, Role, Membership
from rest_framework import serializers, viewsets
from rest_framework import serializers
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
class ProfileSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Profiles.
The djangorestframework plugin will analyse the model `Profile` and parse all fields in the API.
@ -16,7 +16,7 @@ class ProfileSerializer(serializers.HyperlinkedModelSerializer):
fields = '__all__'
class ClubSerializer(serializers.HyperlinkedModelSerializer):
class ClubSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Clubs.
The djangorestframework plugin will analyse the model `Club` and parse all fields in the API.
@ -26,7 +26,7 @@ class ClubSerializer(serializers.HyperlinkedModelSerializer):
fields = '__all__'
class RoleSerializer(serializers.HyperlinkedModelSerializer):
class RoleSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Roles.
The djangorestframework plugin will analyse the model `Role` and parse all fields in the API.
@ -36,7 +36,7 @@ class RoleSerializer(serializers.HyperlinkedModelSerializer):
fields = '__all__'
class MembershipSerializer(serializers.HyperlinkedModelSerializer):
class MembershipSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Memberships.
The djangorestframework plugin will analyse the model `Memberships` and parse all fields in the API.

View File

@ -5,49 +5,60 @@
from note.models.notes import Note, NoteClub, NoteSpecial, NoteUser
from note.models.transactions import TransactionTemplate, Transaction, MembershipTransaction
from rest_framework import serializers
from rest_polymorphic.serializers import PolymorphicSerializer
class NoteSerializer(serializers.HyperlinkedModelSerializer):
class NoteSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Notes.
The djangorestframework plugin will analyse the model `Note` and parse all fields in the API.
"""
class Meta:
model = Note
fields = ('balance', 'is_active', 'display_image', 'created_at',)
fields = '__all__'
extra_kwargs = {
'url': {'view_name': 'project-detail', 'lookup_field': 'pk'},
}
class NoteClubSerializer(serializers.HyperlinkedModelSerializer):
class NoteClubSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Club's notes.
The djangorestframework plugin will analyse the model `NoteClub` and parse all fields in the API.
"""
class Meta:
model = NoteClub
fields = ('balance', 'is_active', 'display_image', 'created_at', 'club',)
fields = '__all__'
class NoteSpecialSerializer(serializers.HyperlinkedModelSerializer):
class NoteSpecialSerializer(serializers.ModelSerializer):
"""
REST API Serializer for special notes.
The djangorestframework plugin will analyse the model `NoteSpecial` and parse all fields in the API.
"""
class Meta:
model = NoteSpecial
fields = ('balance', 'is_active', 'display_image', 'created_at', 'club', 'special_type',)
fields = '__all__'
class NoteUserSerializer(serializers.HyperlinkedModelSerializer):
class NoteUserSerializer(serializers.ModelSerializer):
"""
REST API Serializer for User's notes.
The djangorestframework plugin will analyse the model `NoteUser` and parse all fields in the API.
"""
class Meta:
model = NoteUser
fields = ('balance', 'is_active', 'display_image', 'created_at', 'user',)
fields = '__all__'
class NotePolymorphicSerializer(PolymorphicSerializer):
model_serializer_mapping = {
Note: NoteSerializer,
NoteUser: NoteUserSerializer,
NoteClub: NoteClubSerializer,
NoteSpecial: NoteSpecialSerializer
}
class TransactionTemplateSerializer(serializers.HyperlinkedModelSerializer):
class TransactionTemplateSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Transaction templates.
The djangorestframework plugin will analyse the model `TransactionTemplate` and parse all fields in the API.
@ -57,7 +68,7 @@ class TransactionTemplateSerializer(serializers.HyperlinkedModelSerializer):
fields = '__all__'
class TransactionSerializer(serializers.HyperlinkedModelSerializer):
class TransactionSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Transactions.
The djangorestframework plugin will analyse the model `Transaction` and parse all fields in the API.
@ -67,7 +78,7 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
fields = '__all__'
class MembershipTransactionSerializer(serializers.HyperlinkedModelSerializer):
class MembershipTransactionSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Membership transactions.
The djangorestframework plugin will analyse the model `MembershipTransaction` and parse all fields in the API.

View File

@ -2,7 +2,7 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from .views import NoteViewSet, NoteClubViewSet, NoteUserViewSet, NoteSpecialViewSet, \
from .views import NoteViewSet, NotePolymorphicViewSet, NoteClubViewSet, NoteUserViewSet, NoteSpecialViewSet, \
TransactionViewSet, TransactionTemplateViewSet, MembershipTransactionViewSet
@ -10,7 +10,7 @@ def register_note_urls(router, path):
"""
Configure router for Note REST API.
"""
router.register(path + r'note', NoteViewSet)
router.register(path + r'note', NotePolymorphicViewSet)
router.register(path + r'club', NoteClubViewSet)
router.register(path + r'user', NoteUserViewSet)
router.register(path + r'special', NoteSpecialViewSet)

View File

@ -4,7 +4,7 @@
from note.models.notes import Note, NoteClub, NoteSpecial, NoteUser
from note.models.transactions import TransactionTemplate, Transaction, MembershipTransaction
from .serializers import NoteSerializer, NoteClubSerializer, NoteSpecialSerializer, NoteUserSerializer, \
from .serializers import NoteSerializer, NotePolymorphicSerializer, NoteClubSerializer, NoteSpecialSerializer, NoteUserSerializer, \
TransactionTemplateSerializer, TransactionSerializer, MembershipTransactionSerializer
from rest_framework import viewsets
@ -49,6 +49,16 @@ class NoteUserViewSet(viewsets.ModelViewSet):
serializer_class = NoteUserSerializer
class NotePolymorphicViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `NoteUser` objects, serialize it to JSON with the given serializer,
then render it on /api/note/user/
"""
queryset = Note.objects.all()
serializer_class = NotePolymorphicSerializer
class TransactionTemplateViewSet(viewsets.ModelViewSet):
"""
REST API View set.

View File

@ -9,7 +9,7 @@ from .activity.urls import register_activity_urls
from .members.urls import register_members_urls
from .note.urls import register_note_urls
class UserSerializer(serializers.HyperlinkedModelSerializer):
class UserSerializer(serializers.ModelSerializer):
"""
REST API Serializer for Users.
The djangorestframework plugin will analyse the model `User` and parse all fields in the API.

View File

@ -21,4 +21,5 @@ requests-oauthlib==1.2.0
six==1.12.0
sqlparse==0.3.0
urllib3==1.25.3
djangorestframework==3.11.0
djangorestframework==3.9.0
django-rest-polymorphic==0.1.8