Skip to content

Commit

Permalink
Move mutation to a model method
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchrisadams committed Mar 5, 2024
1 parent be6e071 commit 413809b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 1 addition & 2 deletions apps/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,7 @@ def save_model(self, request, obj, form, change):
# if the provider is being archived, deactivate all networks to avoid having
# to manually do this for each network
if "archived" in form.changed_data and form.instance.archived:
form.instance.deactivate_networks()
form.instance.update(showonwebsite=False)
form.instance.archive()

super().save_model(request, obj, form, change)

Expand Down
10 changes: 8 additions & 2 deletions apps/accounts/models/hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,21 @@ def mark_as_pending_review(self, approval_request):

return False

def deactivate_networks(self):
def archive(self) -> "Hostingprovider":
"""
Deactivate all the IP ranges and ASNs for this provider.
Archive the provider, deactivating all the IP ranges
and ASNs for this provider, removing it from listings
"""
active_green_ips = self.greencheckip_set.filter(active=True)
active_green_asns = self.greencheckasn_set.filter(active=True)
active_green_ips.update(active=False)
active_green_asns.update(active=False)

self.archived = True
self.showonwebsite = False
self.save()
return self

# Queries

def public_supporting_evidence(
Expand Down
8 changes: 6 additions & 2 deletions apps/accounts/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def test_accepted_ways_to_model(

assert datacenter.model == accounting_model

def test_deactivate_networks(
@pytest.mark.only
def test_archive(
self, db, hosting_provider_factory, green_ip_factory, green_asn_factory
):

Expand All @@ -75,14 +76,17 @@ def test_deactivate_networks(
assert ip_range.active is True
assert as_network.active is True

provider.deactivate_networks()
provider.archive()
ip_range.refresh_from_db()
as_network.refresh_from_db()

assert provider.active_ip_ranges().count() == 0
assert provider.active_asns().count() == 0
assert ip_range.active is False
assert as_network.active is False
#
assert provider.archived is True
assert provider.showonwebsite is False


class TestHostingProviderEvidence:
Expand Down

0 comments on commit 413809b

Please sign in to comment.