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

        We let the user edit the LocationName record with the given ID.
    """
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse(settings.ADMIN_HOME_VIEW))

    try:
        loc_name = LocationName.objects.get(id=loc_name_id)
    except LocationName.DoesNotExist:
        return HttpResponseRedirect(reverse(settings.ADMIN_HOME_VIEW))

    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(
                        reverse("location_editor.views.names",
                                args=[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(
                            reverse("location_editor.views.names",
                                    args=[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="'+settings.MEDIA_URL+'jquery-1.2.6.min.js">' +
                           '</script>')
    extra_head_tags.append('<script type="text/javascript" ' +
                           'src="'+settings.MEDIA_URL+'jquery.autocomplete.js">'+
                           '</script>')
    extra_head_tags.append('<link href="' +
                           settings.MEDIA_URL + 'jquery.autocomplete.css" ' +
                           'rel="stylesheet" type="text/css">')
    extra_head_tags.append('<script type="text/javascript" ' +
                           'src="'+settings.MEDIA_URL+'namelookups.js"></script>')

    url = reverse("location_editor.views.edit_name", args=[loc_name_id])

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

        We let the user add a new LocationName for the given location.
    """
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse(settings.ADMIN_HOME_VIEW))

    try:
        location = Location.objects.get(code=loc_code)
    except Location.DoesNotExist:
        return HttpResponseRedirect(reverse(settings.ADMIN_HOME_VIEW))

    if request.method == "GET":
        form   = EditNameForm(location)
        errMsg = None
    elif request.method == "POST":
        if request.POST.get("cancel") == "Cancel":
            return HttpResponseRedirect(
                        reverse("location_editor.views.names",
                                args=[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(
                            reverse("location_editor.views.names",
                                    args=[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="'+settings.MEDIA_URL+'jquery-1.2.6.min.js">' +
                           '</script>')
    extra_head_tags.append('<script type="text/javascript" ' +
                           'src="'+settings.MEDIA_URL+'jquery.autocomplete.js">'+
                           '</script>')
    extra_head_tags.append('<link href="' +
                           settings.MEDIA_URL+'jquery.autocomplete.css" ' +
                           'rel="stylesheet" type="text/css">')
    extra_head_tags.append('<script type="text/javascript" ' +
                           'src="'+settings.MEDIA_URL+'namelookups.js"></script>')

    url = reverse("location_editor.views.add_name", args=[loc_code])

    return render_to_response("shared/templates/editForm.html",
                              {'title'           : "Whenua Admin",
                               'heading'         : "Add Name for " +
                                                   str(location),
                               'action'          : url,
                               'errMsg'          : errMsg,
                               'form'            : form,
                               'extra_head_tags' : extra_head_tags,
                              },
                              context_instance=RequestContext(request))