From 63920008e589cea07588aad7bc92ff2ee3f95b2f Mon Sep 17 00:00:00 2001 From: Fabian Krueger <fabian.krueger@fau.de> Date: Mon, 23 Sep 2019 11:02:31 +0200 Subject: [PATCH] update specified handling on negative and positive amounts of money --- store/backend.py | 17 +++++++++-------- store/models.py | 16 ++++++++++++---- store/store_exceptions.py | 6 ++++++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/store/backend.py b/store/backend.py index 4f70525..5dba007 100644 --- a/store/backend.py +++ b/store/backend.py @@ -178,7 +178,7 @@ class PurchaseLogic: @staticmethod def __updateUserMoney(user, price): - return user.updateMoney(price * (-1)) + user.decrementMoney(price) @staticmethod def __updateProductStock(product): @@ -209,7 +209,7 @@ class PurchaseLogic: with transaction.atomic(): user = list(User.objects.filter(id=purchase.user.id))[0] purchase.annullate() - user.updateMoney(purchase.price) + user.incrementMoney(purchaes.price) class ChargeLogic: @@ -273,7 +273,8 @@ class ChargeLogic: @staticmethod def __updateUserMoney(user, amount): - user.updateMoney(amount) + print("amount:", amount) + user.charge(amount) """ Annulates a charge that is not older than a given number of minutes. Returns False, if the purchase is too old, @@ -300,7 +301,7 @@ class ChargeLogic: with transaction.atomic(): user = list(User.objects.filter(id=charge.user.id))[0] charge.annullate() - user.updateMoney((-1) * charge.amount) + user.decrementMoney(charge.amount) class TransferLogic: @@ -387,11 +388,11 @@ class TransferLogic: @staticmethod def __updateSenderMoney(sender, amount): - sender.updateMoney((-1) * amount) + sender.decrementMoney(amount) @staticmethod def __updateReceiverMoney(receiver, amount): - receiver.updateMoney(amount) + receiver.incrementMoney(amount) @staticmethod def annullateTransfer(transfer_id, token): @@ -412,6 +413,6 @@ class TransferLogic: sender = list(User.objects.filter(id=transfer.sender_id))[0] with transaction.atomic(): transfer.annullate() - receiver.updateMoney((-1) * transfer.amount) - sender.updateMoney(transfer.amount) + receiver.decrementMoney(transfer.amount) + sender.incrementMoney(transfer.amount) diff --git a/store/models.py b/store/models.py index 976902d..95632d3 100644 --- a/store/models.py +++ b/store/models.py @@ -14,14 +14,22 @@ class User(AbstractBaseUser): password = models.TextField(null=True) USERNAME_FIELD = 'id' - def updateMoney(self, amount): - if self.money + amount < 0: - raise UserNotEnoughMoney() - return False + def incrementMoney(self, amount): + if amount < 0: + raise NegativeMoneyAmount() self.money += Decimal(amount) self.save() return True + def decrementMoney(self, amount): + if amount < 0: + raise NegativeMoneyAmount() + if self.money - amount < 0: + raise UserNotEnoughMoney() + self.money -= Decimal(amount) + self.save() + return True + class Product(models.Model): name = models.TextField(null=False) diff --git a/store/store_exceptions.py b/store/store_exceptions.py index a2effa7..3f35b70 100644 --- a/store/store_exceptions.py +++ b/store/store_exceptions.py @@ -3,6 +3,11 @@ class UserNotEnoughMoney(Exception): super().__init__("Guthaben reicht nicht aus!") +class NegativeMoneyAmount(Exception): + def __init__(sefl): + super().__init__("Positiver Betrag erwartet!") + + class NotAnnullable(Exception): def __init__(self): super().__init__("Nicht annullierbar!") @@ -17,6 +22,7 @@ class ChargeNotAnnullable(Exception): def __init__(self): super().__init__("Aufladung ist nicht annullierbar!") + class TransferNotAnnullable(Exception): def __init__(self): super().__init__("Überweisung ist nicht annullierbar!") -- GitLab