def import_data(f, log): """ Import the data in 'f', recording errors etc into 'log'. 'f' is a temporary file holding the data to import, and 'log' is a list of strings. We import the data from 'f', updating the Location records, etc, as we go along. If we encounter any errors, or want to notify the user of an action which has occurred, we append the error or notification to the 'log' list. """ locs_with_names = set() # Set of location codes which we had names for. cur_loc = None # Location object we are currently importing. for line in f: line = line.rstrip() if len(line) == 0: continue # Ignore blanks. parts = line.split("\t") if len(parts) < 2: log.append("File contains invalid data. Are you sure it's " \ + "a tab-delimited text file?") break # Process this line. loc_code = parts[0] field = parts[1].lower() if len(parts) >= 3: value = parts[2] else: value = "" if cur_loc == None or loc_code != cur_loc.code: # We're starting a new location. if cur_loc != None: # Save the previous location to disk. cur_loc.save() if is_new: log.append("Added location " + cur_loc.code) else: log.append("Updated location " + cur_loc.code) # Load the new location into memory. try: cur_loc = Location.objects.get(code=loc_code) is_new = False except Location.DoesNotExist: cur_loc = Location() # Create a new location. cur_loc.code = loc_code is_new = True # Import the field specified by this line in the import field. if field == "level": try: level = Level.objects.get(level=int(value)) except: level = None if level != None: cur_loc.level = level elif field == "full_name": cur_loc.name = value elif field == "display_name": cur_loc.display_name = value elif field == "min_zoom_lat": try: cur_loc.min_zoom_lat = decimal.Decimal(value) except: log.append("Invalid min_zoom_lat value '" + value + "' for location " + loc_code) elif field == "min_zoom_long": try: cur_loc.min_zoom_long = decimal.Decimal(value) except: log.append("Invalid min_zoom_long value '" + value + "' for location " + loc_code) elif field == "max_zoom_lat": try: cur_loc.max_zoom_lat = decimal.Decimal(value) except: log.append("Invalid max_zoom_lat value '" + value + "' for location " + loc_code) elif field == "max_zoom_long": try: cur_loc.max_zoom_long = decimal.Decimal(value) except: log.append("Invalid max_zoom_long value '" + value + "' for location " + loc_code) elif field == "population": try: cur_loc.population = int(value) except: log.append("Invalid population value '" + value + "' for location " + loc_code) elif field == "area": try: cur_loc.area = int(value) except: log.append("Invalid area value '" + value + "' for location " + loc_code) elif field == "income": try: cur_loc.averageIncome = decimal.Decimal(value) except: log.append("Invalid income value '" + value + "' for location " + loc_code) elif field == "parents": parents = [] ok = True # initially. for parent_code in parts[2:]: try: parent = Location.objects.get(code=parent_code) except Location.DoesNotExist: log.append("Invalid parent location code: " + parent_code) ok = False break parents.append(parent) if ok: cur_loc.parents.clear() cur_loc.parents.add(*parents) elif field == "children": children = [] ok = True # initially. for child_code in parts[2:]: try: child = Location.objects.get(code=child_code) except Location.DoesNotExist: log.append("Invalid child location code: " + child_code) ok = False break children.append(child) if ok: cur_loc.children.clear() cur_loc.children.add(*children) elif field == "neighbors": neighbours = [] ok = True # initially. for neighbour_code in parts[2:]: try: neighbour = Location.objects.get(code=neighbour_code) except Location.DoesNotExist: log.append("Invalid neighbour location code: " + neighbour_code) ok = False break neighbours.append(neighbour) if ok: cur_loc.neighbors.clear() cur_loc.neighbors.add(*neighbours) elif field == "outline": wkt = "".join(parts[2:]) try: outline = Outline.objects.get(location=cur_loc) except Outline.DoesNotExist: if is_new: # We have to save the current location before our outline # can refer to it. cur_loc.save() outline = Outline() outline.location = cur_loc outline.outline = wkt outline.save() elif field == "name" or field == "addname": filter = {} ok = True # initially. for filter_src in parts[3:]: if "=" not in filter_src: log.append("Invalid name filter: '" + filter_src + "'") ok = False break key,value = filter_src.split("=", 1) key = key.strip().lower() value = value.strip() if key not in ["source", "country", "state", "metro", "region", "county", "city"]: log.append("Invalid name filter: '" + filter_src + "'") ok = False break try: filter_loc = Location.objects.get(code=value) except Location.DoesNotExist: log.append("The filter '" + filter_src + " refers to " + \ "a non-existent location.") ok = False break filter[key] = filter_loc if ok: # If this is the first time we've received a name for this # location, and we aren't adding to the list of names, erase # the existing names (if any). if field == "name": if loc_code not in locs_with_names and not is_new: for loc_name in LocationName.objects.filter( location=cur_loc): name = loc_name.name loc_name.delete() if name.locationname_set.count() == 0: # We've removed the last occurrence of this # name -> delete the name as well. name.delete() # Remember that we've got a name for this location. locs_with_names.add(loc_code) # Create a new LocationName record for this name. if is_new: # We have to save the current location before our # LocationName object can refer to it. cur_loc.save() try: name = Name.objects.get(level=cur_loc.level, name=parts[2]) except Name.DoesNotExist: name = Name() name.level = cur_loc.level name.name = parts[2] name.save() loc_name = LocationName() loc_name.name = name loc_name.location = cur_loc for field,filter_loc in filter.items(): if field == "source": loc_name.sourceFilter = filter_loc elif field == "country": loc_name.countryFilter = filter_loc elif field == "state": loc_name.stateFilter = filter_loc elif field == "metro": loc_name.metroFilter = filter_loc elif field == "region": loc_name.regionFilter = filter_loc elif field == "county": loc_name.countyFilter = filter_loc elif field == "city": loc_name.cityFilter = filter_loc loc_name.save() elif field == "delete": log.append("Deleting locations is not supported yet.") else: log.append("Invalid field for location " + loc_code + ": '" + field + "'") f.close() if cur_loc != None: # Save the last location to disk. cur_loc.save() if is_new: log.append("Added location " + cur_loc.code) else: log.append("Updated location " + cur_loc.code)
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))