def load_locations(file_path, log_to_console=True): if log_to_console: print "loading static locations from %s" % file_path # give django some time to bootstrap itself if not os.path.exists(file_path): raise LoaderException("Invalid file path: %s." % file_path) # create/load static types country_type = LocationType.objects.get_or_create(slug=config.LocationCodes.COUNTRY, name=config.LocationCodes.COUNTRY)[0] district_type = LocationType.objects.get_or_create(slug=config.LocationCodes.DISTRICT, name=config.LocationCodes.DISTRICT)[0] facility_type = LocationType.objects.get_or_create(slug=config.LocationCodes.FACILITY, name=config.LocationCodes.FACILITY)[0] hsa_type = LocationType.objects.get_or_create(slug=config.LocationCodes.HSA, name=config.LocationCodes.HSA)[0] country = Location.objects.get_or_create(name=settings.COUNTRY[0].upper()+settings.COUNTRY[1:], type=country_type, code=settings.COUNTRY)[0] district_sp_type = SupplyPointType.objects.get_or_create(name="district", code=config.SupplyPointCodes.DISTRICT)[0] fac_sp_type = SupplyPointType.objects.get_or_create(name="health facility", code=config.SupplyPointCodes.FACILITY)[0] # we don't use this anywhere in the loader, but make sure to create it hsa_sp_type = SupplyPointType.objects.get_or_create(name="health surveillance assistant", code=config.SupplyPointCodes.HSA)[0] csv_file = open(file_path, 'r') try: count = 0 for line in csv_file: #leave out first line if "district code" in line.lower(): continue district_code, district_name, facility_code, facility_seq, facility_name, hsa_count = line.split(",") #create/load district try: district = Location.objects.get(code=district_code) except Location.DoesNotExist: district = Location.objects.create(name=district_name.strip(), type=district_type, code=district_code, parent=country) # create/load district supply point info dist_sp = supply_point_from_location(district, type=district_sp_type) #create/load location info if not facility_code: facility_code = "temp%s" % count try: fac_loc = Location.objects.get(code=facility_code) except Location.DoesNotExist: fac_loc = Location(code=facility_code) fac_loc.name = facility_name.strip() fac_loc.parent = district fac_loc.type = facility_type fac_loc.save() # create/load supply point info fac_sp = supply_point_from_location(fac_loc, type=fac_sp_type, parent=dist_sp) count += 1 if log_to_console: print "Successfully processed %s locations." % count finally: csv_file.close()
def load_locations(file_path, log_to_console=True): if log_to_console: print "loading static locations from %s" % file_path # give django some time to bootstrap itself if not os.path.exists(file_path): raise LoaderException("Invalid file path: %s." % file_path) # create/load static types country_type = LocationType.objects.get_or_create( slug=config.LocationCodes.COUNTRY, name=config.LocationCodes.COUNTRY)[0] district_type = LocationType.objects.get_or_create( slug=config.LocationCodes.DISTRICT, name=config.LocationCodes.DISTRICT)[0] facility_type = LocationType.objects.get_or_create( slug=config.LocationCodes.FACILITY, name=config.LocationCodes.FACILITY)[0] hsa_type = LocationType.objects.get_or_create( slug=config.LocationCodes.HSA, name=config.LocationCodes.HSA)[0] country = Location.objects.get_or_create(name=settings.COUNTRY[0].upper() + settings.COUNTRY[1:], type=country_type, code=settings.COUNTRY)[0] district_sp_type = SupplyPointType.objects.get_or_create( name="district", code=config.SupplyPointCodes.DISTRICT)[0] fac_sp_type = SupplyPointType.objects.get_or_create( name="health facility", code=config.SupplyPointCodes.FACILITY)[0] # we don't use this anywhere in the loader, but make sure to create it hsa_sp_type = SupplyPointType.objects.get_or_create( name="health surveillance assistant", code=config.SupplyPointCodes.HSA)[0] csv_file = open(file_path, 'r') try: count = 0 for line in csv_file: #leave out first line if "district code" in line.lower(): continue district_code, district_name, facility_code, facility_seq, facility_name, hsa_count = line.split( ",") #create/load district try: district = Location.objects.get(code=district_code) except Location.DoesNotExist: district = Location.objects.create(name=district_name.strip(), type=district_type, code=district_code, parent=country) # create/load district supply point info dist_sp = supply_point_from_location(district, type=district_sp_type) #create/load location info if not facility_code: facility_code = "temp%s" % count try: fac_loc = Location.objects.get(code=facility_code) except Location.DoesNotExist: fac_loc = Location(code=facility_code) fac_loc.name = facility_name.strip() fac_loc.parent = district fac_loc.type = facility_type fac_loc.save() # create/load supply point info fac_sp = supply_point_from_location(fac_loc, type=fac_sp_type, parent=dist_sp) count += 1 if log_to_console: print "Successfully processed %s locations." % count finally: csv_file.close()