diff --git a/store/backend.py b/store/backend.py index 5d4c9d5186bc9288ec534c965c9c5f6093ff0dc6..73329205c245f89e3ee052fd92097dc486e5cf27 100644 --- a/store/backend.py +++ b/store/backend.py @@ -172,15 +172,14 @@ class PurchaseLogic: # warning: summertime/wintertime currently is not respected in the following calculations. This should be # implemented to avoid non-annullable transactions in the lost hour between summer- and wintertime annullable_time = config['T_ANNULLABLE_PURCHASE_M'] + now = datetime.now() time_limit = datetime.now() - timedelta(minutes=annullable_time) timezone = pytz.timezone('Europe/Berlin') time_limit = timezone.localize(time_limit) - print(purchase_id) + purchase = list(Purchase.objects.filter(id=purchase_id))[0] purchase_time = purchase.time_stamp - print("purchase_id:", purchase.id, "time_limit:", time_limit, "purchase_time", purchase_time, "valid:", - time_limit > purchase_time) if time_limit >= purchase_time: raise PurchaseNotAnnullable() @@ -188,7 +187,6 @@ class PurchaseLogic: user = list(User.objects.filter(id=purchase.user.id))[0] purchase.annullate() user.updateMoney(purchase.price) - print("user_id:", user.id, "purchase_id:", purchase.id, "user.money:", user.money) class ChargeLogic: @@ -253,3 +251,24 @@ class ChargeLogic: @staticmethod def __updateUserMoney(user, amount): user.updateMoney(amount) + + @staticmethod + def annullateCharge(charge_id): + # warning: summertime/wintertime currently is not respected in the following calculations. This should be + # implemented to avoid non-annullable transactions in the lost hour between summer- and wintertime + annullable_time = config['T_ANNULLABLE_CHARGE_M'] + + now = datetime.now() + time_limit = datetime.now() - timedelta(minutes=annullable_time) + timezone = pytz.timezone('Europe/Berlin') + time_limit = timezone.localize(time_limit) + + charge = list(Charge.objects.filter(id=charge_id))[0] + + if time_limit > charge.time_stamp: + raise ChargeNotAnnullable() + + with transaction.atomic(): + user = list(User.objects.filter(id=charge.user.id))[0] + charge.annullate() + user.updateMoney((-1) * charge.amount) diff --git a/store/models.py b/store/models.py index 6dbd8abd91800201d660fb19c9315a055d322028..3b7d01762c40ee2b32980061ce9e4f8779c5add1 100644 --- a/store/models.py +++ b/store/models.py @@ -40,6 +40,10 @@ class Charge(models.Model): amount = models.DecimalField(max_digits=6, decimal_places=2) annullated = models.BooleanField(null=False) + def annullate(self): + self.annullated = True + self.save() + class Purchase(models.Model): token = models.BigIntegerField(null=False, unique=True) user = models.ForeignKey('user', on_delete=models.CASCADE, null=False) diff --git a/store/store_exceptions.py b/store/store_exceptions.py index f2ce149cf08e4f8106cd93a45ff41ee8fe277242..2feeaca70df4e29ea6be54bd76282c2b76ccd09f 100644 --- a/store/store_exceptions.py +++ b/store/store_exceptions.py @@ -11,3 +11,8 @@ class NotAnnullable(Exception): class PurchaseNotAnnullable(Exception): def __init__(self): super().__init__("Einkauf ist nicht annullierbar!") + + +class ChargeNotAnnullable(Exception): + def __init__(self): + super().__init__("Aufladung ist nicht annullierbar")