def handle(self, **options):
        path = options["path"]
        csv_rows = csv.reader(open(path,'rU'), delimiter=",")
        rnum = 0
        ug=Location.tree.root_nodes()[0]
        d_type=LocationType.objects.get(name="district")
        v_type=LocationType.objects.get(name="village")
        s_type=LocationType.objects.get(name="sub_county")
        for row in csv_rows:

            
            district_s,county_s,subcounty_s,parish_s,village_s = tuple(row)

            
            
            district=None
            subcounty=None
            village=None


            try:
                
                district=Location.objects.get(type=d_type,name=district_s.strip().lower().title())

            except Location.DoesNotExist:
                tocreate_district=Location(type=d_type,name=district_s.strip().lower().title())
                district=Location.tree.insert_node(tocreate_district, ug)
                district.save()
                print district_s
            except MultipleObjectsReturned:
                district=Location.objects.filter(type=d_type,name=district_s.strip().lower().title())[0]
                print "double" +district_s

            try:
                subcounty=district.children.get(type=s_type,name=subcounty_s.strip().lower().title())
            
            except Location.DoesNotExist:
                tocreate_subcounty=Location(type=s_type,name=subcounty_s.strip().lower().title())
                subcounty=Location.tree.insert_node(tocreate_subcounty, district)
                subcounty.parent=district
                subcounty.save()
                print subcounty_s

            try:
                village=district.children.get(type=v_type,name=village_s.strip().lower().title())
                if not village.get_ancestors():
                    #village.parent=None
                    #village.save()

                    print village_s
                    vill=Location.objects.get(pk=village.pk)
                    vill.move_to(subcounty, 'first-child')
                    vill.save()
            except Location.DoesNotExist:
                tocreate_village=Location(type=v_type,name=village_s.strip().lower().title())
                village=Location.tree.insert_node(tocreate_village, subcounty)
                village.parent=subcounty
                village.save()
                print village_s
Example #2
0
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()
Example #3
0
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
Example #4
0
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