def upload(request): 'Upload waypoints' # If the form contains an upload, if 'gpx' in request.FILES: # Get gpxFile = request.FILES['gpx'] # Save targetPath = tempfile.mkstemp()[1] destination = open(targetPath, 'wt') for chunk in gpxFile.chunks(): destination.write(chunk) destination.close() # Parse dataSource = DataSource(targetPath) layer = dataSource[0] waypointNames = layer.get_fields('name') waypointGeometries = layer.get_geoms() for waypointName, waypointGeometry in itertools.izip( waypointNames, waypointGeometries): waypoint = Waypoint(name=waypointName, geometry=waypointGeometry.wkt) waypoint.save() # Clean up os.remove(targetPath) # Redirect return HttpResponseRedirect(reverse('waypoints-index'))
def upload(request): if 'gpx' in request.FILES: # Get try: gpxFile = request.FILES['gpx'] except IOError: print("gpx") handle, targetPath = tempfile.mkstemp() destination = os.fdopen(handle, 'wb') for chunk in gpxFile.chunks(): destination.write(chunk) destination.close() # Parse dataSource = DataSource(targetPath) layer = dataSource[0] waypointNames = layer.get_fields('name') waypointGeometries = layer.get_geoms() # name change from Python 2 to 3 try: zip_longest = itertools.zip_longest # Python 3 except AttributeError: zip_longest = itertools.izip_longest # Python 2 for waypointName, waypointGeometry in zip_longest( waypointNames, waypointGeometries): waypoint = Waypoint(name=waypointName, geometry=waypointGeometry.wkt) waypoint.save() # Clean up: took out the os.remove(targetPath) return HttpResponseRedirect(reverse('waypoints-index'))