Path problems with upload in forms.py
I had some problems getting the upload functionality to work on Os X running django from the svn. I was able to get it working by messing with the way paths and filenames are parsed in forms.py. The way it was working as cloned from hg, it was trying to open a file that didn't exist.
Additionally, it looks like ds._driver isn't a private method anymore. It's ds.driver now. I couldn't figure out how to do a proper patch in hg, so I'll just include the modified validate method from forms.py bellow. Aside from importing os, that's all I changed.
def validate(self,uploaded_file): tmp = tempfile.NamedTemporaryFile(suffix='.shp', mode = 'w') tmp_dir = tempfile.gettempdir() destination = open(tmp.name, 'wb+') for chunk in uploaded_file.chunks(): destination.write(chunk) destination.close() if not zipfile.is_zipfile(tmp.name): return False, 'That file is not a valid Zip Archive' else: zfile = zipfile.ZipFile(tmp.name) if not self.zip_check('shp', zfile): return False, 'Found Zip Archive but no file with a .shp extension found inside.' elif not self.zip_check('prj', zfile): return False, 'You must supply a .prj file with the Shapefile to indicate the projection.' elif not self.zip_check('dbf', zfile): return False, 'You must supply a .dbf file with the Shapefile to supply attribute data.' elif not self.zip_check('shx', zfile): return False, 'You must supply a .shx file for the Shapefile to have a valid index.' else: for info in zfile.infolist(): data = zfile.read(info.filename) if not info.filename[-1]==os.path.sep: shp_part = '%s%s%s' % (tmp_dir, os.path.sep, info.filename.split(os.path.sep)[-1]) fout = open(shp_part, "wb") fout.write(data) fout.close() # http://code.djangoproject.com/wiki/GeoDjangoExtras#DataSource ds_name = zfile.infolist().filename.split('.') ds = DataSource('%s%s%s.shp' % (tmp_dir, os.path.sep, ds_name.rstrip(os.path.sep))) layer = ds if layer.test_capability('RandomRead'): if ds.driver.__str__() == 'ESRI Shapefile': #apparently driver is no longer a private method return True, None else: return False, "Sorry, we've experienced a problem on our server. Please try again later." else: return False, 'Cannot read the shapefile, data is corrupted inside the zip, please try to upload again'
Thanks! I'm already using the export functionality and that works great.
-Jared Kibele MarineMap GIS analyst