This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
forked from GeoNode/django-osgeo-importer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from sarasafavi/multiform
Multifile upload support
- Loading branch information
Showing
17 changed files
with
733 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,73 @@ | ||
import os | ||
from django import forms | ||
from .models import UploadFile | ||
from .validators import validate_extension, validate_inspector_can_read, validate_shapefiles_have_all_parts | ||
from zipfile import is_zipfile, ZipFile | ||
import tempfile | ||
import logging | ||
import shutil | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class UploadFileForm(forms.ModelForm): | ||
class UploadFileForm(forms.Form): | ||
file = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True})) | ||
|
||
class Meta: | ||
model = UploadFile | ||
fields = ['file'] | ||
|
||
def clean(self): | ||
cleaned_data = super(UploadFileForm, self).clean() | ||
outputdir = tempfile.mkdtemp() | ||
files = self.files.getlist('file') | ||
validfiles = [] | ||
|
||
# Create list of all potentially valid files, exploding first level zip files | ||
for file in files: | ||
if not validate_extension(file.name): | ||
self.add_error('file', 'Filetype not supported.') | ||
continue | ||
|
||
if is_zipfile(file): | ||
with ZipFile(file) as zip: | ||
for zipname in zip.namelist(): | ||
if not validate_extension(zipname): | ||
self.add_error('file', 'Filetype in zip not supported.') | ||
continue | ||
validfiles.append(zipname) | ||
else: | ||
validfiles.append(file.name) | ||
# Make sure shapefiles have all their parts | ||
if not validate_shapefiles_have_all_parts(validfiles): | ||
self.add_error('file', 'Shapefiles must include .shp,.dbf,.shx,.prj') | ||
# Unpack all zip files and create list of cleaned file objects | ||
cleaned_files = [] | ||
for file in files: | ||
if file.name in validfiles: | ||
with open(os.path.join(outputdir, file.name), 'w') as outfile: | ||
for chunk in file.chunks(): | ||
outfile.write(chunk) | ||
cleaned_files.append(outfile) | ||
elif is_zipfile(file): | ||
with ZipFile(file) as zip: | ||
for zipfile in zip.namelist(): | ||
if zipfile in validfiles: | ||
with zip.open(zipfile) as f: | ||
with open(os.path.join(outputdir, zipfile), 'w') as outfile: | ||
shutil.copyfileobj(f, outfile) | ||
cleaned_files.append(outfile) | ||
|
||
# After moving files in place make sure they can be opened by inspector | ||
inspected_files = [] | ||
for cleaned_file in cleaned_files: | ||
cleaned_file_path = os.path.join(outputdir, cleaned_file.name) | ||
if not validate_inspector_can_read(cleaned_file_path): | ||
self.add_error( | ||
'file', | ||
'Inspector could not read file {} or file is empty'.format(cleaned_file_path) | ||
) | ||
continue | ||
inspected_files.append(cleaned_file) | ||
|
||
cleaned_data['file'] = inspected_files | ||
return cleaned_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('osgeo_importer', '0002_auto_20160713_1429'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='uploadlayer', | ||
name='upload_file', | ||
field=models.ForeignKey(blank=True, to='osgeo_importer.UploadFile', null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('osgeo_importer', '0003_uploadlayer_upload_file'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='uploadfile', | ||
name='file_type', | ||
field=models.CharField(max_length=50, null=True, blank=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('osgeo_importer', '0004_uploadfile_file_type'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='uploadlayer', | ||
name='layer_name', | ||
field=models.CharField(max_length=64, null=True), | ||
), | ||
] |
Oops, something went wrong.