From 5633f0123d7a881d6f2233a64ea242d0f51e6b93 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 7 Feb 2020 17:02:07 +0100 Subject: [PATCH] Clean code --- apps/api/activity/serializers.py | 35 ++++++++ apps/api/activity/urls.py | 14 ++++ .../serializers.py => api/activity/views.py} | 37 +-------- apps/api/members/serializers.py | 46 +++++++++++ apps/api/members/urls.py | 15 ++++ .../serializers.py => api/members/views.py} | 45 +---------- apps/{ => api}/note/serializers.py | 67 +--------------- apps/api/note/urls.py | 20 +++++ apps/api/note/views.py | 79 +++++++++++++++++++ apps/api/urls.py | 25 ++---- 10 files changed, 226 insertions(+), 157 deletions(-) create mode 100644 apps/api/activity/serializers.py create mode 100644 apps/api/activity/urls.py rename apps/{activity/serializers.py => api/activity/views.py} (51%) create mode 100644 apps/api/members/serializers.py create mode 100644 apps/api/members/urls.py rename apps/{member/serializers.py => api/members/views.py} (53%) rename apps/{ => api}/note/serializers.py (55%) create mode 100644 apps/api/note/urls.py create mode 100644 apps/api/note/views.py diff --git a/apps/api/activity/serializers.py b/apps/api/activity/serializers.py new file mode 100644 index 00000000..4d1bc449 --- /dev/null +++ b/apps/api/activity/serializers.py @@ -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__' diff --git a/apps/api/activity/urls.py b/apps/api/activity/urls.py new file mode 100644 index 00000000..ca0fab43 --- /dev/null +++ b/apps/api/activity/urls.py @@ -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) diff --git a/apps/activity/serializers.py b/apps/api/activity/views.py similarity index 51% rename from apps/activity/serializers.py rename to apps/api/activity/views.py index 4d79426b..af2f0fe1 100644 --- a/apps/activity/serializers.py +++ b/apps/api/activity/views.py @@ -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 \ No newline at end of file diff --git a/apps/api/members/serializers.py b/apps/api/members/serializers.py new file mode 100644 index 00000000..98e2ba15 --- /dev/null +++ b/apps/api/members/serializers.py @@ -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__' diff --git a/apps/api/members/urls.py b/apps/api/members/urls.py new file mode 100644 index 00000000..ffce1cbd --- /dev/null +++ b/apps/api/members/urls.py @@ -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) diff --git a/apps/member/serializers.py b/apps/api/members/views.py similarity index 53% rename from apps/member/serializers.py rename to apps/api/members/views.py index 2ead2c97..9755151e 100644 --- a/apps/member/serializers.py +++ b/apps/api/members/views.py @@ -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 - diff --git a/apps/note/serializers.py b/apps/api/note/serializers.py similarity index 55% rename from apps/note/serializers.py rename to apps/api/note/serializers.py index db63ea6b..be99eec3 100644 --- a/apps/note/serializers.py +++ b/apps/api/note/serializers.py @@ -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 diff --git a/apps/api/note/urls.py b/apps/api/note/urls.py new file mode 100644 index 00000000..766bc963 --- /dev/null +++ b/apps/api/note/urls.py @@ -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) diff --git a/apps/api/note/views.py b/apps/api/note/views.py new file mode 100644 index 00000000..eb4a3019 --- /dev/null +++ b/apps/api/note/views.py @@ -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 diff --git a/apps/api/urls.py b/apps/api/urls.py index c149b3f4..c9f3002d 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -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.