Beispiel #1
0
def csv2areas(csv_file='areas.csv', \
              create_kinds=True, \
              skip_first=False, \
              prefix=None):

    ''' Creates Area objects from a CSV file.

    FORMAT:

    NAME | TYPE_SLUG | PARENT_ID '''

    from simple_locations.models import Area, AreaType

    try:
        fhandler = open(csv_file)
    except IOError:
        print "Unable to open file %s" % csv_file
        return None

    for line in fhandler:

        # skip header line?
        if skip_first:
            skip_first = False
            continue

        # retrieve raw data
        data = line.strip().split(',')
        name = data[0]
        type_slug = data[1]
        parent_id = data[2] or None
        parent = None
        try:
            name = name.decode('utf-8')
        except:
            pass

        # find or create AreaType
        try:
            kind = AreaType.objects.get(slug=type_slug)
        except AreaType.DoesNotExist:
            if create_kinds:
                kind = AreaType(slug=type_slug, name=type_slug.title())
                kind.save()
            else:
                raise

        # find parent
        if parent_id:
            parent = Area.objects.get(id=int(parent_id))

        print u"N: %(name)s - T: %(type)s - TS: %(type_slug)s - " \
               "P: %(parent)s - PID: %(parent_id)s" % \
               {'name': name, 'type': kind, 'type_slug': type_slug, \
                'parent': parent, 'parent_id': parent_id}

        area = Area(name=name, kind=kind, parent=parent)
        area.save()
Beispiel #2
0
def add_location(req, parent_id=None):
    nodes = Area.tree.all()

    if req.method == "POST":
        form = LocationForm(req.POST)
        if form.is_valid():
            name = form.cleaned_data["name"]
            code = form.cleaned_data["code"]
            lat = form.cleaned_data["lat"]
            lon = form.cleaned_data["lon"]
            target = form.cleaned_data["target"]
            position = form.cleaned_data["position"]
            kind = form.cleaned_data["kind"]

            area = Area(name=name, code=code)
            if lat and lon:
                location = Point(latitude=lat, longitude=lon)
                location.save()
                area.location = location
            try:
                kind = get_object_or_404(AreaType, pk=int(kind))
                area.kind = kind
            except ValueError:
                pass
            area.save()
            if form.cleaned_data["move_choice"]:
                try:
                    Area.tree.move_node(area, target, position)
                except InvalidMove:
                    pass
            form = LocationForm()

            return render_to_response(
                "simple_locations/location_edit.html",
                {"form": form, "nodes": nodes},
                context_instance=RequestContext(req),
            )
        else:
            form = LocationForm(req.POST)
            return render_to_response(
                "simple_locations/location_edit.html",
                {"form": form, "nodes": nodes},
                context_instance=RequestContext(req),
            )

    else:
        if parent_id:
            default_data = {}
            parent = get_object_or_404(Area, pk=parent_id)
            default_data["move_choice"] = True
            default_data["target"] = parent.pk
            default_data["position"] = "last-child"
            form = LocationForm(default_data)
            form._errors = ""

        else:
            form = LocationForm()

    return render_to_response(
        "simple_locations/location_edit.html", {"form": form, "nodes": nodes}, context_instance=RequestContext(req)
    )