From 0a31e6e5d195867d9c8c8a4cd611ec388ceaaa67 Mon Sep 17 00:00:00 2001 From: Rizky Maulana Nugraha Date: Mon, 1 May 2017 12:31:02 +0700 Subject: [PATCH] Fix QGIS-Server thumbnail - Add thumbnail for map - Fix thumbnail tests for layer and map --- geonode/messaging/management/__init__.py | 2 +- geonode/qgis_server/signals.py | 26 ++++++++++++++++-------- geonode/qgis_server/tasks/update.py | 22 +++++++++++++++++--- geonode/tests/integration.py | 11 ++-------- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/geonode/messaging/management/__init__.py b/geonode/messaging/management/__init__.py index dea3bf87b5a..e84ebf403c4 100644 --- a/geonode/messaging/management/__init__.py +++ b/geonode/messaging/management/__init__.py @@ -32,7 +32,7 @@ def create_notice_types(app, created_models, verbosity, **kwargs): notification.models.NoticeType.create("layer_comment", _("Comment on Layer"), _("A layer was commented on")) notification.models.NoticeType.create("layer_rated", _("Rating for Layer"), _("A rating was given to a layer")) - signals.post_syncdb.connect(create_notice_types, sender=notification) + signals.post_migrate.connect(create_notice_types, sender=notification) logger.info("Notifications Configured for geonode.layers.managment.commands") else: logger.info("Skipping creation of NoticeTypes for geonode.layers.management.commands, since notification app was \ diff --git a/geonode/qgis_server/signals.py b/geonode/qgis_server/signals.py index a88202ceafb..4e1ffb07165 100644 --- a/geonode/qgis_server/signals.py +++ b/geonode/qgis_server/signals.py @@ -34,7 +34,6 @@ from geonode.layers.models import Layer from geonode.maps.models import Map, MapLayer from geonode.qgis_server.tasks.update import create_qgis_server_thumbnail -from geonode.geoserver.helpers import http_client from geonode.qgis_server.gis_tools import set_attributes logger = logging.getLogger("geonode.qgis_server.signals") @@ -232,13 +231,8 @@ def qgis_server_post_save(instance, sender, **kwargs): ) # Create thumbnail - thumbnail_remote_url = reverse( - 'qgis-server-thumbnail', kwargs={'layername': instance.name}) - thumbnail_remote_url = urljoin(base_url, thumbnail_remote_url) - logger.debug(thumbnail_remote_url) - create_qgis_server_thumbnail.delay( - instance, thumbnail_remote_url, ogc_client=http_client) + instance, overwrite=True) # Attributes set_attributes(instance) @@ -277,6 +271,18 @@ def qgis_server_post_save_map(instance, sender, **kwargs): # The signal is called to early, the map has not layer yet. return + # Set bounding box based on all layers extents. + bbox = instance.get_bbox_from_layers(instance.local_layers) + instance.set_bounds_from_bbox(bbox) + Map.objects.filter(id=map_id).update( + bbox_x0=instance.bbox_x0, + bbox_x1=instance.bbox_x1, + bbox_y0=instance.bbox_y0, + bbox_y1=instance.bbox_y1, + zoom=instance.zoom, + center_x=instance.center_x, + center_y=instance.center_y) + # Create the QGIS Project qgis_server = settings.QGIS_SERVER_CONFIG['qgis_server_url'] project_path = os.path.join(QGIS_layer_directory, 'map_%s.qgs' % map_id) @@ -284,7 +290,8 @@ def qgis_server_post_save_map(instance, sender, **kwargs): 'SERVICE': 'MAPCOMPOSITION', 'PROJECT': project_path, 'FILES': ';'.join(files), - 'NAMES': ';'.join(names) + 'NAMES': ';'.join(names), + 'OVERWRITE': 'true', } url = qgis_server + '?' @@ -296,6 +303,9 @@ def qgis_server_post_save_map(instance, sender, **kwargs): logger.debug( 'Creating the QGIS Project : %s -> %s' % (project_path, data)) + create_qgis_server_thumbnail.delay( + instance, overwrite=True) + logger.debug('Register signals QGIS Server') signals.pre_save.connect( diff --git a/geonode/qgis_server/tasks/update.py b/geonode/qgis_server/tasks/update.py index c8a7cb8b0c8..507f28307a1 100644 --- a/geonode/qgis_server/tasks/update.py +++ b/geonode/qgis_server/tasks/update.py @@ -20,11 +20,15 @@ import logging import time +from urlparse import urljoin from celery.task import task +from django.conf import settings +from django.core.urlresolvers import reverse +from geonode.layers.models import Layer from geonode.layers.utils import create_thumbnail - +from geonode.maps.models import Map logger = logging.getLogger(__name__) @@ -32,12 +36,24 @@ @task( name='geonode.qgis_server.tasks.update.create_qgis_server_thumbnail', queue='update') -def create_qgis_server_thumbnail(instance, thumbnail_remote_url, ogc_client): +def create_qgis_server_thumbnail(instance, overwrite=False): try: # to make sure it is executed after the instance saved time.sleep(5) + base_url = settings.SITEURL + if isinstance(instance, Layer): + thumbnail_remote_url = reverse( + 'qgis-server-thumbnail', kwargs={'layername': instance.name}) + elif isinstance(instance, Map): + thumbnail_remote_url = reverse( + 'qgis-server-map-thumbnail', kwargs={'map_id': instance.id}) + else: + # instance type does not have associated thumbnail + return True + thumbnail_remote_url = urljoin(base_url, thumbnail_remote_url) + logger.debug(thumbnail_remote_url) logger.debug('Create thumbnail for %s' % thumbnail_remote_url) - create_thumbnail(instance, thumbnail_remote_url, ogc_client=ogc_client) + create_thumbnail(instance, thumbnail_remote_url, overwrite=overwrite) return True except Exception as e: logger.exception(e) diff --git a/geonode/tests/integration.py b/geonode/tests/integration.py index 753885a3a04..e8b1db6a67b 100644 --- a/geonode/tests/integration.py +++ b/geonode/tests/integration.py @@ -915,10 +915,6 @@ def setUp(self): def tearDown(self): pass - @unittest.skipIf( - hasattr(settings, 'SKIP_GEOSERVER_TEST') and - settings.SKIP_GEOSERVER_TEST, - 'Temporarily skip this test until fixed') def test_layer_thumbnail(self): """Test the layer save method generates a thumbnail link """ @@ -937,16 +933,13 @@ def test_layer_thumbnail(self): overwrite=True, ) - saved_layer = geoserver_post_save2(saved_layer.id) + if 'geonode.geoserver' in settings.INSTALLED_APPS: + saved_layer = geoserver_post_save2(saved_layer.id) thumbnail_url = saved_layer.get_thumbnail_url() assert thumbnail_url != staticfiles.static(settings.MISSING_THUMBNAIL) - @unittest.skipIf( - hasattr(settings, 'SKIP_GEOSERVER_TEST') and - settings.SKIP_GEOSERVER_TEST, - 'Temporarily skip this test until fixed') def test_map_thumbnail(self): """Test the map save method generates a thumbnail link """