diff --git a/backend/backend_api/admin.py b/backend/backend_api/admin.py index 9189618..f3919bf 100644 --- a/backend/backend_api/admin.py +++ b/backend/backend_api/admin.py @@ -103,7 +103,7 @@ def execute_mailer(self, request, obj): class DiscountAdmin(admin.ModelAdmin): - list_display = ('__str__', 'is_active', 'discount_percent', 'capacity', 'expiration_date') + list_display = ('__str__', 'is_active', 'discount_percent', 'capacity', 'remaining_capacity', 'expiration_date') readonly_fields = ('participants',) class Meta: diff --git a/backend/backend_api/models.py b/backend/backend_api/models.py index 47f033b..726e7f1 100644 --- a/backend/backend_api/models.py +++ b/backend/backend_api/models.py @@ -362,20 +362,21 @@ def participants(self): users.append(payment.user) return users - def _remaining_capacity(self) -> int: + @property + def remaining_capacity(self) -> int: return self.capacity - self.payment_set.filter(status=Payment.PaymentStatus.PAYMENT_CONFIRMED).count() def is_usable(self, user: User) -> bool: if not self.is_active: raise ValidationError(new_detailed_response(status.HTTP_400_BAD_REQUEST, "Discount is not active")) - if self._remaining_capacity() <= 0: + if self.remaining_capacity <= 0: raise ValidationError(new_detailed_response(status.HTTP_400_BAD_REQUEST, "Discount capacity is full")) if self.expiration_date < timezone.now(): raise ValidationError(new_detailed_response(status.HTTP_400_BAD_REQUEST, "Discount has expired")) - if self.payment_set.filter(user=user).exists(): + if self.payment_set.filter(user=user, status=Payment.PaymentStatus.PAYMENT_CONFIRMED).exists(): raise ValidationError(new_detailed_response(status.HTTP_400_BAD_REQUEST, "Discount has already been used by this user")) return True