From df1490dd2d4e0091e627d7e951e80964871370d3 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Fri, 17 Jan 2020 07:20:07 +0000 Subject: [PATCH] Add API route for transactions --- apiserver/apiserver/api/serializers.py | 14 ++++++++++++++ apiserver/apiserver/api/views.py | 11 ++++++++++- apiserver/apiserver/urls.py | 1 + webclient/src/Transactions.js | 2 +- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index b2bdb46..8f840db 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -203,9 +203,23 @@ class CardSerializer(serializers.ModelSerializer): class TransactionSerializer(serializers.ModelSerializer): + account_type = serializers.ChoiceField(['Interac', 'TD Chequing', 'Paypal', 'Dream Pmt', 'PayPal', 'Square Pmt', 'Member', 'Clearing', 'Cash']) + info_source = serializers.ChoiceField(['Web', 'DB Edit', 'System', 'Receipt or Stmt', 'Quicken Import', 'Paypal IPN', 'Auto', 'Nexus DB Bulk', 'PayPal IPN', 'IPN Trigger', 'Intranet Receipt', 'Automatic', 'Manual']) class Meta: model = models.Transaction fields = '__all__' + read_only_fields = [ + 'id', + 'last_seen_at', + 'user', + 'recorder', + ] + + def create(self, validated_data): + member = get_object_or_404(models.Member, id=validated_data['member_id']) + if member.user: + validated_data['user'] = member.user + return super().create(validated_data) diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index 77ed9da..c32845a 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -135,7 +135,7 @@ class MemberViewSet(Base, Retrieve, Update): class CardViewSet(Base, Create, Retrieve, Update, Destroy): - permission_classes = [AllowMetadata | IsAuthenticated, IsObjOwnerOrAdmin, IsAdminOrReadOnly] + permission_classes = [AllowMetadata | IsAuthenticated, IsObjOwnerOrAdmin] queryset = models.Card.objects.all() serializer_class = serializers.CardSerializer @@ -192,6 +192,15 @@ class TrainingViewSet(Base, Retrieve, Create, Update): serializer.save(user=self.request.user) +class TransactionViewSet(Base, Create, Retrieve, Update): + permission_classes = [AllowMetadata | IsAuthenticated, IsObjOwnerOrAdmin] + queryset = models.Transaction.objects.all() + serializer_class = serializers.TransactionSerializer + + def perform_create(self, serializer): + serializer.save(recorder=self.request.user) + + class UserView(views.APIView): permission_classes = [AllowMetadata | IsAuthenticated] diff --git a/apiserver/apiserver/urls.py b/apiserver/apiserver/urls.py index b81cf6c..c4f3b75 100644 --- a/apiserver/apiserver/urls.py +++ b/apiserver/apiserver/urls.py @@ -13,6 +13,7 @@ router.register(r'members', views.MemberViewSet, basename='members') router.register(r'courses', views.CourseViewSet, basename='course') router.register(r'sessions', views.SessionViewSet, basename='session') router.register(r'training', views.TrainingViewSet, basename='training') +router.register(r'transactions', views.TransactionViewSet, basename='transaction') #router.register(r'me', views.FullMemberView, basename='fullmember') #router.register(r'registration', views.RegistrationViewSet, basename='register') diff --git a/webclient/src/Transactions.js b/webclient/src/Transactions.js index f17a949..e730c0a 100644 --- a/webclient/src/Transactions.js +++ b/webclient/src/Transactions.js @@ -75,7 +75,7 @@ export function TransactionDetail(props) { Account: - {t.account} + {t.account_type} Info Source: