示例#1
0
def add(request, level):
    """ Respond to the "/geo/editor/location/add/X/" URL.

        We let the user add a location with the given level ID.
    """
    if not request.user.is_authenticated:
        return HttpResponseRedirect("/geo/editor/")

    try:
        level = Level.objects.get(level=level)
    except Level.DoesNotExist:
        return HttpResponseRedirect("/geo/editor/location/select/" +
                                    str(level.level) + "/")

    if request.method == "GET":
        form   = LocationDetailsForm()
        errMsg = None
    elif request.method == "POST":
        if request.POST.get("cancel") == "Cancel":
            return HttpResponseRedirect("/geo/editor/location/select/" +
                                        str(level.level) + "/")

        form = LocationDetailsForm(request.POST)
        errMsg = None
        if form.is_valid():
            code         = request.POST['code']
            name         = request.POST['name']
            display_name = request.POST['display_name']
            abbreviation = request.POST['abbreviation']
            display_lat  = request.POST['display_point_lat']
            display_long = request.POST['display_point_long']

            if code == "":
                errMsg = "You must enter a code for this location."
            elif name == "":
                errMsg = "You must enter a name for this location."
            elif Location.objects.filter(code=code).count() > 0:
                    errMsg = "There is already a location with that code."
            elif Location.objects.filter(level=level, name=name).count() > 0:
                    errMsg = "There is already a " + level.name.lower() \
                           + " with that name."

            if errMsg == None:
                location = Location()
                location.level         = level
                location.code          = code
                location.name          = name
                location.display_name  = display_name
                location.abbreviation  = abbreviation
                location.min_zoom_lat  = decimal.Decimal("0.00")
                location.min_zoom_long = decimal.Decimal("0.00")
                location.max_zoom_lat  = decimal.Decimal("0.00")
                location.max_zoom_long = decimal.Decimal("0.00")

                if display_lat != None:
                    location.display_lat = decimal.Decimal(display_lat)

                if display_long != None:
                    location.display_long = decimal.Decimal(display_long)

                location.population    = 0
                location.area          = 0
                location.averageIncome = 0
                location.save()

                # Send a signal telling the rest of the system that the
                # location record was added.

                location_changed.send(sender=None)

                # Finally, tell the user's web browser to reload the page.

                return HttpResponseRedirect("/geo/editor/location/details/" +
                                            code + "/")

    return render_to_response("editor/templates/editForm.html",
                              {'title'   : "Geodatabase Editor",
                               'heading' : "Add " + level.name,
                               'errMsg'  : errMsg,
                               'form'    : form},
                              context_instance=RequestContext(request))
示例#2
0
def details(request, loc_code):
    """ Respond to the "/geo/editor/location/details/XYZ/" URL.

        We let the user edit the details of the location with the given code.
    """
    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":
        data = {}
        data['code']               = location.code
        data['name']               = location.name
        data['display_name']       = location.display_name
        data['abbreviation']       = location.abbreviation
        data['display_point_lat']  = location.display_lat
        data['display_point_long'] = location.display_long
        data['population']         = location.population
        data['area']               = location.area

        if location.averageIncome != None:
            data['averageIncome'] = int(location.averageIncome)

        form   = LocationDetailsForm(data)
        errMsg = None
    elif request.method == "POST":
        if request.POST.get("cancel") == "Finished":
            return HttpResponseRedirect("/geo/editor/location/")

        if request.POST.get("delete") == "Delete this Location":
            return HttpResponseRedirect("/geo/editor/location/delete/" +
                                        location.code)

        form = LocationDetailsForm(request.POST)
        errMsg = None
        if form.is_valid():
            code          = form.cleaned_data['code']
            name          = form.cleaned_data['name']
            display_name  = form.cleaned_data['display_name']
            abbreviation  = form.cleaned_data['abbreviation']
            display_lat   = form.cleaned_data['display_point_lat']
            display_long  = form.cleaned_data['display_point_long']
            population    = form.cleaned_data['population']
            area          = form.cleaned_data['area']
            averageIncome = form.cleaned_data['averageIncome']

            if display_lat != None:
                display_lat = decimal.Decimal(display_lat)

            if display_long != None:
                display_long = decimal.Decimal(display_long)

            if averageIncome != None:
                averageIncome = decimal.Decimal(averageIncome)

            if code == "":
                errMsg = "You must enter a code for this location."
            elif name == "":
                errMsg = "You must enter a name for this location."
            elif code != location.code:
                if Location.objects.filter(code=code).count() > 0:
                    errMsg = "There is already a location with that code."
            elif name != location.name:
                if Location.objects.filter(level=location.level,
                                           name=name).count() > 0:
                    errMsg = "There is already a " \
                           + location.level.name.lower() \
                           + " with that name."
            elif display_name == "":
                errMsg = "You must enter a display name for this location."

            if errMsg == None:
                location.code          = code
                location.name          = name
                location.display_name  = display_name
                location.abbreviation  = abbreviation
                location.display_lat   = display_lat
                location.display_long  = display_long
                location.population    = population
                location.area          = area
                location.averageIncome = averageIncome
                location.save()

                # Send a signal telling the rest of the system that the
                # location record was changed.

                location_changed.send(sender=None)

                # Finally, tell the user's web browser to reload the page.

                return HttpResponseRedirect("/geo/editor/location/details/" +
                                            loc_code + "/")

    return render_to_response("editor/templates/location_wrapper.html",
                              {'tab'           : "details",
                               'heading'       : "Editing " + str(location),
                               'location'      : location,
                               'template_name' : "location_details.html",
                               'errMsg'        : errMsg,
                               'form'          : form},
                              context_instance=RequestContext(request))