Skip to content

Commit b2aad66

Browse files
committed
Patch requests now update original credits rather than adding
1 parent 3de5c42 commit b2aad66

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

coral_credits/api/db_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,15 @@ def create_credit_resource_allocations(credit_allocation, resource_allocations):
342342
car, created = models.CreditAllocationResource.objects.get_or_create(
343343
allocation=credit_allocation,
344344
resource_class=resource_class,
345-
defaults={"resource_hours": resource_hours},
345+
defaults={"resource_hours": resource_hours, "allocated_resource_hours": resource_hours},
346346
)
347347
# If exists, update:
348348
if not created:
349-
car.resource_hours += resource_hours
349+
newly_allocated_resource_hours = resource_hours
350+
car.resource_hours += newly_allocated_resource_hours - car.allocated_resource_hours
351+
if car.resource_hours < 0:
352+
raise db_exceptions.InsufficientCredits("Cannot set credits to fewer than currently consumed")
353+
car.allocated_resource_hours = newly_allocated_resource_hours
350354
car.save()
351355

352356
# Refresh from db to get the updated resource_hours

coral_credits/api/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class CreditAllocationResource(models.Model):
8282
ResourceClass, on_delete=models.DO_NOTHING, related_name="+"
8383
)
8484
resource_hours = models.FloatField()
85+
allocated_resource_hours = models.FloatField()
8586
created = models.DateTimeField(auto_now_add=True)
8687

8788
class Meta:

coral_credits/api/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CreditAllocationResourceSerializer(serializers.ModelSerializer):
4646

4747
class Meta:
4848
model = models.CreditAllocationResource
49-
fields = ["id", "resource_class", "resource_hours"]
49+
fields = ["id", "resource_class", "resource_hours","allocated_resource_hours"]
5050

5151
def to_representation(self, instance):
5252
"""Pass the context to the ResourceClassSerializer"""

0 commit comments

Comments
 (0)