Fix more Protocoin sum bugs

This commit is contained in:
Tanner Collin 2022-08-24 01:51:11 +00:00
parent c88d5add1f
commit fbb388b67c
2 changed files with 5 additions and 5 deletions

View File

@ -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)))

View File

@ -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: