Path problems with upload in

Issue #9 resolved
Anonymous created an issue

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 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 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(, 'wb+')
    for chunk in uploaded_file.chunks():
    if not zipfile.is_zipfile(
        return False, 'That file is not a valid Zip Archive'
        zfile = zipfile.ZipFile(
    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.'
        for info in zfile.infolist():
            data =
            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")
    ds_name = zfile.infolist()[0].filename.split('.')[0]
    ds = DataSource('%s%s%s.shp' % (tmp_dir, os.path.sep, ds_name.rstrip(os.path.sep)))
    layer = ds[0]
    if layer.test_capability('RandomRead'):
        if ds.driver.__str__() == 'ESRI Shapefile': #apparently driver is no longer a private method
            return True, None
            return False, "Sorry, we've experienced a problem on our server. Please try again later."
        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

Comments (2)

  1. Dane Springmeyer repo owner


    Nice catch on the change in the driver method. I've make your fix asap. Thanks!

    As far as the naming/file saving issue, what kind of name did the zipped shapefile have that you were having trouble uploading?

    That section is simply working with a tmp file so it could likely be simplified a bit anyway.

  2. Dane Springmeyer repo owner

    Jared, I'm not sure how the existing code was working at all because it surely was missing a path separator. I've not really used the upload functionality much in production (and likely won't until it is extended to actually write to pickled model fields), but in 4be84acf45f7 I've added a bunch of cleanup and code comments. Re-open if you find any further problems.

    Glad you are getting some use from this - be in touch if you have ideas for extending it.

  3. Log in to comment