def get_record(self, identifier): # use code to find a matching OrganisationGeography # or OrganisationDivision record _, code = split_code(identifier) try: return OrganisationGeography.objects.all().get(gss=code) except OrganisationGeography.DoesNotExist: return OrganisationDivision.objects.all().get( official_identifier=identifier)
def validate_identifier(self, identifier): # throw an exception if we're going to have a bad time with this id # return True if it looks good code_type, code = split_code(identifier) if code_type == "unit_id" and re.match(r"^\d+$", code): # FIXME before 2021 error = ( "Importing boundaries from BoundaryLine against CEDs is " "not yet available because unit_id is not a stable identifier") raise ValueError(error) if code_type == "gss" and re.match(r"^[A-Z][0-9]{8}$", code): return True raise ValueError( "Unknown code type. Expected 'gss:X01000001' or 'unit_id:12345'")
def import_div_geography(self, div): area_type = div.division_type bl = self.open_boundaryline(area_type) code_type, code = split_code(div.official_identifier) fieldname = "code" if code_type == "gss" else code_type geom = self.get_geography_from_feature(bl.get_feature_by_field(fieldname, code)) try: div.geography.geography = geom.ewkb div.geography.source = self.source div.geography.save() except DivisionGeography.DoesNotExist: dg = DivisionGeography( division_id=div.id, geography=geom.ewkb, source=self.source ) dg.save() self.stdout.write("..saved {}".format(str(div)))
def filter_records(self, identifier): # use code to find matching OrganisationGeography # or OrganisationDivision records _, code = split_code(identifier) orgs = OrganisationGeography.objects.all().filter(gss=code) if orgs.exists(): return orgs divs = OrganisationDivision.objects.all().filter( official_identifier=identifier) if divs.exists(): return divs raise ObjectDoesNotExist( ("Couldn't find any OrganisationGeography or OrganisationDivision " "objects matching {}".format(identifier)))