From 2ad143b1686c11cf374012cf2d4f7de9e0ab895d Mon Sep 17 00:00:00 2001 From: Claire Peters Date: Tue, 27 Jun 2023 12:36:40 -0700 Subject: [PATCH 1/2] add allocation usage and remove project attributes from projectdetail page --- coldfront/core/allocation/models.py | 20 ++++++++++++++----- coldfront/core/allocation/test_models.py | 15 ++++++++++++-- .../templates/project/project_detail.html | 8 +++++--- coldfront/core/test_helpers/factories.py | 4 ++-- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/coldfront/core/allocation/models.py b/coldfront/core/allocation/models.py index 7a9ccbb13..5b6518078 100644 --- a/coldfront/core/allocation/models.py +++ b/coldfront/core/allocation/models.py @@ -5,7 +5,7 @@ from enum import Enum from django.conf import settings -from django.core.exceptions import ValidationError +from django.core.exceptions import ValidationError, ObjectDoesNotExist from django.db import models from django.db.models import Q from django.utils.html import mark_safe @@ -136,17 +136,27 @@ def save(self, *args, **kwargs): @property def size(self): - return self.allocationattribute_set.get(allocation_attribute_type_id=1).value + try: + return self.allocationattribute_set.get(allocation_attribute_type_id=1).value + except ObjectDoesNotExist: + return None + @property def usage(self): - return self.allocationattribute_set.get(allocation_attribute_type_id=1).allocationattributeusage.value + try: + return self.allocationattribute_set.get( + allocation_attribute_type_id=1 + ).allocationattributeusage.value + except ObjectDoesNotExist: + return None @property def path(self): subdir_attribute = AllocationAttributeType.objects.get(name='Subdirectory') - attr_filter = ( Q(allocation_id=self.id) & - Q(allocation_attribute_type_id=subdir_attribute.pk)) + attr_filter = ( + Q(allocation_id=self.id) & Q(allocation_attribute_type_id=subdir_attribute.pk) + ) if AllocationAttribute.objects.filter(attr_filter): return AllocationAttribute.objects.get(attr_filter).value return '' diff --git a/coldfront/core/allocation/test_models.py b/coldfront/core/allocation/test_models.py index 386390cb1..940e2cb2b 100644 --- a/coldfront/core/allocation/test_models.py +++ b/coldfront/core/allocation/test_models.py @@ -2,7 +2,7 @@ from django.test import TestCase -from coldfront.core.test_helpers.factories import setup_models +from coldfront.core.test_helpers.factories import setup_models, AllocationFactory UTIL_FIXTURES = [ "coldfront/core/test_helpers/test_data/test_fixtures/ifx.json", @@ -19,7 +19,18 @@ def setUpTestData(cls): def test_allocation_str(self): """test that allocation str method returns correct string""" - allocation_str = '%s (%s)' % (self.proj_allocation.get_parent_resource.name, self.proj_allocation.project.pi) + allocation_str = '%s (%s)' % ( + self.proj_allocation.get_parent_resource.name, + self.proj_allocation.project.pi + ) self.assertEqual(str(self.proj_allocation), allocation_str) + def test_allocation_usage_property(self): + """Test that allocation usage property displays correctly""" + self.assertEqual(self.proj_allocation.usage, 10) + + def test_allocation_usage_property_na(self): + """Create allocation with no usage. Usage property should return None""" + allocation = AllocationFactory() + self.assertIsNone(allocation.usage) diff --git a/coldfront/core/project/templates/project/project_detail.html b/coldfront/core/project/templates/project/project_detail.html index 82755efff..a6721c6ed 100644 --- a/coldfront/core/project/templates/project/project_detail.html +++ b/coldfront/core/project/templates/project/project_detail.html @@ -97,7 +97,8 @@