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(path): info("Loading locations %s" % (path)) if not os.path.exists(path): raise Exception("no file found at %s" % path) count = 0 with open(path, 'r') as f: reader = csv.reader(f, delimiter=',', quotechar='"') for row in reader: id, name, is_active, msd_code, parent_name, parent_type, lat, lon, group, type = row # for now assumes these are already create loc_type = LocationType.objects.get(name__iexact=type) parent = Location.objects.get(name__iexact=parent_name, type__name__iexact=parent_type) \ if parent_name and parent_type else None if lat and lon: if Point.objects.filter(longitude=lon, latitude=lat).exists(): point = Point.objects.filter(longitude=lon, latitude=lat)[0] else: point = Point.objects.create(longitude=lon, latitude=lat) else: point = None code = msd_code if msd_code else _get_code(type, name) try: l = Location.objects.get(code=code) except Location.DoesNotExist: l = Location(code=code) l.name = name l.type = loc_type l.is_active = string_to_boolean(is_active) if parent: l.parent = parent if point: l.point = point l.save() sp = supply_point_from_location\ (l, SupplyPointType.objects.get(name__iexact=type), SupplyPoint.objects.get(location=parent) if parent else None) if group: group_obj = SupplyPointGroup.objects.get_or_create( code=group)[0] sp.groups.add(group_obj) sp.save() count += 1 print "Processed %d locations" % count
def load_locations(path): info("Loading locations %s" % (path)) if not os.path.exists(path): raise Exception("no file found at %s" % path) count = 0 with open(path, 'r') as f: reader = csv.reader(f, delimiter=',', quotechar='"') for row in reader: id, name, is_active, msd_code, parent_name, parent_type, lat, lon, group, type = row # for now assumes these are already create loc_type = LocationType.objects.get(name__iexact=type) parent = Location.objects.get(name__iexact=parent_name, type__name__iexact=parent_type) \ if parent_name and parent_type else None if lat and lon: if Point.objects.filter(longitude=lon, latitude=lat).exists(): point = Point.objects.filter(longitude=lon, latitude=lat)[0] else: point = Point.objects.create(longitude=lon, latitude=lat) else: point = None code = msd_code if msd_code else _get_code(type, name) try: l = Location.objects.get(code=code) except Location.DoesNotExist: l = Location(code=code) l.name = name l.type = loc_type l.is_active = string_to_boolean(is_active) if parent: l.parent = parent if point: l.point = point l.save() sp = supply_point_from_location\ (l, SupplyPointType.objects.get(name__iexact=type), SupplyPoint.objects.get(location=parent) if parent else None) if group: group_obj = SupplyPointGroup.objects.get_or_create(code=group)[0] sp.groups.add(group_obj) sp.save() count += 1 print "Processed %d locations" % count
class Command(BaseCommand): help = "Create regions and assign districts accordingly" def handle(self, *args, **options): if len(args) != 1: print "Please specify the csv file from where to load district-region mappings" print "It should be of the format 'district, region', with one tuple per line" return try: country = LocationType.objects.get(name='country') except LocationType.DoesNotExist: country = LocationType(name='country', slug='country') country.save() try: district = LocationType.objects.get(name='district') except LocationType.DoesNotExist: district = LocationType(name='district', slug='district') district.save() try: self.country = Location.objects.get(type__name='country') except Location.MultipleObjectsReturned: print "There should only be one 'country' specified." exit() except Location.DoesNotExist: self.country = Location(type=country, name='Uganda', code='uganda') self.country.save() self.regions = self.create_regions() self.assign_districts(args[0]) def create_regions(self): region_type, created = LocationType.objects.get_or_create(name='region', slug='region') regions = {} for region in region_names: code = region.lower() reg, created = Location.objects.get_or_create(code=code) if not created: print 'Modifying location %s %s to be children of "uganda"' % (reg.name, reg.pk) reg.name = region reg.type = region_type reg.save() # TODO: is this right??? reg.move_to(self.country, 'last-child') reg.save() regions[code] = reg # verify that new regions have been added to the country country = Location.objects.get(pk=self.country.pk) country_children_pks = [c.pk for c in country.get_children()] for x in regions: assert regions[x].pk in country_children_pks return regions def assign_districts(self, filename): district_type = LocationType.objects.get(slug='district') reader = csv.reader(open(filename, 'rb'), delimiter=',', quotechar='"') line_number = -1 error_count = 0 districts_assigned = 0 for row in reader: line_number = line_number + 1 region_code = row[1].lower() if region_code not in self.regions: print " ERROR: region code %s on line %s not recognized" % (region_code, line_number) error_count = error_count + 1 continue district_code = row[0].lower() try: district = Location.objects.get(name__iexact=district_code, type=district_type) except Location.DoesNotExist: print " ERROR: district code %s on line %s not recognized" % (district_code, line_number) error_count = error_count + 1 continue if district.tree_parent != self.country: print " District %s is already set to have %s as its parent" % (district_code, self.country) continue # TODO: is this right??? region = Location.objects.get(pk=self.regions[region_code].pk) region_children_count = region.get_children().count() district.move_to(region, 'last-child') district.save() # verify that new districts have been added to the region region = Location.objects.get(pk=self.regions[region_code].pk) new_region_children_count = region.get_children().count() assert new_region_children_count == region_children_count + 1 assert district.get_ancestors(ascending=True)[0] == region print "%s districts assigned." % districts_assigned print "%s lines had errors." % error_count
def load_locations(file_path): # give django some time to bootstrap itself from rapidsms.contrib.locations.models import LocationType, Location, Point if not os.path.exists(file_path): raise CommandError("Invalid file path: %s." % file_path) try: province_type = LocationType.objects.get(slug="provinces") except LocationType.DoesNotExist: province_type = LocationType.objects.create\ (slug="provinces", singular="Province", plural="Provinces") try: district_type = LocationType.objects.get(slug="districts") except LocationType.DoesNotExist: district_type = LocationType.objects.create\ (slug="districts", singular="district", plural="districts") csv_file = open(file_path, 'r') count = 0 for line in csv_file: #leave out first line if "latitude" in line.lower(): continue province_name, district_name, facility_name, code, facility_type, latitude, longitude = line.split(",") #create/load province try: province = Location.objects.get(name=province_name, type=province_type) except Location.DoesNotExist: province = Location.objects.create(name=province_name, type=province_type, slug=clean(province_name)) #create/load district try: district = Location.objects.get(name=district_name, type=district_type) except Location.DoesNotExist: district = Location.objects.create(name=district_name, type=district_type, slug=clean(district_name), parent=province) #create/load facility type try: facility_type = facility_type.strip() type = LocationType.objects.get(slug=clean(facility_type), singular=facility_type) except LocationType.DoesNotExist: type = LocationType.objects.create(slug=clean(facility_type), singular=facility_type, plural=facility_type + "s") #create/load facility try: facility = Location.objects.get(slug=code) except Location.DoesNotExist: facility = Location(slug=code) facility.name = facility_name facility.parent = district facility.point = Point.objects.get_or_create(latitude=latitude, longitude=longitude)[0] facility.type = type facility.save() count += 1 print "Successfully processed %s locations." % count
def load_locations(file_path): # give django some time to bootstrap itself from rapidsms.contrib.locations.models import LocationType, Location, Point if not os.path.exists(file_path): raise CommandError("Invalid file path: %s." % file_path) try: province_type = LocationType.objects.get(slug="provinces") except LocationType.DoesNotExist: province_type = LocationType.objects.create\ (slug="provinces", singular="Province", plural="Provinces") try: district_type = LocationType.objects.get(slug="districts") except LocationType.DoesNotExist: district_type = LocationType.objects.create\ (slug="districts", singular="district", plural="districts") csv_file = open(file_path, 'r') count = 0 for line in csv_file: #leave out first line if "latitude" in line.lower(): continue province_name, district_name, facility_name, code, facility_type, latitude, longitude = line.split( ",") #create/load province try: province = Location.objects.get(name=province_name, type=province_type) except Location.DoesNotExist: province = Location.objects.create(name=province_name, type=province_type, slug=clean(province_name)) #create/load district try: district = Location.objects.get(name=district_name, type=district_type) except Location.DoesNotExist: district = Location.objects.create(name=district_name, type=district_type, slug=clean(district_name), parent=province) #create/load facility type try: facility_type = facility_type.strip() type = LocationType.objects.get(slug=clean(facility_type), singular=facility_type) except LocationType.DoesNotExist: type = LocationType.objects.create(slug=clean(facility_type), singular=facility_type, plural=facility_type + "s") #create/load facility try: facility = Location.objects.get(slug=code) except Location.DoesNotExist: facility = Location(slug=code) facility.name = facility_name facility.parent = district facility.point = Point.objects.get_or_create(latitude=latitude, longitude=longitude)[0] facility.type = type facility.save() count += 1 print "Successfully processed %s locations." % count