diff --git a/caps/management/commands/link_combined_authorities.py b/caps/management/commands/link_combined_authorities.py index 96c806cba..63bd92bda 100644 --- a/caps/management/commands/link_combined_authorities.py +++ b/caps/management/commands/link_combined_authorities.py @@ -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()