From 98a163f63a2cb4063a5bb321b928d7b7a8d1d1b3 Mon Sep 17 00:00:00 2001 From: Yingtong Li Date: Mon, 28 Feb 2022 19:14:20 +1100 Subject: [PATCH] Don't error on invalid unit price, etc. --- sstreasury/models.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/sstreasury/models.py b/sstreasury/models.py index 8651d1e..d58e969 100644 --- a/sstreasury/models.py +++ b/sstreasury/models.py @@ -1,5 +1,5 @@ # Society Self-Service -# Copyright © 2018–2020 Yingtong Li (RunasSudo) +# Copyright © 2018–2022 Yingtong Li (RunasSudo) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by @@ -100,15 +100,23 @@ class BudgetRevision(models.Model): def get_revenue_total(self): total = Decimal(0) for item in self.revenue: - total += Decimal(item['Unit price']) * Decimal(item['Units']) - if item['IWT'] and item['Unit price'] > 0: - total -= (Decimal(item['Unit price']) - (Decimal(item['Unit price']) - Decimal('0.8133')) / Decimal('1.01884')) * item['Units'] + try: + total += Decimal(item['Unit price']) * Decimal(item['Units']) + if item['IWT'] and item['Unit price'] > 0: + total -= (Decimal(item['Unit price']) - (Decimal(item['Unit price']) - Decimal('0.8133')) / Decimal('1.01884')) * item['Units'] + except TypeError: + # Invalid unit price, etc. + pass return total def get_expense_total(self): total = Decimal(0) for item in self.expense: - total += Decimal(item['Unit price']) * Decimal(item['Units']) + try: + total += Decimal(item['Unit price']) * Decimal(item['Units']) + except TypeError: + # Invalid unit price, etc. + pass if not self.expense_no_emergency_fund: total *= Decimal('1.05') return total @@ -240,7 +248,11 @@ class ReimbursementClaim(models.Model): def get_total(self): total = Decimal(0) for item in self.items: - total += Decimal(item['Unit price']) * Decimal(item['Units']) + try: + total += Decimal(item['Unit price']) * Decimal(item['Units']) + except TypeError: + # Invalid unit price, etc. + pass return total def update_state(self, user, state):