def test_slugify(self): self.assertEqual( Slugger.slugify( " Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/" ), "jack-jill-like-numbers-1-2-3-and-4-and-silly-characters-", )
def pre_import(self): filename = self.base_url % (self.council_id, "stations", "json") with tempfile.NamedTemporaryFile() as tmp: urllib.request.urlretrieve(filename, tmp.name) stations = self.get_data("json", tmp.name) for station in stations: self.station_points[Slugger.slugify( station["place"])] = station
def _mk_place_id(self, record): """ This data doesn't have IDs in, so we'll make them :/ """ return Slugger.slugify(" ".join([ record.polling_place_name, record.polling_place_address_1, ]))
def get_slug(self, address_info): self.logger.log_message(logging.DEBUG, "Generating custom slug") return Slugger.slugify("%s-%s-%s-%s" % ( self.council.pk, address_info["polling_station_id"], address_info["address"], address_info["postcode"], ))
def district_record_to_dict(self, record): if record[2] == self.council_name: station_slug = Slugger.slugify(record[3]) if station_slug == "thornhill-village-hall": station_slug = "thornhill-community-hall" elif station_slug == "john-mclintock-hall": station_slug = "mclintock-hall" self.station_map.setdefault(station_slug, []).append(record[0]) return super().district_record_to_dict(record)
def district_record_to_dict(self, record): if record[2] == self.council_name: station_slug = Slugger.slugify(record[3]) if station_slug == "whitburn-bowling-club": station_slug = "whitburn-bowling-club-club" elif station_slug == "livingston-village-primary-school": station_slug = "livingston-village-primary" elif station_slug == "uphall-station-institute-hall": station_slug = "uphall-station-institue-hall" self.station_map.setdefault(station_slug, []).append(record[0]) return super().district_record_to_dict(record)
def get_slug(self, address_info): # if we have a uprn, use that as the slug if 'uprn' in address_info: if address_info['uprn']: self.logger.log_message(logging.DEBUG, "Using UPRN as slug") return address_info['uprn'] # otherwise build a slug from the other data we have self.logger.log_message(logging.DEBUG, "Generating custom slug") return Slugger.slugify( "%s-%s-%s-%s" % (self.council.pk, address_info['polling_station_id'], address_info['address'], address_info['postcode']))
def get_address_lookup(self, addresses): # for each address, build a lookup of address -> list of station ids address_lookup = {} for i, record in enumerate(addresses): address_slug = Slugger.slugify( "-".join([record["address"], record["postcode"]]) ) addresses[i]["address_slug"] = address_slug if address_slug in address_lookup: address_lookup[address_slug].append(record["polling_station_id"]) else: address_lookup[address_slug] = [record["polling_station_id"]] return address_lookup
def get_address_lookup(self): # for each address, build a lookup of address -> set of station ids address_lookup = {} for record in self.elements: address_slug = Slugger.slugify("-".join( [record["address"], record["postcode"]])) record["address_slug"] = address_slug if address_slug in address_lookup: address_lookup[address_slug].add(record["polling_station_id"]) else: address_lookup[address_slug] = set( [record["polling_station_id"]]) return address_lookup
def build_address_lookup(self, addresses): # for each address, build a lookup of address -> list of station ids address_lookup = {} for i in range(0, len(addresses)): record = addresses[i] address_slug = Slugger.slugify("-".join( [record['address'], record['postcode']])) addresses[i]['address_slug'] = address_slug if address_slug in address_lookup: address_lookup[address_slug].append( record['polling_station_id']) else: address_lookup[address_slug] = [record['polling_station_id']] return address_lookup
def build_address_lookup(self, addresses): # for each address, build a lookup of address -> list of station ids address_lookup = {} for i in range(0, len(addresses)): record = addresses[i] address_slug = Slugger.slugify( "-".join([record['address'], record['postcode']])) addresses[i]['address_slug'] = address_slug if address_slug in address_lookup: address_lookup[address_slug].append( record['polling_station_id']) else: address_lookup[address_slug] = [record['polling_station_id']] return address_lookup
def station_record_to_dict(self, record): if not record["place"]: return None slug = Slugger.slugify(record["place"]) if slug in self.station_points: location = self.extract_geometry(self.station_points[slug], self.geom_type, self.get_srid()) else: location = None return { "internal_council_id": record["polling"], "address": record["place"], "postcode": record["postcode"], "location": location, }
def station_record_to_dict(self, record): if record[2] == self.council_name: try: station_slug = Slugger.slugify(record[3]) codes = self.station_map[station_slug] except KeyError: return None stations = [] for code in codes: rec = { "internal_council_id": code, "postcode": "", "address": record[3], } stations.append(rec) return stations
def get_slug(self, address_info): # if we have a uprn, use that as the slug if 'uprn' in address_info: if address_info['uprn']: self.logger.log_message(logging.DEBUG, "Using UPRN as slug") return address_info['uprn'] # otherwise build a slug from the other data we have self.logger.log_message(logging.DEBUG, "Generating custom slug") return Slugger.slugify( "%s-%s-%s-%s" % ( self.council.pk, address_info['polling_station_id'], address_info['address'], address_info['postcode'] ) )
def test_slugify_addresses(self): slug1 = Slugger.slugify("5-6 Mickleton Dr, Southport") slug2 = Slugger.slugify("5/6, Mickleton Dr. Southport") slug3 = Slugger.slugify("5-6 mickleton dr southport") slug4 = Slugger.slugify("56 Mickleton Dr, Southport") self.assertTrue((slug1 == slug2 == slug3) != slug4)
def district_record_to_dict(self, record): if record[2] == self.council_name: station_slug = Slugger.slugify(record[3]) self.station_map.setdefault(station_slug, []).append(record[0]) return super().district_record_to_dict(record)
def test_slugify_addresses(self): slug1 = Slugger.slugify('5-6 Mickleton Dr, Southport') slug2 = Slugger.slugify('5/6, Mickleton Dr. Southport') slug3 = Slugger.slugify('5-6 mickleton dr southport') slug4 = Slugger.slugify('56 Mickleton Dr, Southport') self.assertTrue((slug1 == slug2 == slug3) != slug4)
def test_non_string_input(self): self.assertEqual(Slugger.slugify(123), '123')
def test_unicode(self): self.assertEqual( Slugger.slugify("Un \xe9l\xe9phant \xe0 l'or\xe9e du bois"), 'un-elephant-a-l-oree-du-bois', )
def test_unicode(self): self.assertEqual( Slugger.slugify("Un \xe9l\xe9phant \xe0 l'or\xe9e du bois"), "un-elephant-a-l-oree-du-bois", )
def test_non_string_input(self): self.assertEqual(Slugger.slugify(123), "123")
def test_slugify(self): self.assertEqual( Slugger.slugify(' Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/'), 'jack-jill-like-numbers-1-2-3-and-4-and-silly-characters-', )