Skip to content

Commit

Permalink
Merge pull request #3 from stackhpc/black-format
Browse files Browse the repository at this point in the history
black formatting
  • Loading branch information
scrungus authored Jun 26, 2024
2 parents 743dd27 + a4bf6f5 commit b1ea74b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 48 deletions.
47 changes: 31 additions & 16 deletions coral_credits/api/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from auditlog.mixins import LogAccessMixin
from auditlog.registry import auditlog
from django.db import models

# TODO(tylerchristie): add allocation window in here, to simplify.


class ResourceClass(models.Model):
name = models.CharField(max_length=200, unique=True)
created = models.DateTimeField(auto_now_add=True)
Expand Down Expand Up @@ -38,26 +40,34 @@ class CreditAllocation(models.Model):
end = models.DateTimeField()

class Meta:
unique_together = (('name', 'account',),
('account', 'start'))
unique_together = (
(
"name",
"account",
),
("account", "start"),
)

def __str__(self) -> str:
return f"{self.account} from {self.start}"


class CreditAllocationResource(models.Model):
allocation = models.ForeignKey(
CreditAllocation, on_delete=models.CASCADE,
related_name="resources")
CreditAllocation, on_delete=models.CASCADE, related_name="resources"
)
resource_class = models.ForeignKey(
ResourceClass, on_delete=models.DO_NOTHING,
related_name="+")
ResourceClass, on_delete=models.DO_NOTHING, related_name="+"
)
resource_hours = models.FloatField()
created = models.DateTimeField(auto_now_add=True)

class Meta:
unique_together = ('allocation', 'resource_class',)
ordering = ('allocation__start',)
unique_together = (
"allocation",
"resource_class",
)
ordering = ("allocation__start",)

def __str__(self) -> str:
return f"{self.resource_class} for {self.allocation}"
Expand All @@ -72,25 +82,30 @@ class Consumer(models.Model):
end = models.DateTimeField()

class Meta:
unique_together = ('consumer_ref', 'resource_provider',)
unique_together = (
"consumer_ref",
"resource_provider",
)

def __str__(self) -> str:
return f"{self.consumer_ref}@{self.resource_provider}"


class ResourceConsumptionRecord(models.Model):
consumer = models.ForeignKey(
Consumer,
on_delete=models.CASCADE,
related_name="resources")
Consumer, on_delete=models.CASCADE, related_name="resources"
)
resource_class = models.ForeignKey(
ResourceClass, on_delete=models.DO_NOTHING,
related_name="+")
ResourceClass, on_delete=models.DO_NOTHING, related_name="+"
)
resource_hours = models.FloatField()

class Meta:
unique_together = ('consumer', 'resource_class',)
ordering = ('consumer__start',)
unique_together = (
"consumer",
"resource_class",
)
ordering = ("consumer__start",)

def __str__(self) -> str:
return f"{self.consumer} from {self.resource_class}"
12 changes: 6 additions & 6 deletions coral_credits/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
class ResourceClassSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.ResourceClass
fields = ['url', 'name', 'created']
fields = ["url", "name", "created"]


class ResourceProviderSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.ResourceProvider
fields = ['url', 'name', 'created', 'email', 'info_url']
fields = ["url", "name", "created", "email", "info_url"]


class CreditAccountSerializer(serializers.ModelSerializer):
class Meta:
model = models.CreditAccount
fields = ['url', 'name', 'email', 'created']
fields = ["url", "name", "email", "created"]


class ResourceClass(serializers.ModelSerializer):
Expand All @@ -34,7 +34,7 @@ class CreditAllocationResource(serializers.ModelSerializer):

class Meta:
model = models.CreditAllocationResource
fields = ['resource_class', 'resource_hours']
fields = ["resource_class", "resource_hours"]


class CreditAllocation(serializers.ModelSerializer):
Expand All @@ -50,7 +50,7 @@ class ResourceConsumptionRecord(serializers.ModelSerializer):

class Meta:
model = models.ResourceConsumptionRecord
fields = ['resource_class', 'resource_hours']
fields = ["resource_class", "resource_hours"]


class Consumer(serializers.ModelSerializer):
Expand All @@ -59,7 +59,7 @@ class Consumer(serializers.ModelSerializer):

class Meta:
model = models.Consumer
fields = ['consumer_ref', 'resource_provider', 'start', 'end', "resources"]
fields = ["consumer_ref", "resource_provider", "start", "end", "resources"]


class ConsumerRequest(serializers.Serializer):
Expand Down
45 changes: 25 additions & 20 deletions coral_credits/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def list(self, request):
"""
queryset = models.CreditAccount.objects.all()
serializer = serializers.CreditAccountSerializer(
queryset, many=True, context={'request': request})
queryset, many=True, context={"request": request}
)
return Response(serializer.data)

def retrieve(self, request, pk=None):
Expand All @@ -36,23 +37,18 @@ def retrieve(self, request, pk=None):
queryset = models.CreditAccount.objects.all()
account = get_object_or_404(queryset, pk=pk)
serializer = serializers.CreditAccountSerializer(
account, context={'request': request})
account, context={"request": request}
)
account_summary = serializer.data

# TODO(johngarbutt) look for any during the above allocations
all_allocations_query = models.CreditAllocation.objects.filter(
account__pk=pk
)
allocations = serializers.CreditAllocation(
all_allocations_query, many=True
)
all_allocations_query = models.CreditAllocation.objects.filter(account__pk=pk)
allocations = serializers.CreditAllocation(all_allocations_query, many=True)

# TODO(johngarbutt) look for any during the above allocations
consumers_query = models.Consumer.objects.filter(
account__pk=pk
)
consumers_query = models.Consumer.objects.filter(account__pk=pk)
consumers = serializers.Consumer(
consumers_query, many=True, context={'request': request}
consumers_query, many=True, context={"request": request}
)

account_summary["allocations"] = allocations.data
Expand All @@ -63,13 +59,19 @@ def retrieve(self, request, pk=None):
for allocation in account_summary["allocations"]:
for resource_allocation in allocation["resources"]:
if "resource_hours_remaining" not in resource_allocation:
resource_allocation["resource_hours_remaining"] = \
resource_allocation["resource_hours_remaining"] = (
resource_allocation["resource_hours"]
)
for consumer in account_summary["consumers"]:
for resource_consumer in consumer["resources"]:
consume_resource = resource_consumer["resource_class"]["name"]
if (resource_allocation["resource_class"]["name"] == consume_resource):
resource_allocation["resource_hours_remaining"] -= float(resource_consumer["resource_hours"])
if (
resource_allocation["resource_class"]["name"]
== consume_resource
):
resource_allocation["resource_hours_remaining"] -= float(
resource_consumer["resource_hours"]
)

return Response(account_summary)

Expand Down Expand Up @@ -99,7 +101,8 @@ def update(self, request, pk=None):

rp_queryset = models.ResourceProvider.objects.all()
resource_provider = get_object_or_404(
rp_queryset, pk=request.data["resource_provider_id"])
rp_queryset, pk=request.data["resource_provider_id"]
)

account_queryset = models.CreditAccount.objects.all()
account = get_object_or_404(account_queryset, pk=pk)
Expand All @@ -108,10 +111,12 @@ def update(self, request, pk=None):
for resource in request.data["resources"]:
name = resource["resource_class"]["name"]
resource_class = models.ResourceClass.objects.get(name=name)
resource_records.append(dict(
resource_class=resource_class,
resource_hours=resource["resource_hours"],
))
resource_records.append(
dict(
resource_class=resource_class,
resource_hours=resource["resource_hours"],
)
)

# TODO(johngarbutt): add validation we have enough credits

Expand Down
12 changes: 6 additions & 6 deletions coral_credits/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@


router = routers.DefaultRouter()
router.register(r'resource_class', views.ResourceClassViewSet)
router.register(r'resource_provider', views.ResourceProviderViewSet)
router.register(r'account', views.AccountViewSet, basename="creditaccount")
router.register(r"resource_class", views.ResourceClassViewSet)
router.register(r"resource_provider", views.ResourceProviderViewSet)
router.register(r"account", views.AccountViewSet, basename="creditaccount")


def status(request):
Expand All @@ -37,9 +37,9 @@ def status(request):
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('_status/', status, name='status'),
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path("_status/", status, name="status"),
path("", include(router.urls)),
path("api-auth/", include("rest_framework.urls", namespace="rest_framework")),
path("admin/", admin.site.urls),
]

Expand Down

0 comments on commit b1ea74b

Please sign in to comment.