From c5c0f1616b8441a77d6c70e15f1d10f87572fad8 Mon Sep 17 00:00:00 2001 From: Oliver Roick Date: Wed, 7 Dec 2016 18:42:45 +0100 Subject: [PATCH] Fix #925 -- Remove project slug from file names --- cadasta/organization/download/shape.py | 23 +++++----- cadasta/organization/tests/test_downloads.py | 42 +++++++++---------- cadasta/organization/tests/test_forms.py | 24 +++++------ .../organization/download/shp_readme.txt | 2 +- 4 files changed, 45 insertions(+), 46 deletions(-) diff --git a/cadasta/organization/download/shape.py b/cadasta/organization/download/shape.py index 7ba4b7163..2e266d87d 100644 --- a/cadasta/organization/download/shape.py +++ b/cadasta/organization/download/shape.py @@ -66,21 +66,21 @@ def write_features(self, layers, filename): values = self.get_values(su, model_attrs, schema_attrs) csvwriter.writerow(values) - def create_datasource(self, dst_dir, f_name): + def create_datasource(self, dst_dir): if not os.path.exists(dst_dir): os.makedirs(dst_dir) - path = os.path.join(dst_dir, f_name + '-point.shp') + path = os.path.join(dst_dir, 'point.shp') driver = ogr.GetDriverByName('ESRI Shapefile') return driver.CreateDataSource(path) - def create_shp_layers(self, datasource, f_name): + def create_shp_layers(self, datasource): srs = osr.SpatialReference() srs.ImportFromEPSG(4326) layers = ( - datasource.CreateLayer(f_name + '-point', srs, geom_type=1), - datasource.CreateLayer(f_name + '-line', srs, geom_type=2), - datasource.CreateLayer(f_name + '-polygon', srs, geom_type=3) + datasource.CreateLayer('point', srs, geom_type=1), + datasource.CreateLayer('line', srs, geom_type=2), + datasource.CreateLayer('polygon', srs, geom_type=3) ) for layer in layers: @@ -92,8 +92,8 @@ def create_shp_layers(self, datasource, f_name): def make_download(self, f_name): dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/{}'.format(f_name)) - ds = self.create_datasource(dst_dir, self.project.slug) - layers = self.create_shp_layers(ds, self.project.slug) + ds = self.create_datasource(dst_dir) + layers = self.create_shp_layers(ds) self.write_features(layers, os.path.join(dst_dir, 'locations.csv')) self.write_relationships(os.path.join(dst_dir, 'relationships.csv')) @@ -104,12 +104,11 @@ def make_download(self, f_name): path = os.path.join(settings.MEDIA_ROOT, 'temp/{}.zip'.format(f_name)) readme = render_to_string( 'organization/download/shp_readme.txt', - {'project_name': self.project.name, - 'project_slug': self.project.slug} + {'project_name': self.project.name} ) with ZipFile(path, 'a') as myzip: myzip.writestr('README.txt', readme) - for file in os.listdir(dst_dir): - myzip.write(os.path.join(dst_dir, file), arcname=file) + for f in os.listdir(dst_dir): + myzip.write(os.path.join(dst_dir, f), arcname=f) return path, MIME_TYPE diff --git a/cadasta/organization/tests/test_downloads.py b/cadasta/organization/tests/test_downloads.py index d67402f71..fc6b4a8ad 100644 --- a/cadasta/organization/tests/test_downloads.py +++ b/cadasta/organization/tests/test_downloads.py @@ -129,9 +129,9 @@ def test_create_datasource(self): exporter = ShapeExporter(project) dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file') - ds = exporter.create_datasource(dst_dir, 'file0') + ds = exporter.create_datasource(dst_dir) assert (ds.GetName() == - os.path.join(settings.MEDIA_ROOT, 'temp/file/file0-point.shp')) + os.path.join(settings.MEDIA_ROOT, 'temp/file/point.shp')) ds.Destroy() def test_create_shp_layers(self): @@ -140,12 +140,12 @@ def test_create_shp_layers(self): exporter = ShapeExporter(project) dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file6') - ds = exporter.create_datasource(dst_dir, 'file6') - layers = exporter.create_shp_layers(ds, 'file6') + ds = exporter.create_datasource(dst_dir) + layers = exporter.create_shp_layers(ds) assert len(layers) == 3 - assert layers[0].GetName() == 'file6-point' - assert layers[1].GetName() == 'file6-line' - assert layers[2].GetName() == 'file6-polygon' + assert layers[0].GetName() == 'point' + assert layers[1].GetName() == 'line' + assert layers[2].GetName() == 'polygon' ds.Destroy() def test_write_items(self): @@ -228,8 +228,8 @@ def test_write_features(self): attributes={'key': 'value 2'}) dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file4') - ds = exporter.create_datasource(dst_dir, 'file4') - layers = exporter.create_shp_layers(ds, 'file4') + ds = exporter.create_datasource(dst_dir) + layers = exporter.create_shp_layers(ds) csvfile = os.path.join(dst_dir, 'locations.csv') exporter.write_features(layers, csvfile) @@ -322,18 +322,18 @@ def test_make_download(self): with ZipFile(path, 'r') as testzip: assert len(testzip.namelist()) == 16 - assert project.slug + '-point.dbf' in testzip.namelist() - assert project.slug + '-point.prj' in testzip.namelist() - assert project.slug + '-point.shp' in testzip.namelist() - assert project.slug + '-point.shx' in testzip.namelist() - assert project.slug + '-line.dbf' in testzip.namelist() - assert project.slug + '-line.prj' in testzip.namelist() - assert project.slug + '-line.shp' in testzip.namelist() - assert project.slug + '-line.shx' in testzip.namelist() - assert project.slug + '-polygon.dbf' in testzip.namelist() - assert project.slug + '-polygon.prj' in testzip.namelist() - assert project.slug + '-polygon.shp' in testzip.namelist() - assert project.slug + '-polygon.shx' in testzip.namelist() + assert 'point.dbf' in testzip.namelist() + assert 'point.prj' in testzip.namelist() + assert 'point.shp' in testzip.namelist() + assert 'point.shx' in testzip.namelist() + assert 'line.dbf' in testzip.namelist() + assert 'line.prj' in testzip.namelist() + assert 'line.shp' in testzip.namelist() + assert 'line.shx' in testzip.namelist() + assert 'polygon.dbf' in testzip.namelist() + assert 'polygon.prj' in testzip.namelist() + assert 'polygon.shp' in testzip.namelist() + assert 'polygon.shx' in testzip.namelist() assert 'relationships.csv' in testzip.namelist() assert 'parties.csv' in testzip.namelist() assert 'locations.csv' in testzip.namelist() diff --git a/cadasta/organization/tests/test_forms.py b/cadasta/organization/tests/test_forms.py index e3676fe93..27c4b329f 100644 --- a/cadasta/organization/tests/test_forms.py +++ b/cadasta/organization/tests/test_forms.py @@ -1110,18 +1110,18 @@ def test_get_shape_download(self): with ZipFile(path, 'r') as testzip: assert len(testzip.namelist()) == 16 - assert project.slug + '-point.dbf' in testzip.namelist() - assert project.slug + '-point.prj' in testzip.namelist() - assert project.slug + '-point.shp' in testzip.namelist() - assert project.slug + '-point.shx' in testzip.namelist() - assert project.slug + '-line.dbf' in testzip.namelist() - assert project.slug + '-line.prj' in testzip.namelist() - assert project.slug + '-line.shp' in testzip.namelist() - assert project.slug + '-line.shx' in testzip.namelist() - assert project.slug + '-polygon.dbf' in testzip.namelist() - assert project.slug + '-polygon.prj' in testzip.namelist() - assert project.slug + '-polygon.shp' in testzip.namelist() - assert project.slug + '-polygon.shx' in testzip.namelist() + assert 'point.dbf' in testzip.namelist() + assert 'point.prj' in testzip.namelist() + assert 'point.shp' in testzip.namelist() + assert 'point.shx' in testzip.namelist() + assert 'line.dbf' in testzip.namelist() + assert 'line.prj' in testzip.namelist() + assert 'line.shp' in testzip.namelist() + assert 'line.shx' in testzip.namelist() + assert 'polygon.dbf' in testzip.namelist() + assert 'polygon.prj' in testzip.namelist() + assert 'polygon.shp' in testzip.namelist() + assert 'polygon.shx' in testzip.namelist() assert 'relationships.csv' in testzip.namelist() assert 'parties.csv' in testzip.namelist() assert 'locations.csv' in testzip.namelist() diff --git a/cadasta/templates/organization/download/shp_readme.txt b/cadasta/templates/organization/download/shp_readme.txt index 06fc6e99a..0ab1778cd 100644 --- a/cadasta/templates/organization/download/shp_readme.txt +++ b/cadasta/templates/organization/download/shp_readme.txt @@ -5,7 +5,7 @@ You have downloaded all data from project "{{ project_name }}" in shapefile form Besides this README, the ZIP archive contains the shape files and CSV files containing the project data. -Shape files can only store geometries of a single type: either point, line or polygon. Because of this, you will find three shape files ({{ project_slug }}-point.shp, {{ project_slug }}-line.shp, and {{ project_slug }}-polygon.shp) containing the geometries of all locations in project "{{ project_name }}", along with the corresponding *.shx, *.prj and *.dbf files. The attribute table of each shapefile contains only the location ID. +Shape files can only store geometries of a single type: either point, line or polygon. Because of this, you will find three shape files (point.shp, line.shp, and polygon.shp) containing the geometries of all locations in project "{{ project_name }}", along with the corresponding *.shx, *.prj and *.dbf files. The attribute table of each shapefile contains only the location ID. The attributes for locations, parties and relationships are provided in CSV files called locations.csv, parties.csv and relationships.csv. We use CSV instead of dBase files to avoid certain restrictions of the dBase format that would cause some data to be truncated. {% endblocktrans %}