Path problems with upload in forms.py

Anonymous avatarAnonymous 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 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()[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
            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

Comments (2)

  1. Dane Springmeyer

    Jared,

    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

    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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.