Example #1
0
File: add.py Project: 3taps/geo
def add(request, level=None):
    """ Respond to the "/add" URL.

        If a level was supplied, we let the user add the location with that
        level.  Otherwise, we let the user choose which level of location to
        add.
    """
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse(settings.ADMIN_HOME_VIEW))

    if level == None:
        # The user hasn't chosen a level yet.  Display the "Choose a Level"
        # page.

        levels = []
        for level in Level.objects.all():
            levels.append(level)

        menu_html = menus.generate(request, "Location Editor",
                                   "location_editor", "add")

        return render_to_response("location_editor/templates/" +
                                  "add_select_level.html",
                                  {'menu_html' : menu_html,
                                   'levels'    : levels,
                                   },
                                  context_instance=RequestContext(request))

    # If we get here, we know what level of location to add.  Display the
    # "Add Location" form.

    try:
        level = Level.objects.get(level=level)
    except Level.DoesNotExist:
        return HttpResponseRedirect(reverse("location_editor.views.main"))

    if request.method == "GET":
        form   = LocationDetailsForm()
        errMsg = None
    elif request.method == "POST":
        if request.POST.get("cancel") == "Cancel":
            return HttpResponseRedirect(reverse("location_editor.views.main"))

        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 != "":
                    location.display_lat = decimal.Decimal(display_lat)

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

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

                # Finally, redirect the user to the "details" page for the
                # newly-added location.

                return HttpResponseRedirect(
                            reverse("location_editor.views.details",
                                    args=[location.code]))

    return render_to_response("shared/templates/editForm.html",
                              {'title'   : "Whenua Admin",
                               'heading' : "Add " + level.name,
                               'errMsg'  : errMsg,
                               'form'    : form},
                              context_instance=RequestContext(request))
Example #2
0
File: details.py Project: 3taps/geo
def details(request, loc_code):
    """ Respond to the "/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(reverse(settings.ADMIN_HOME_VIEW))

    try:
        location = Location.objects.get(code=loc_code)
    except Location.DoesNotExist:
        return HttpResponseRedirect(reverse("location_editor.views.main"))

    if request.method == "GET":
        data = {}
        data["code"] = location.code
        data["name"] = location.name
        data["starred"] = location.starred
        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(reverse("location_editor.views.main"))

        if request.POST.get("delete") == "Delete this Location":
            return HttpResponseRedirect(reverse("location_editor.views.delete", args=[location.code]))

        form = LocationDetailsForm(request.POST)
        errMsg = None
        if form.is_valid():
            code = form.cleaned_data["code"]
            name = form.cleaned_data["name"]
            starred = form.cleaned_data["starred"]
            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:
                is_dup = False
                for loc in Location.objects.filter(level=location.level, name=name):
                    if location.code != loc.code:
                        is_dup = True
                        break

                if is_dup:
                    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.starred = starred
                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()

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

                return HttpResponseRedirect(reverse("location_editor.views.details", args=[loc_code]))

    menu_html = menus.generate(request, "Location Editor", "location_editor", "details")

    return render_to_response(
        "location_editor/templates/wrapper.html",
        {
            "menu_html": menu_html,
            "tab": "details",
            "heading": "Editing " + str(location),
            "location": location,
            "template_name": "details.html",
            "errMsg": errMsg,
            "form": form,
        },
        context_instance=RequestContext(request),
    )