From 0f5dbee24b2366d8521dca5c5fd13a338accb530 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Mon, 20 Feb 2023 02:38:34 +0000 Subject: [PATCH] Add API route for protocoin printer balance --- apiserver/apiserver/api/throttles.py | 2 +- apiserver/apiserver/api/views.py | 32 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/apiserver/apiserver/api/throttles.py b/apiserver/apiserver/api/throttles.py index 88c2b7f..6724c5d 100644 --- a/apiserver/apiserver/api/throttles.py +++ b/apiserver/apiserver/api/throttles.py @@ -24,7 +24,7 @@ class LoggingThrottle(throttling.BaseThrottle): return True elif path == '/sessions/' and user == None: return True - elif path in ['/pinball/high_scores/']: + elif path in ['/pinball/high_scores/', '/protocoin/printer_balance/']: return True if request.data: diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index 714f30a..e7a92ce 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -1236,6 +1236,38 @@ class ProtocoinViewSet(Base): ) return Response(res) + @action(detail=False, methods=['get']) + def printer_balance(self, request, pk=None): + #auth_token = request.META.get('HTTP_AUTHORIZATION', '') + #if secrets.VEND_API_TOKEN and auth_token != 'Bearer ' + secrets.VEND_API_TOKEN: + # raise exceptions.PermissionDenied() + + track = cache.get('track', {}) + track_graphics_computer = track.get('PROTOGRAPH1', None) + + if not track_graphics_computer: + return Response(200) + + track_username = track_graphics_computer['username'] + track_time = track_graphics_computer['time'] + + try: + source_user = User.objects.get(username__iexact=track_username) + except User.DoesNotExist: + return Response(200) + + if time.time() - track_time > 10: + return Response(200) + + user_balance = source_user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] or 0 + user_balance = float(user_balance) + + res = dict( + balance=user_balance, + first_name=source_user.member.preferred_name, + ) + return Response(res) + @action(detail=True, methods=['post']) def card_vend_request(self, request, pk=None): try: