Skip to content

Commit

Permalink
Redo organization class
Browse files Browse the repository at this point in the history
  • Loading branch information
slo248 committed Sep 21, 2024
1 parent 2fd8f29 commit 3f6475e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions judge/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class Organization(models.Model):
'viewing the organization.'))
performance_points = models.FloatField(default=0)
member_count = models.IntegerField(default=0)
current_consumed_credit = models.FloatField(default=0, help_text='Total used credit this month')
available_credit = models.FloatField(default=0, help_text='Available credits')
monthly_credit = models.FloatField(default=0, help_text='Total monthly free credit left')

_pp_table = [pow(settings.VNOJ_ORG_PP_STEP, i) for i in range(settings.VNOJ_ORG_PP_ENTRIES)]

Expand Down Expand Up @@ -110,6 +113,23 @@ def get_absolute_url(self):
def get_users_url(self):
return reverse('organization_users', args=[self.slug])

def has_credit_left(self):
return self.current_consumed_credit < self.available_credit + self.monthly_credit

def consume_credit(self, consumed):
# reduce credit in monthly credit first
# then reduce the left to available credit
if self.monthly_credit >= consumed:
self.monthly_credit -= consumed
else:
consumed -= self.monthly_credit
self.monthly_credit = 0
# if available credit can be negative if we don't enable the monthly credit limitation
self.available_credit -= consumed

self.current_consumed_credit += consumed
self.save(update_fields=['monthly_credit', 'available_credit', 'current_consumed_credit'])

class Meta:
ordering = ['name']
permissions = (
Expand Down

0 comments on commit 3f6475e

Please sign in to comment.