Ejemplo n.º 1
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
Ejemplo n.º 2
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