コード例 #1
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.objects.create(name=name,code=code,parent=target)
            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()
            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))
コード例 #2
0
ファイル: views.py プロジェクト: nyaruka/simple_locations
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)
    )