Clean code

This commit is contained in:
Yohann D'ANELLO 2020-02-07 17:02:07 +01:00
parent c28884114c
commit 5633f0123d
10 changed files with 226 additions and 157 deletions

View File

@ -0,0 +1,35 @@
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from activity.models import ActivityType, Activity, Guest
from rest_framework import serializers
class ActivityTypeSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Activity types.
The djangorestframework plugin will analyse the model `ActivityType` and parse all fields in the API.
"""
class Meta:
model = ActivityType
fields = '__all__'
class ActivitySerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Activities.
The djangorestframework plugin will analyse the model `Activity` and parse all fields in the API.
"""
class Meta:
model = Activity
fields = '__all__'
class GuestSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Guests.
The djangorestframework plugin will analyse the model `Guest` and parse all fields in the API.
"""
class Meta:
model = Guest
fields = '__all__'

14
apps/api/activity/urls.py Normal file
View File

@ -0,0 +1,14 @@
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from .views import ActivityTypeViewSet, ActivityViewSet, GuestViewSet
def register_activity_urls(router, path):
"""
Configure router for Activity REST API.
"""
router.register(path + r'activity', ActivityViewSet)
router.register(path + r'type', ActivityTypeViewSet)
router.register(path + r'guest', GuestViewSet)

View File

@ -2,18 +2,9 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from .models import ActivityType, Activity, Guest
from rest_framework import serializers, viewsets
class ActivityTypeSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Activity types.
The djangorestframework plugin will analyse the model `ActivityType` and parse all fields in the API.
"""
class Meta:
model = ActivityType
fields = '__all__'
from activity.models import ActivityType, Activity, Guest
from .serializers import ActivityTypeSerializer, ActivitySerializer, GuestSerializer
from rest_framework import viewsets
class ActivityTypeViewSet(viewsets.ModelViewSet):
"""
@ -25,16 +16,6 @@ class ActivityTypeViewSet(viewsets.ModelViewSet):
serializer_class = ActivityTypeSerializer
class ActivitySerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Activities.
The djangorestframework plugin will analyse the model `Activity` and parse all fields in the API.
"""
class Meta:
model = Activity
fields = '__all__'
class ActivityViewSet(viewsets.ModelViewSet):
"""
REST API View set.
@ -45,16 +26,6 @@ class ActivityViewSet(viewsets.ModelViewSet):
serializer_class = ActivitySerializer
class GuestSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Guests.
The djangorestframework plugin will analyse the model `Guest` and parse all fields in the API.
"""
class Meta:
model = Guest
fields = '__all__'
class GuestViewSet(viewsets.ModelViewSet):
"""
REST API View set.
@ -62,4 +33,4 @@ class GuestViewSet(viewsets.ModelViewSet):
then render it on /api/activity/guest/
"""
queryset = Guest.objects.all()
serializer_class = GuestSerializer
serializer_class = GuestSerializer

View File

@ -0,0 +1,46 @@
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from member.models import Profile, Club, Role, Membership
from rest_framework import serializers, viewsets
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Profiles.
The djangorestframework plugin will analyse the model `Profile` and parse all fields in the API.
"""
class Meta:
model = Profile
fields = '__all__'
class ClubSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Clubs.
The djangorestframework plugin will analyse the model `Club` and parse all fields in the API.
"""
class Meta:
model = Club
fields = '__all__'
class RoleSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Roles.
The djangorestframework plugin will analyse the model `Role` and parse all fields in the API.
"""
class Meta:
model = Role
fields = '__all__'
class MembershipSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Memberships.
The djangorestframework plugin will analyse the model `Memberships` and parse all fields in the API.
"""
class Meta:
model = Membership
fields = '__all__'

15
apps/api/members/urls.py Normal file
View File

@ -0,0 +1,15 @@
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from .views import ProfileViewSet, ClubViewSet, RoleViewSet, MembershipViewSet
def register_members_urls(router, path):
"""
Configure router for Member REST API.
"""
router.register(path + r'profile', ProfileViewSet)
router.register(path + r'club', ClubViewSet)
router.register(path + r'role', RoleViewSet)
router.register(path + r'membership', MembershipViewSet)

View File

@ -2,17 +2,9 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from .models import Profile, Club, Role, Membership
from rest_framework import serializers, viewsets
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Profiles.
The djangorestframework plugin will analyse the model `Profile` and parse all fields in the API.
"""
class Meta:
model = Profile
fields = '__all__'
from member.models import Profile, Club, Role, Membership
from .serializers import ProfileSerializer, ClubSerializer, RoleSerializer, MembershipSerializer
from rest_framework import viewsets
class ProfileViewSet(viewsets.ModelViewSet):
@ -25,16 +17,6 @@ class ProfileViewSet(viewsets.ModelViewSet):
serializer_class = ProfileSerializer
class ClubSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Clubs.
The djangorestframework plugin will analyse the model `Club` and parse all fields in the API.
"""
class Meta:
model = Club
fields = '__all__'
class ClubViewSet(viewsets.ModelViewSet):
"""
REST API View set.
@ -45,16 +27,6 @@ class ClubViewSet(viewsets.ModelViewSet):
serializer_class = ClubSerializer
class RoleSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Roles.
The djangorestframework plugin will analyse the model `Role` and parse all fields in the API.
"""
class Meta:
model = Role
fields = '__all__'
class RoleViewSet(viewsets.ModelViewSet):
"""
REST API View set.
@ -65,16 +37,6 @@ class RoleViewSet(viewsets.ModelViewSet):
serializer_class = RoleSerializer
class MembershipSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Memberships.
The djangorestframework plugin will analyse the model `Memberships` and parse all fields in the API.
"""
class Meta:
model = Membership
fields = '__all__'
class MembershipViewSet(viewsets.ModelViewSet):
"""
REST API View set.
@ -83,4 +45,3 @@ class MembershipViewSet(viewsets.ModelViewSet):
"""
queryset = Membership.objects.all()
serializer_class = MembershipSerializer

View File

@ -2,9 +2,10 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from .models.notes import Note, NoteClub, NoteSpecial, NoteUser
from .models.transactions import TransactionTemplate, Transaction, MembershipTransaction
from rest_framework import serializers, viewsets
from note.models.notes import Note, NoteClub, NoteSpecial, NoteUser
from note.models.transactions import TransactionTemplate, Transaction, MembershipTransaction
from rest_framework import serializers
class NoteSerializer(serializers.HyperlinkedModelSerializer):
"""
@ -16,16 +17,6 @@ class NoteSerializer(serializers.HyperlinkedModelSerializer):
fields = ('balance', 'is_active', 'display_image', 'created_at',)
class NoteViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `Note` objects, serialize it to JSON with the given serializer,
then render it on /api/note/note/
"""
queryset = Note.objects.all()
serializer_class = NoteSerializer
class NoteClubSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Club's notes.
@ -56,16 +47,6 @@ class NoteSpecialSerializer(serializers.HyperlinkedModelSerializer):
fields = ('balance', 'is_active', 'display_image', 'created_at', 'club', 'special_type',)
class NoteSpecialViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `NoteSpecial` objects, serialize it to JSON with the given serializer,
then render it on /api/note/special/
"""
queryset = NoteSpecial.objects.all()
serializer_class = NoteSpecialSerializer
class NoteUserSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for User's notes.
@ -76,16 +57,6 @@ class NoteUserSerializer(serializers.HyperlinkedModelSerializer):
fields = ('balance', 'is_active', 'display_image', 'created_at', 'user',)
class NoteUserViewSet(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 = NoteUser.objects.all()
serializer_class = NoteUserSerializer
class TransactionTemplateSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Transaction templates.
@ -96,16 +67,6 @@ class TransactionTemplateSerializer(serializers.HyperlinkedModelSerializer):
fields = '__all__'
class TransactionTemplateViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `TransactionTemplate` objects, serialize it to JSON with the given serializer,
then render it on /api/note/transaction/template/
"""
queryset = TransactionTemplate.objects.all()
serializer_class = TransactionTemplateSerializer
class TransactionSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Transactions.
@ -116,16 +77,6 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
fields = '__all__'
class TransactionViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `Transaction` objects, serialize it to JSON with the given serializer,
then render it on /api/note/transaction/transaction/
"""
queryset = Transaction.objects.all()
serializer_class = TransactionSerializer
class MembershipTransactionSerializer(serializers.HyperlinkedModelSerializer):
"""
REST API Serializer for Membership transactions.
@ -134,13 +85,3 @@ class MembershipTransactionSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = MembershipTransaction
fields = '__all__'
class MembershipTransactionViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `MembershipTransaction` objects, serialize it to JSON with the given serializer,
then render it on /api/note/transaction/membership/
"""
queryset = MembershipTransaction.objects.all()
serializer_class = MembershipTransactionSerializer

20
apps/api/note/urls.py Normal file
View File

@ -0,0 +1,20 @@
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from .views import NoteViewSet, NoteClubViewSet, NoteUserViewSet, NoteSpecialViewSet, \
TransactionViewSet, TransactionTemplateViewSet, MembershipTransactionViewSet
def register_note_urls(router, path):
"""
Configure router for Note REST API.
"""
router.register(path + r'note', NoteViewSet)
router.register(path + r'club', NoteClubViewSet)
router.register(path + r'user', NoteUserViewSet)
router.register(path + r'special', NoteSpecialViewSet)
router.register(path + r'transaction/transaction', TransactionViewSet)
router.register(path + r'transaction/template', TransactionTemplateViewSet)
router.register(path + r'transaction/membership', MembershipTransactionViewSet)

79
apps/api/note/views.py Normal file
View File

@ -0,0 +1,79 @@
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from note.models.notes import Note, NoteClub, NoteSpecial, NoteUser
from note.models.transactions import TransactionTemplate, Transaction, MembershipTransaction
from .serializers import NoteSerializer, NoteClubSerializer, NoteSpecialSerializer, NoteUserSerializer, \
TransactionTemplateSerializer, TransactionSerializer, MembershipTransactionSerializer
from rest_framework import viewsets
class NoteViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `Note` objects, serialize it to JSON with the given serializer,
then render it on /api/note/note/
"""
queryset = Note.objects.all()
serializer_class = NoteSerializer
class NoteClubViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `NoteClub` objects, serialize it to JSON with the given serializer,
then render it on /api/note/club/
"""
queryset = NoteClub.objects.all()
serializer_class = NoteClubSerializer
class NoteSpecialViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `NoteSpecial` objects, serialize it to JSON with the given serializer,
then render it on /api/note/special/
"""
queryset = NoteSpecial.objects.all()
serializer_class = NoteSpecialSerializer
class NoteUserViewSet(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 = NoteUser.objects.all()
serializer_class = NoteUserSerializer
class TransactionTemplateViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `TransactionTemplate` objects, serialize it to JSON with the given serializer,
then render it on /api/note/transaction/template/
"""
queryset = TransactionTemplate.objects.all()
serializer_class = TransactionTemplateSerializer
class TransactionViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `Transaction` objects, serialize it to JSON with the given serializer,
then render it on /api/note/transaction/transaction/
"""
queryset = Transaction.objects.all()
serializer_class = TransactionSerializer
class MembershipTransactionViewSet(viewsets.ModelViewSet):
"""
REST API View set.
The djangorestframework plugin will get all `MembershipTransaction` objects, serialize it to JSON with the given serializer,
then render it on /api/note/transaction/membership/
"""
queryset = MembershipTransaction.objects.all()
serializer_class = MembershipTransactionSerializer

View File

@ -5,10 +5,9 @@
from django.conf.urls import url, include
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
from member.serializers import ProfileViewSet, ClubViewSet, RoleViewSet, MembershipViewSet
from activity.serializers import ActivityTypeViewSet, ActivityViewSet, GuestViewSet
from note.serializers import NoteViewSet, NoteClubViewSet, NoteUserViewSet, NoteSpecialViewSet, \
TransactionViewSet, TransactionTemplateViewSet, MembershipTransactionViewSet
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):
"""
@ -33,25 +32,13 @@ router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
# Routers for members app
router.register(r'members/profile', ProfileViewSet)
router.register(r'members/club', ClubViewSet)
router.register(r'members/role', RoleViewSet)
router.register(r'members/membership', MembershipViewSet)
register_members_urls(router, r'members/')
# Routers for activity app
router.register(r'activity/activity', ActivityViewSet)
router.register(r'activity/type', ActivityTypeViewSet)
router.register(r'activity/guest', GuestViewSet)
register_activity_urls(router, r'activity/')
# Routers for note app
router.register(r'note/note', NoteViewSet)
router.register(r'note/club', NoteClubViewSet)
router.register(r'note/user', NoteUserViewSet)
router.register(r'note/special', NoteSpecialViewSet)
router.register(r'note/transaction/transaction', TransactionViewSet)
router.register(r'note/transaction/template', TransactionTemplateViewSet)
router.register(r'note/transaction/membership', MembershipTransactionViewSet)
register_note_urls(router, r'note/')
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.