Added a small code warning and added more comments to models.py

This commit is contained in:
ChaoticByte 2025-09-07 22:29:07 +02:00
parent 054c5db2f2
commit 5fefee2282
No known key found for this signature in database
2 changed files with 5 additions and 4 deletions

View file

@ -40,6 +40,7 @@ class Drink(models.Model):
do_not_count = models.BooleanField(default=False)
def delete(self, *args, **kwargs):
# we flag the field as deleted.
self.deleted = True
super().save()
@ -107,10 +108,9 @@ class Order(models.Model):
price_sum = models.DecimalField(max_digits=6, decimal_places=2, default=0, editable=False)
content_litres = models.DecimalField(max_digits=6, decimal_places=3, default=0, editable=False)
# TODO: Add more comments on how and why the save & delete functions are implemented
# address this in a refactoring issue.
def save(self, *args, **kwargs):
# saving this may affect other fields
# so we reimplement the save function
drink = Drink.objects.get(pk=self.drink.pk)
if self._state.adding and drink.available > 0:
if not drink.do_not_count:
@ -126,6 +126,7 @@ class Order(models.Model):
raise ValidationError("This entry can't be changed.")
def delete(self, *args, **kwargs):
# when deleting, we affect other fields as well.
self.user.balance += self.price_sum
self.user.save()
drink = Drink.objects.get(pk=self.drink.pk)