Beispiel #1
0
def save_name(data, location, loc_name):
    """ Save the given location name into the database.

        The parameters are as follows:

            'data'

                The entered form data, as a dictionary.

            'location'

                The Location object we are entering a name for.

            'loc_name'

                The existing LocationName record we are editing, if any.  If we
                are adding a new name, this should be set to None.

        We save the given name into the database, either updating 'loc_name' or
        creating a new LocationName record as appropriate.
    """
    # Get the Name object to use for this location name.  If necessary, we
    # create a new Name object for this name.

    name_value = helpers.tidy_name(data['name'].upper())

    try:
        name = Name.objects.get(level=location.level, name=name_value)
    except Name.DoesNotExist:
        name = Name()
        name.level = location.level
        name.name  = name_value
        name.save()

    # If necessary, create a new LocationName object for this name.

    if loc_name == None:
        loc_name = LocationName()

    # Save the entered data into this location name.

    loc_name.name         = name
    loc_name.location     = location
    loc_name.sourceFilter = getSource(data['source_id'])

    if location.level.level >= 2:
        if data['country_field'] != "":
            loc_name.countryFilter = getLocation(data['country_id'])
        else:
            loc_name.countryFilter = None
    else:
        loc_name.countryFilter = None

    if location.level.level >= 3:
        if data['state_field'] != "":
            loc_name.stateFilter = getLocation(data['state_id'])
        else:
            loc_name.stateFilter = None
    else:
        loc_name.stateFilter = None

    if location.level.level >= 4:
        if data['metro_field'] != "":
            loc_name.metroFilter = getLocation(data['metro_id'])
        else:
            loc_name.metroFilter = None
    else:
        loc_name.metroFilter = None

    if location.level.level >= 5:
        if data['region_field'] != "":
            loc_name.regionFilter = getLocation(data['region_id'])
        else:
            loc_name.regionFilter = None
    else:
        loc_name.regionFilter = None

    if location.level.level >= 6:
        if data['county_field'] != "":
            loc_name.countyFilter = getLocation(data['county_id'])
        else:
            loc_name.countyFilter = None
    else:
        loc_name.countyFilter = None

    if location.level.level >= 7:
        if data['city_field'] != "":
            loc_name.cityFilter = getLocation(data['city_id'])
        else:
            loc_name.cityFilter = None
    else:
        loc_name.cityFilter = None

    loc_name.save()

    # Finally, send signals telling the rest of the system that the Name and
    # LocationName tables have been chagned.

    name_changed.send(sender=None)
    location_name_changed.send(sender=None)
Beispiel #2
0
def main(request, loc_code):
    """ Respond to the main "/geo/editor/names/XYZ/" URL.

        We let the user edit the names 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/")

    loc_names = []
    for loc_name in LocationName.objects.filter(location=location):
        loc_names.append(loc_name)

    imports = []
    imports.append('<link href="/geo/media/editor.css" ' +
                         'rel="stylesheet" type="text/css">')

    if request.method == "GET":

        # We're displaying the form for the first time.  Process our CGI
        # parameters (if any).

        confirm = request.GET.get("confirm")

    elif request.method == "POST":

        # Respond to the user clicking on one of our buttons.

        # Did the user click on the "Finished" button?

        if request.POST.get("finished") == "Finished":
            return HttpResponseRedirect("/geo/editor/location/")

        # Did the user click on one of our "Edit" buttons?  We redirect the
        # user to the "Edit" page for the associated location name.

        for loc_name in loc_names:
            editValue = request.POST.get("edit-" + str(loc_name.id))
            if editValue == "Edit":
                return HttpResponseRedirect("/geo/editor/names/edit/" +
                                            str(loc_name.id) + "/")

        # Did the user click on one of our "Delete" buttons?  We firstly
        # display the confirmation button beside the entry, and only delete the
        # entry if the user confirms.

        for loc_name in loc_names:
            deleteValue = request.POST.get("del-" + str(loc_name.id))
            if deleteValue == "Delete":
                # The user clicked on the "Delete" button for the first time ->
                # redisplay the page with the confirmation buttons.
                return HttpResponseRedirect("/geo/editor/names/" +
                                            loc_code + "/" +
                                            "?confirm=" + str(loc_name.id))
            elif deleteValue == "Yes":
                # The user clicked on our "Yes" confirmation button.  Remove
                # this location name entry.  If this is the last occurrence of
                # this name, remove the name as well.

                name = loc_name.name
                loc_name.delete()

                if name.locationname_set.count() == 0:
                    name.delete()

                # Send a signal telling the rest of the system that the Name
                # and LocationName tables have been changed.

                name_changed.send(sender=None)
                location_name_changed.send(sender=None)

                # Redisplay the updated page without the confirmation buttons.

                return HttpResponseRedirect("/geo/editor/names/" + loc_code)
            elif deleteValue == "No":
                # The user clicked on the "No" confirmation button.  Redisplay
                # the page without the confirmation buttons.
                return HttpResponseRedirect("/geo/editor/names/"+loc_code+"/")

        # Did the user click on the "Add" button?

        if request.POST.get("add") == "Add Name":
            return HttpResponseRedirect("/geo/editor/names/add/"+loc_code+"/")

        # If we get here, we're going to display the form again.  Grab our
        # "confirm" parameter so the form can display the appropriate
        # confirmation buttons.

        confirm = request.POST.get("confirm")

    # If we get here, display the list of names for this location.

    return render_to_response("editor/templates/location_wrapper.html",
                              {'tab'             : "names",
                               'heading'         : "Editing " + str(location),
                               'location'        : location,
                               'template_name'   : "location_names.html",
                               'extra_head_tags' : imports,
                               'errMsg'          : None,
                               'form'            : None,
                               'confirm'         : confirm,
                               'loc_names'       : loc_names,
                              },
                              context_instance=RequestContext(request))