예제 #1
0
파일: names.py 프로젝트: 3taps/Geodatabase
def edit(request, loc_name_id):
    """ Respond to the "/geo/editor/names/edit/NN" URL.

        We let the user edit the LocationName record with the given ID.
    """
    if not request.user.is_authenticated:
        return HttpResponseRedirect("/geo/editor/")

    try:
        loc_name = LocationName.objects.get(id=loc_name_id)
    except LocationName.DoesNotExist:
        return HttpResponseRedirect("/geo/editor/location/")

    location = loc_name.location

    if request.method == "GET":
        initial_data = {}
        initial_data['name'] = loc_name.name.name

        if loc_name.sourceFilter != None:
            initial_data['source_id']    = loc_name.sourceFilter.id
            initial_data['source_field'] = loc_name.sourceFilter.name
        else:
            initial_data['source_id']    = None
            initial_data['source_field'] = ""

        if location.level.level >= 2:
            if loc_name.countryFilter != None:
                initial_data['country_id']    = loc_name.countryFilter.id
                initial_data['country_field'] = loc_name.countryFilter.name
            else:
                initial_data['country_id']    = None
                initial_data['country_field'] = ""

        if location.level.level >= 3:
            if loc_name.stateFilter != None:
                initial_data['state_id']    = loc_name.stateFilter.id
                initial_data['state_field'] = loc_name.stateFilter.name
            else:
                initial_data['state_id']    = None
                initial_data['state_field'] = ""

        if location.level.level >= 4:
            if loc_name.metroFilter != None:
                initial_data['metro_id']    = loc_name.metroFilter.id
                initial_data['metro_field'] = loc_name.metroFilter.name
            else:
                initial_data['metro_id']    = None
                initial_data['metro_field'] = ""

        if location.level.level >= 5:
            if loc_name.regionFilter != None:
                initial_data['region_id']    = loc_name.regionFilter.id
                initial_data['region_field'] = loc_name.regionFilter.name
            else:
                initial_data['region_id']    = None
                initial_data['region_field'] = ""

        if location.level.level >= 6:
            if loc_name.countyFilter != None:
                initial_data['county_id']    = loc_name.countyFilter.id
                initial_data['county_field'] = loc_name.countyFilter.name
            else:
                initial_data['county_id']    = None
                initial_data['county_field'] = ""

        if location.level.level >= 7:
            if loc_name.cityFilter != None:
                initial_data['city_id']    = loc_name.cityFilter.id
                initial_data['city_field'] = loc_name.cityFilter.name
            else:
                initial_data['city_id']    = None
                initial_data['city_field'] = ""

        form   = EditNameForm(location, initial=initial_data)
        errMsg = None
    elif request.method == "POST":
        if request.POST.get("cancel") == "Cancel":
            return HttpResponseRedirect("/geo/editor/names/" +
                                        location.code + "/")

        form = EditNameForm(location, request.POST)
        errMsg = None # initially.
        if form.is_valid():
            if len(form.cleaned_data['name']) == 0:
                errMsg = "You must enter a name."

            if errMsg == None:
                errMsg = check_validity(form.cleaned_data, location, loc_name)

            if errMsg == None:
                save_name(form.cleaned_data, location, loc_name)
                return HttpResponseRedirect("/geo/editor/names/" +
                                            location.code + "/")

    # If we get here, display our form.

    extra_head_tags = []
    extra_head_tags.append('<style type="text/css">')
    extra_head_tags.append('  th {')
    extra_head_tags.append('    text-align:right;')
    extra_head_tags.append('  }')
    extra_head_tags.append('</style>')
    extra_head_tags.append('<script type="text/javascript" ' +
                           'src="/geo/media/jquery-1.2.6.min.js"></script>')
    extra_head_tags.append('<script type="text/javascript" ' +
                           'src="/geo/media/jquery.autocomplete.js"></script>')
    extra_head_tags.append('<link href="/geo/media/jquery.autocomplete.css" ' +
                           'rel="stylesheet" type="text/css">')
    extra_head_tags.append('<script type="text/javascript" ' +
                           'src="/geo/media/namelookups.js"></script>')

    return render_to_response("editor/templates/editForm.html",
                              {'title'           : "Location Editor",
                               'heading'         : "Edit Name for " +
                                                   str(location),
                               'action'          : "/geo/editor/names/edit/"+
                                                   str(loc_name.id) + "/",
                               'errMsg'          : errMsg,
                               'form'            : form,
                               'extra_head_tags' : extra_head_tags,
                              },
                              context_instance=RequestContext(request))
예제 #2
0
파일: names.py 프로젝트: 3taps/Geodatabase
def add(request, loc_code):
    """ Respond to the "/geo/editor/names/add/XYZ" URL.

        We let the user add a new LocationName for the given location.
    """
    if not request.user.is_authenticated:
        return HttpResponseRedirect("/geo/editor/")

    try:
        location = Location.objects.get(code=loc_code)
    except Location.DoesNotExist:
        return HttpResponseRedirect("/geo/editor/location/")

    if request.method == "GET":
        form   = EditNameForm(location)
        errMsg = None
    elif request.method == "POST":
        if request.POST.get("cancel") == "Cancel":
            return HttpResponseRedirect("/geo/editor/names/" + loc_code + "/")

        form = EditNameForm(location, request.POST)
        errMsg = None # initially.
        if form.is_valid():
            if len(form.cleaned_data['name']) == 0:
                errMsg = "You must enter a name."

            if errMsg == None:
                errMsg = check_validity(form.cleaned_data, location, None)

            if errMsg == None:
                save_name(form.cleaned_data, location, None)
                return HttpResponseRedirect("/geo/editor/names/"+loc_code+"/")

    # If we get here, display our form.

    extra_head_tags = []
    extra_head_tags.append('<style type="text/css">')
    extra_head_tags.append('  th {')
    extra_head_tags.append('    text-align:right;')
    extra_head_tags.append('  }')
    extra_head_tags.append('</style>')
    extra_head_tags.append('<script type="text/javascript" ' +
                           'src="/geo/media/jquery-1.2.6.min.js"></script>')
    extra_head_tags.append('<script type="text/javascript" ' +
                           'src="/geo/media/jquery.autocomplete.js"></script>')
    extra_head_tags.append('<link href="/geo/media/jquery.autocomplete.css" ' +
                           'rel="stylesheet" type="text/css">')
    extra_head_tags.append('<script type="text/javascript" ' +
                           'src="/geo/media/namelookups.js"></script>')

    return render_to_response("editor/templates/editForm.html",
                              {'title'           : "Location Editor",
                               'heading'         : "Add Name for " +
                                                   str(location),
                               'action'          : "/geo/editor/names/add/"+
                                                   loc_code + "/",
                               'errMsg'          : errMsg,
                               'form'            : form,
                               'extra_head_tags' : extra_head_tags,
                              },
                              context_instance=RequestContext(request))