Avoid loading multiple times the same data

There was a bug in `data.py` which made it load the same data as many
times as the number of postal codes in constraints. This is now fixed.
This commit is contained in:
Lucas Verney 2017-06-19 12:01:10 +02:00
parent d396d33506
commit b187a106e4

View File

@ -83,8 +83,13 @@ def load_data(model, constraint, config):
get_session = database.init_db(config["database"], config["search_index"]) get_session = database.init_db(config["database"], config["search_index"])
results = [] results = []
with get_session() as session: with get_session() as session:
areas = []
# Get areas to fetch from, using postal codes
for postal_code in constraint["postal_codes"]: for postal_code in constraint["postal_codes"]:
area = data_files.french_postal_codes_to_iso_3166(postal_code) areas.append(data_files.french_postal_codes_to_iso_3166(postal_code))
# Load data for each area
areas = list(set(areas))
for area in areas:
results.extend( results.extend(
session.query(model) session.query(model)
.filter(model.area == area).all() .filter(model.area == area).all()