Skip to content

Commit

Permalink
Fix formatting with black
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Powers committed Sep 30, 2024
1 parent eab1f86 commit 55de41b
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 105 deletions.
44 changes: 22 additions & 22 deletions allocations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ def pi_institution(self, obj):
keycloak_client = KeycloakClient()
uname = obj.project.pi.username
user = keycloak_client.get_user_by_username(uname)
return user['attributes'].get('affiliationInstitution', '')
return user["attributes"].get("affiliationInstitution", "")

list_display = (
'project_title',
'project',
'status',
'date_requested',
'date_reviewed',
'reviewer',
"project_title",
"project",
"status",
"date_requested",
"date_reviewed",
"reviewer",
)
fields = (
'pi_name',
'pi_email',
'pi_institution',
'project',
'project_title',
'project_description',
'justification',
'status',
'requestor',
'decision_summary',
'reviewer',
'date_requested',
'date_reviewed',
'start_date',
'expiration_date',
"pi_name",
"pi_email",
"pi_institution",
"project",
"project_title",
"project_description",
"justification",
"status",
"requestor",
"decision_summary",
"reviewer",
"date_requested",
"date_reviewed",
"start_date",
"expiration_date",
)
ordering = ["-date_requested"]

Expand Down
9 changes: 5 additions & 4 deletions allocations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ class ChargeBudget(models.Model):
related_name="projectbudgets",
on_delete=models.CASCADE,
)
project = models.ForeignKey(
Project, on_delete=models.CASCADE
)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
su_budget = models.IntegerField(default=0, validators=[MinValueValidator(0)])

class Meta:
unique_together = ('user', 'project',)
unique_together = (
"user",
"project",
)
93 changes: 64 additions & 29 deletions allocations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

LOG = logging.getLogger(__name__)


class StatusTests(TestCase):
def setUp(self):
User = get_user_model()
Expand All @@ -37,7 +38,7 @@ def setUp(self):
pi=self.test_requestor,
title="Test Project",
nickname="test_project",
charge_code="TEST123"
charge_code="TEST123",
)

self.test_project.save()
Expand All @@ -47,10 +48,14 @@ def tearDown(self):
self.test_reviewer.delete()
self.test_project.delete()

@mock.patch.object(KeycloakClient, '_lookup_group', return_value={"attributes": {"has_active_allocation": False}})
@mock.patch.object(KeycloakClient, 'update_project', return_value=None)
@mock.patch.object(
KeycloakClient,
"_lookup_group",
return_value={"attributes": {"has_active_allocation": False}},
)
@mock.patch.object(KeycloakClient, "update_project", return_value=None)
def test_deactivate_active_allocation(self, update_project_mock, lookup_group_mock):
""" Tests whether deactivate_active_allocation()
"""Tests whether deactivate_active_allocation()
updates an active allocation's status to inactive
"""
test_active_allocation = Allocation(
Expand All @@ -70,7 +75,7 @@ def test_deactivate_active_allocation(self, update_project_mock, lookup_group_mo
balance_service_version=2,
)
sample_dict = {"has_active_allocation": False}
sample_dict.get('has_active_allocation')
sample_dict.get("has_active_allocation")
tasks._deactivate_allocation(test_active_allocation)

update_project_mock.assert_called_once_with(
Expand All @@ -82,9 +87,15 @@ def test_deactivate_active_allocation(self, update_project_mock, lookup_group_mo

self.assertEqual(test_active_allocation.status, "inactive")

@mock.patch.object(KeycloakClient, '_lookup_group', return_value={"attributes": {"has_active_allocation": False}})
@mock.patch.object(KeycloakClient, 'update_project', return_value=None)
def test_deactivate_inactive_allocation(self, update_project_mock, lookup_group_mock):
@mock.patch.object(
KeycloakClient,
"_lookup_group",
return_value={"attributes": {"has_active_allocation": False}},
)
@mock.patch.object(KeycloakClient, "update_project", return_value=None)
def test_deactivate_inactive_allocation(
self, update_project_mock, lookup_group_mock
):
"""Tests whether deactivate_active_allocation() does not update
an inactive allocation's status
"""
Expand All @@ -105,7 +116,7 @@ def test_deactivate_inactive_allocation(self, update_project_mock, lookup_group_
balance_service_version=2,
)
sample_dict = {"has_active_allocation": False}
sample_dict.get('has_active_allocation')
sample_dict.get("has_active_allocation")
tasks._deactivate_allocation(test_inactive_allocation)

update_project_mock.assert_called_once_with(
Expand All @@ -128,7 +139,7 @@ def test_deactivate_multiple_allocations_of_projects(self):
pi=self.test_requestor, # Replace my_user with an actual User instance
title="Multiple Alloc Project",
nickname="multiple_alloc_project",
charge_code="multiple_alloc" # You can set a unique charge code
charge_code="multiple_alloc", # You can set a unique charge code
)

test_project_multiple_alloc.save()
Expand Down Expand Up @@ -176,11 +187,11 @@ def test_deactivate_multiple_allocations_of_projects(self):

passed_assertions = 0
for alloc in inactive_allocs:
if (alloc.justification == "earlier alloc"):
if alloc.justification == "earlier alloc":
self.assertEqual(alloc.status, "inactive")
passed_assertions += 1
alloc.delete()
if (alloc.justification == "later alloc"):
if alloc.justification == "later alloc":
self.assertEqual(alloc.status, "active")
passed_assertions += 1
alloc.delete()
Expand All @@ -189,8 +200,12 @@ def test_deactivate_multiple_allocations_of_projects(self):

self.assertEqual(passed_assertions, 2)

@mock.patch.object(KeycloakClient, '_lookup_group', return_value={"attributes": {"has_active_allocation": True}})
@mock.patch.object(KeycloakClient, 'update_project', return_value=None)
@mock.patch.object(
KeycloakClient,
"_lookup_group",
return_value={"attributes": {"has_active_allocation": True}},
)
@mock.patch.object(KeycloakClient, "update_project", return_value=None)
def test_active_approved_allocations(self, update_project_mock, lookup_group_mock):
"""Tests that active_approved_allocations() changes the status of
approved allocations to active
Expand All @@ -215,8 +230,9 @@ def test_active_approved_allocations(self, update_project_mock, lookup_group_moc
test_approved_allocation.save()
tasks.active_approved_allocations()

test_allocations = Allocation.objects.filter(project=self.test_project,
justification="approved alloc")
test_allocations = Allocation.objects.filter(
project=self.test_project, justification="approved alloc"
)

update_project_mock.assert_called_once_with(
test_approved_allocation.project.charge_code, has_active_allocation="true"
Expand All @@ -231,9 +247,15 @@ def test_active_approved_allocations(self, update_project_mock, lookup_group_moc

test_approved_allocation.delete()

@mock.patch.object(KeycloakClient, '_lookup_group', return_value={"attributes": {"has_active_allocation": False}})
@mock.patch.object(KeycloakClient, 'update_project', return_value=None)
def test_nonapproved_active_approved_allocation(self, update_project_mock, lookup_group_mock):
@mock.patch.object(
KeycloakClient,
"_lookup_group",
return_value={"attributes": {"has_active_allocation": False}},
)
@mock.patch.object(KeycloakClient, "update_project", return_value=None)
def test_nonapproved_active_approved_allocation(
self, update_project_mock, lookup_group_mock
):
"""Tests that active_approved_allocations() does not change
the status of non-approved allocations
"""
Expand All @@ -257,8 +279,9 @@ def test_nonapproved_active_approved_allocation(self, update_project_mock, looku
test_nonapproved_allocation.save()
tasks.active_approved_allocations()

test_allocations = Allocation.objects.filter(project=self.test_project,
justification="pending alloc")
test_allocations = Allocation.objects.filter(
project=self.test_project, justification="pending alloc"
)

for alloc in test_allocations:
self.assertEqual(alloc.status, "pending")
Expand Down Expand Up @@ -291,7 +314,7 @@ def setUp(self):
pi=self.test_requestor, # Replace my_user with an actual User instance
title="Test Project",
nickname="test_project",
charge_code="TEST123" # You can set a unique charge code
charge_code="TEST123", # You can set a unique charge code
)

self.test_project.save()
Expand All @@ -301,9 +324,15 @@ def tearDown(self):
self.test_reviewer.delete()
self.test_project.delete()

@mock.patch.object(KeycloakClient, '_lookup_group', return_value={"attributes": {"has_active_allocation": False}})
@mock.patch.object(KeycloakClient, 'update_project', return_value=None)
def test_expire_expired_alloc_allocations(self, update_project_mock, lookup_group_mock):
@mock.patch.object(
KeycloakClient,
"_lookup_group",
return_value={"attributes": {"has_active_allocation": False}},
)
@mock.patch.object(KeycloakClient, "update_project", return_value=None)
def test_expire_expired_alloc_allocations(
self, update_project_mock, lookup_group_mock
):
"""Tests that expore_allocations() changes the status
of all expired allocations to inactive
"""
Expand Down Expand Up @@ -338,16 +367,22 @@ def test_expire_expired_alloc_allocations(self, update_project_mock, lookup_grou
expired_allocations = Allocation.objects.filter(
project=self.test_project,
justification="expired allocation",
expiration_date__lte=timezone.now()
expiration_date__lte=timezone.now(),
)

for alloc in expired_allocations:
self.assertTrue(alloc.status, "inactive")
alloc.delete()

@mock.patch.object(KeycloakClient, '_lookup_group', return_value={"attributes": {"has_active_allocation": False}})
@mock.patch.object(KeycloakClient, 'update_project', return_value=None)
def test_expire_non_expired_alloc_allocations(self, update_project_mock, lookup_group_mock):
@mock.patch.object(
KeycloakClient,
"_lookup_group",
return_value={"attributes": {"has_active_allocation": False}},
)
@mock.patch.object(KeycloakClient, "update_project", return_value=None)
def test_expire_non_expired_alloc_allocations(
self, update_project_mock, lookup_group_mock
):
"""Tests that expore_allocations() does not change the status
of unexpired allocations
"""
Expand Down
42 changes: 21 additions & 21 deletions allocations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,27 @@ def approval(request):
"Waiting",
"waiting",
]:
errors[
"status"
] = 'Status must be "Pending", "pending", "Approved", "approved", "Rejected", "rejected"'
errors["status"] = (
'Status must be "Pending", "pending", "Approved", "approved", "Rejected", "rejected"'
)
else:
if data["start"]:
try:
validate_datestring(data["start"])
except ValidationError:
errors[
"start"
] = 'Start date must be a valid date string e.g. "2015-05-20" .'
errors["start"] = (
'Start date must be a valid date string e.g. "2015-05-20" .'
)
elif data["status"].lower() == "approved":
errors["start"] = "Start date is required."

if data["end"]:
try:
validate_datestring(data["end"])
except ValidationError:
errors[
"end"
] = 'Start date must be a valid date string e.g. "2015-05-20" .'
errors["end"] = (
'Start date must be a valid date string e.g. "2015-05-20" .'
)
elif data["status"].lower() == "approved":
errors["end"] = "Start date is required."

Expand Down Expand Up @@ -240,19 +240,19 @@ def approval(request):
try:
validate_datetimestring(data["dateRequested"])
except ValidationError:
errors[
"dateRequested"
] = 'Requested date must be a valid date string e.g. "2015-05-20T05:00:00Z" .'
errors["dateRequested"] = (
'Requested date must be a valid date string e.g. "2015-05-20T05:00:00Z" .'
)
# else:
# errors['dateRequested'] = 'Requested date is required.'

if data["dateReviewed"]:
try:
validate_datestring(data["dateReviewed"])
except ValidationError:
errors[
"dateReviewed"
] = 'Reviewed date must be a valid date string e.g. "2015-05-20" .'
errors["dateReviewed"] = (
'Reviewed date must be a valid date string e.g. "2015-05-20" .'
)
else:
errors["dateReviewed"] = "Reviewed date is required."
if len(errors) == 0:
Expand All @@ -265,9 +265,9 @@ def approval(request):
except Exception as e:
logger.exception("Error processing allocation approval.")
status = "error"
errors[
"message"
] = "An unexpected error occurred. If this problem persists please create a help ticket."
errors["message"] = (
"An unexpected error occurred. If this problem persists please create a help ticket."
)

else:
logger.info("Request data failed validation. %s", list(errors.values()))
Expand Down Expand Up @@ -302,9 +302,9 @@ def contact(request):
except Exception:
logger.exception("Error contacting PI.")
status = "error"
errors[
"message"
] = "An unexpected error occurred. If this problem persists please create a help ticket."
errors["message"] = (
"An unexpected error occurred. If this problem persists please create a help ticket."
)

else:
logger.info("Request data failed validation. %s", list(errors.values()))
Expand Down
8 changes: 2 additions & 6 deletions appliance_catalog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ class ApplianceAdmin(admin.ModelAdmin):
"needs_review",
"project_supported",
)
ordering = [
"-created_date"
]
list_filter = [
"needs_review"
]
ordering = ["-created_date"]
list_filter = ["needs_review"]
actions = [make_reviewed]
form = ApplianceAdminForm

Expand Down
1 change: 1 addition & 0 deletions chameleon/decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""View Decorators for termsandconditions module"""

import urllib.parse
from functools import wraps
from django.conf import settings
Expand Down
Loading

0 comments on commit 55de41b

Please sign in to comment.