Add API route for vending card balance

master
Tanner Collin 2 years ago
parent 50a210a9b2
commit 0257a8cd63
  1. 20
      apiserver/apiserver/api/views.py
  2. 6
      apiserver/apiserver/secrets.py.example
  3. 2
      webclient/src/Paymaster.js

@ -1082,8 +1082,6 @@ class ProtocoinViewSet(Base):
source_user_balance = source_user.transactions.aggregate(Sum('protocoin'))['protocoin__sum']
source_user_balance = float(source_user_balance)
print(source_user_balance)
if source_user_balance != balance:
raise exceptions.ValidationError(dict(balance='Incorrect current balance.'))
@ -1125,6 +1123,24 @@ class ProtocoinViewSet(Base):
return Response(200)
@action(detail=True, methods=['get'])
def card_vend_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()
card = get_object_or_404(models.Card, card_number=pk)
user = card.user
user_balance = user.transactions.aggregate(Sum('protocoin'))['protocoin__sum']
user_balance = float(user_balance)
res = dict(
balance=user_balance,
first_name=user.member.first_name,
)
return Response(res)
class RegistrationView(RegisterView):

@ -56,6 +56,12 @@ AUTH_API_KEY = ''
# head /dev/urandom | base32 | head -c 40
DOOR_API_TOKEN = ''
# Vending machine cards API token
# Set this to random characters
# For example, use the output of this:
# head /dev/urandom | base32 | head -c 40
VEND_API_TOKEN = ''
# Protospace general info
DOOR_CODE = ''
WIFI_PASS = ''

@ -92,7 +92,7 @@ export function Paymaster(props) {
<Header size='medium'>Protocoin</Header>
<p>Protocoin is used to buy things from Protospace's vending machines.</p>
<p>Current balance: &thinsp;{user.member.protocoin}</p>
<p>Current balance: &thinsp;{user.member.protocoin.toFixed(2)}</p>
<Grid stackable padded columns={2}>
<Grid.Column width={5}>

Loading…
Cancel
Save