Skip to content

Commit

Permalink
[CAPE] make link_combined_authorities more robust if CA missing
Browse files Browse the repository at this point in the history
Rather than falling over spit out an error message indicating which
authority is missing to make it easier to fix

Related to #719
  • Loading branch information
struan committed Feb 6, 2025
1 parent 4848865 commit 3e6b64c
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions caps/management/commands/link_combined_authorities.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
from caps.models import Council
from django.core.management.base import BaseCommand, CommandError

from caps.import_utils import get_council_df


def link_combined_authorities():
df = get_council_df()

# limit just to those that have 'combined-authority'
df = df[~df["combined-authority"].isna()]

# iterate and link
for _, row in df.iterrows():
council = Council.objects.get(authority_code=row["local-authority-code"])
combined_authority = Council.objects.get(
authority_code=row["combined-authority"]
)
council.combined_authority = combined_authority
council.save()
from caps.models import Council


class Command(BaseCommand):
help = "Adds authority codes to the csv of plans"

def link_combined_authorities(self):
df = get_council_df()

# limit just to those that have 'combined-authority'
df = df[~df["combined-authority"].isna()]

# iterate and link
for _, row in df.iterrows():
try:
council = Council.objects.get(
authority_code=row["local-authority-code"]
)
except Council.DoesNotExist:
self.stderr.write(f"Can't find council {row['nice-name']}")
try:
combined_authority = Council.objects.get(
authority_code=row["combined-authority"]
)
except Council.DoesNotExist:
self.stderr.write(
f"Can't find combined authority {row['combined-authority']} for {row['nice-name']}"
)
continue

council.combined_authority = combined_authority
council.save()

def handle(self, *args, **options):
print("linking combined authorities")
link_combined_authorities()
self.link_combined_authorities()

0 comments on commit 3e6b64c

Please sign in to comment.