コード例 #1
0
ファイル: loader.py プロジェクト: dslowikowski/logistics
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
コード例 #2
0
ファイル: loader.py プロジェクト: andile2012/logistics
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
コード例 #3
0
ファイル: import_locations.py プロジェクト: AndreLesa/mwana
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
コード例 #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