From fbb388b67c7e9d87dcfcea37962ba441a6f5eba8 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 24 Aug 2022 01:51:11 +0000 Subject: [PATCH] Fix more Protocoin sum bugs --- apiserver/apiserver/api/serializers.py | 4 ++-- apiserver/apiserver/api/views.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index 20ece48..a6ddb51 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -120,7 +120,7 @@ class TransactionSerializer(serializers.ModelSerializer): if validated_data['protocoin'] < 0: user = validated_data['user'] - current_protocoin = user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] + current_protocoin = user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] or 0 new_protocoin = current_protocoin + validated_data['protocoin'] if new_protocoin < 0: raise ValidationError(dict(category='Insufficient funds. Member only has {} protocoin.'.format(current_protocoin))) @@ -141,7 +141,7 @@ class TransactionSerializer(serializers.ModelSerializer): if validated_data['protocoin'] < 0: user = validated_data['user'] # when updating, we need to subtract out the transaction being edited - current_protocoin = user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] - instance.protocoin + current_protocoin = (user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] or 0) - instance.protocoin new_protocoin = current_protocoin + validated_data['protocoin'] if new_protocoin < 0: raise ValidationError(dict(category='Insufficient funds. Member only had {} protocoin.'.format(current_protocoin))) diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index c661646..c722dee 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -1083,7 +1083,7 @@ class ProtocoinViewSet(Base): destination_member = get_object_or_404(models.Member, id=member_id) destination_user = destination_member.user - source_user_balance = source_user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] + source_user_balance = source_user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] or 0 source_user_balance = float(source_user_balance) if source_user_balance != balance: @@ -1140,7 +1140,7 @@ class ProtocoinViewSet(Base): source_card = get_object_or_404(models.Card, card_number=pk) source_user = source_card.user - user_balance = source_user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] + user_balance = source_user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] or 0 user_balance = float(user_balance) res = dict( @@ -1183,7 +1183,7 @@ class ProtocoinViewSet(Base): raise exceptions.ValidationError(dict(amount='Amount too small.')) - source_user_balance = source_user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] + source_user_balance = source_user.transactions.aggregate(Sum('protocoin'))['protocoin__sum'] or 0 source_user_balance = float(source_user_balance) if source_user_balance != balance: