-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CAPE] make link_combined_authorities more robust if CA missing
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
Showing
1 changed file
with
29 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |