diff --git a/every_election/apps/organisations/boundaries/boundaryline.py b/every_election/apps/organisations/boundaries/boundaryline.py index b5a3cafc0..1827e8030 100644 --- a/every_election/apps/organisations/boundaries/boundaryline.py +++ b/every_election/apps/organisations/boundaries/boundaryline.py @@ -1,3 +1,5 @@ +import sys + from django.contrib.gis.gdal import DataSource, OGRGeometry from django.contrib.gis.geos import MultiPolygon from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist @@ -13,11 +15,12 @@ class BoundaryLine: - def __init__(self, filename): + def __init__(self, filename, show_picker=False): ds = DataSource(filename) if len(ds) != 1: raise ValueError("Expected 1 layer, found %i" % (len(ds))) self.layer = ds[0] + self.show_picker = show_picker def merge_features(self, features): polygons = [] @@ -166,6 +169,16 @@ def get_division_code(self, div, org): warning = self.get_match_warning(div, matches[0]) if warning: + if self.show_picker: + sys.stdout.write( + f"""{warning} + Do you want to match {matches[0].get('name')} ({matches[0].get('code')}) to {div.official_identifier}? + """ + ) + choice = input("enter 'y' to accept, or 'n' not to: ") + if choice.lower() in ("y", "yes"): + return self.get_code_from_feature(matches[0]) + raise ObjectDoesNotExist(warning) return self.get_code_from_feature(matches[0]) diff --git a/every_election/apps/organisations/boundaries/management/commands/boundaryline_backport_codes.py b/every_election/apps/organisations/boundaries/management/commands/boundaryline_backport_codes.py index 3657b9a93..5b15419f1 100644 --- a/every_election/apps/organisations/boundaries/management/commands/boundaryline_backport_codes.py +++ b/every_election/apps/organisations/boundaries/management/commands/boundaryline_backport_codes.py @@ -60,6 +60,12 @@ def check_valid_date(value): dest="dry-run", help="Don't commit changes", ) + parser.add_argument( + "--show-picker", + action="store_true", + dest="show-picker", + help="Show picker when requiring manual review", + ) super().add_arguments(parser) def get_divisions(self, types, date): @@ -130,7 +136,10 @@ def handle(self, *args, **options): self.stdout.write("Searching...") lookup = get_area_type_lookup(filter=lambda x: x in self.WARD_TYPES, group=True) for org_type, filename in lookup.items(): - bl = BoundaryLine(os.path.join(base_dir, "Data", "GB", filename)) + bl = BoundaryLine( + os.path.join(base_dir, "Data", "GB", filename), + show_picker=options["show-picker"], + ) divs = self.get_divisions(org_type, options["date"]) for div in divs: org = self.get_parent_org_boundary(div)