diff --git a/geonode/maps/qgis_server_views.py b/geonode/maps/qgis_server_views.py index 270b3d69cef..a7b0245fdce 100644 --- a/geonode/maps/qgis_server_views.py +++ b/geonode/maps/qgis_server_views.py @@ -548,10 +548,11 @@ def perm_filter(layer): + '?SERVICE=LAYERDEFINITIONS&LAYERS=' + json_layers fwd_request = requests.get(url_server) response = HttpResponse( - fwd_request.content, content_type="application/xml", + fwd_request.content, + content_type="application/x-qgis-layer-definition", status=fwd_request.status_code) - response['Content-Disposition'] = 'attachment; filename=%s' \ - % map_obj.title + '.qlr' + response['Content-Disposition'] = 'attachment; filename=%s.qlr' \ + % map_obj.title return response diff --git a/geonode/maps/views.py b/geonode/maps/views.py index 3b58b2827b8..5723b0c3e30 100644 --- a/geonode/maps/views.py +++ b/geonode/maps/views.py @@ -832,7 +832,7 @@ def map_download_leaflet(request, mapid): return HttpResponseBadRequest('Sorry, leaflet page has not supported yet') -def map_download_qlr(request): +def map_download_qlr(request, mapid): """ This function serves as geoserver compatibility. We might need to use QLR on geoserver in the future. diff --git a/geonode/qgis_server/tests/test_views.py b/geonode/qgis_server/tests/test_views.py index 42203159ee5..c0d505596fb 100644 --- a/geonode/qgis_server/tests/test_views.py +++ b/geonode/qgis_server/tests/test_views.py @@ -133,6 +133,41 @@ def test_ogc_specific_layer(self): self.assertEqual(response.get('Content-Type'), 'image/tiff') self.assertEqual(what('', h=response.content), 'tiff') + # Layer is already on the database + # checking the Link + links = uploaded.link_set.download().filter( + name__in=settings.DOWNLOAD_FORMATS_RASTER) + + # checks signals.py for the hardcoded names in QLR and QGS + qlr_link = links.get(name='QGIS layer file (.qlr)') + self.assertIn("download-qlr", qlr_link.url) + qgs_link = links.get(name='QGIS project file (.qgs)') + self.assertIn("download-qgs", qgs_link.url) + + # QLR + response = self.client.get( + reverse('qgis_server:download-qlr', kwargs=params)) + self.assertEqual(response.status_code, 200) + self.assertEqual( + response.get('Content-Type'), + 'application/x-qgis-layer-definition') + # check file name's extension + file_name = response.get('Content-Disposition').split('filename=') + file_ext = file_name[1].split('.') + self.assertEqual(file_ext[1], "qlr") + + # QGS + response = self.client.get( + reverse('qgis_server:download-qgs', kwargs=params)) + self.assertEqual(response.status_code, 200) + self.assertEqual( + response.get('Content-Type'), + 'application/x-qgis-project') + # check file name's extension + file_name = response.get('Content-Disposition').split('filename=') + file_ext = file_name[1].split('.') + self.assertEqual(file_ext[1], "qgs") + response = self.client.get( reverse('qgis_server:geotiff', kwargs={ 'layername': vector_layer.name @@ -286,8 +321,64 @@ def test_ogc_specific_layer(self): vector_layer.delete() @on_ogc_backend(qgis_server.BACKEND_PACKAGE) + def test_download_map_qlr(self): + """Test download QLR file for a map""" + # 2 layers to be added to the map + filename = os.path.join( + gisdata.GOOD_DATA, 'raster/relief_san_andres.tif') + layer1 = file_upload(filename) + + filename = os.path.join( + gisdata.GOOD_DATA, + 'vector/san_andres_y_providencia_administrative.shp') + layer2 = file_upload(filename) + + # construct json request for new map + json_payload = InitialSetup.generate_initial_map(layer1, layer2) + + self.client.login(username='admin', password='admin') + + response = self.client.post( + reverse('new_map_json'), + json.dumps(json_payload), + content_type='application/json') + # map is successfull saved + self.assertEqual(response.status_code, 200) + + map_id = json.loads(response.content).get('id') + + map = Map.objects.get(id=map_id) + + # check that QLR is added to the links + links = map.link_set.download() + map_qlr_link = links.get(name='Download QLR Layer file') + self.assertIn('qlr', map_qlr_link.url) + + # QLR + response = self.client.get( + reverse('map_download_qlr', kwargs={'mapid': map_id})) + self.assertEqual(response.status_code, 200) + self.assertEqual( + response.get('Content-Type'), + 'application/x-qgis-layer-definition') + + # cleanup + map.delete() + layer1.delete() + layer2.delete() + def test_map_json(self): - json_payload = InitialSetup.generate_initial_map() + # 2 layers to be added to the map + filename = os.path.join( + gisdata.GOOD_DATA, 'raster/relief_san_andres.tif') + layer1 = file_upload(filename) + + filename = os.path.join( + gisdata.GOOD_DATA, + 'vector/san_andres_y_providencia_administrative.shp') + layer2 = file_upload(filename) + + json_payload = InitialSetup.generate_initial_map(layer1, layer2) # First, create a map with two layers # Need to log in for saving a map self.client.login(username='admin', password='admin') @@ -322,6 +413,8 @@ def test_map_json(self): # clean up map = Map.objects.get(id=map_id) map.delete() + layer1.delete() + layer2.delete() class QGISServerStyleManagerTest(LiveServerTestCase): @@ -508,7 +601,7 @@ def test_map_thumbnail(self): """:type: geonode.layers.models.Layer""" # construct json request for new map - json_payload = InitialSetup.generate_initial_map() + json_payload = InitialSetup.generate_initial_map(layer1, layer2) self.client.login(username='admin', password='admin') @@ -572,7 +665,7 @@ def test_map_thumbnail(self): class InitialSetup(): @classmethod - def generate_initial_map(cls): + def generate_initial_map(cls, layer1, layer2): # construct json request for new map json_payload = { "sources": { @@ -620,9 +713,8 @@ def generate_initial_map(cls): "source": "source_OpenStreetMap" }, { - "name": "geonode:" - "san_andres_y_providencia_administrative", - "title": "san_andres_y_providencia_administrative", + "name": layer2.typename, + "title": layer2.name, "visibility": True, "url": "http://geonode.dev/qgis-server/tiles" "/san_andres_y_providencia_administrative/" @@ -631,8 +723,8 @@ def generate_initial_map(cls): "san_andres_y_providencia_administrative" }, { - "name": "geonode:relief_san_andres", - "title": "relief_san_andres", + "name": layer1.typename, + "title": layer1.name, "visibility": True, "url": "http://geonode.dev/qgis-server/tiles" "/relief_san_andres/{z}/{x}/{y}.png", diff --git a/geonode/qgis_server/views.py b/geonode/qgis_server/views.py index e9d4cbc76d5..76cb4426536 100644 --- a/geonode/qgis_server/views.py +++ b/geonode/qgis_server/views.py @@ -131,7 +131,7 @@ def download_qgs(request, layername): layer_title = layer.name response = HttpResponse( - result.content, content_type="application/xml", + result.content, content_type="application/x-qgis-project", status=result.status_code) response['Content-Disposition'] = \ 'attachment; filename=%s.qgs' % layer_title @@ -847,7 +847,7 @@ def download_qlr(request, layername): result = requests.get(url) response = HttpResponse( result.content, - content_type="application/xml", + content_type="application/x-qgis-layer-definition", status=result.status_code) response['Content-Disposition'] = \ 'attachment; filename=%s.qlr' % layer_title