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: