def delete(request, loc_code): """ Respond to the "/geo/editor/location/delete/XYZ" URL. We let the user delete 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: raise Http404 if request.method == "POST": level = location.level if request.POST['confirm'] == "1": # Delete this location and all its associated records. Note that # we have to be careful not to delete the records associated to # this one by ForeignKey and ManyToManyFields. for l in location.loc_parents.all(): l.parents.remove(location) for l in location.loc_children.all(): l.children.remove(location) for l in location.loc_neighbors.all(): l.neighbors.remove(location) for outline in Outline.objects.filter(location=location): outline.delete() location.level = None location.parents.clear() location.children.clear() location.neighbors.clear() location.delete() # Send a signal telling the rest of the system that the location # record was deleted. location_changed.send(sender=None) return HttpResponseRedirect("/geo/editor/location/select/" + str(level.level) + "/") elif request.method == "GET": return render_to_response("editor/templates/confirm.html", {'title' : "Geodatabase Editor", 'heading' : "Delete Location", 'message' : "Are you sure you want to " + "delete the "+str(location)+"?", }, context_instance=RequestContext(request))
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))
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))
def neighbors(request, loc_code): """ Respond to the "/geo/editor/location/neighbors/XYZ/" URL. We let the user edit the list of neighbours 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/") neighbours = [] for neighbour in location.neighbors.all(): neighbours.append(neighbour) if request.method == "GET": # We're visiting the page for the first time -> just display it. form = AddLocationForm() errMsg = None confirm = request.GET.get("confirm") elif request.method == "POST": # The user is submitting our form. See what the user wants to do. # 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 "Delete" buttons? We firstly # display the confirmation button beside the entry, and only delete the # entry if the user confirms. for neighbour in location.neighbors.all(): deleteValue = request.POST.get("del-" + neighbour.code) 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/neighbors/" + loc_code + "/" + "?confirm=" + neighbour.code) elif deleteValue == "Yes": # The user clicked on our "Yes" confirmation button. Delete # this neighbour. location.neighbors.remove(neighbour) location.save() # Tell the rest of the system that the Location table has # changed. location_changed.send(sender=None) # Finally, tell the user's web browser to reload the page. return HttpResponseRedirect("/geo/editor/neighbors/"+loc_code) elif deleteValue == "No": # The user clicked on the "No" confirmation button. Redisplay # the page without the confirmation buttons. return HttpResponseRedirect("/geo/editor/neighbors/" + loc_code + "/") # Did the user click on the "Add" button? if request.POST.get("add") == "Add": # Respond to the user adding a new neighbour. form = AddLocationForm(request.POST) errMsg = None if form.is_valid(): new_neighbour_id = form.cleaned_data['loc_id'] if new_neighbour_id == None: errMsg = "Please enter a location" if errMsg == None: try: newNeighbour = Location.objects.get( id=new_neighbour_id) except Location.DoesNotExist: errMsg = "No such location." if errMsg == None: filter = location.neighbors.filter( id=new_neighbour_id) if filter.count() > 0: errMsg = "That location is already listed as " \ + "a neighbor!" if errMsg == None: location.neighbors.add(newNeighbour) location.save() # Tell the rest of the system that the Location table # has changed. location_changed.send(sender=None) # Finally, tell the user's web browser to reload the # page. return HttpResponseRedirect("/geo/editor/neighbors/" + 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 form to the user. imports = ['<script type="text/javascript" ' + 'src="/geo/media/jquery-1.2.6.min.js"></script>', '<script type="text/javascript" ' + 'src="/geo/media/jquery.autocomplete.js"></script>', '<link href="/geo/media/jquery.autocomplete.css" ' + 'rel="stylesheet" type="text/css">', '<link href="/geo/media/editor.css" rel="stylesheet" ' + 'type="text/css">', ] return render_to_response("editor/templates/location_wrapper.html", {'tab' : "neighbors", 'heading' : "Editing " + str(location), 'location' : location, 'template_name' : "location_neighbours.html", 'errMsg' : errMsg, 'form' : form, 'neighbours' : neighbours, 'confirm' : confirm, 'extra_head_tags' : imports, 'lookup_url' : "/geo/editor/location/lookup/", }, context_instance=RequestContext(request))