Skip to content

Commit

Permalink
Fix QGIS-Server thumbnail
Browse files Browse the repository at this point in the history
- Add thumbnail for map
- Fix thumbnail tests for layer and map
  • Loading branch information
lucernae committed May 2, 2017
1 parent 9e83e39 commit 344f893
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion geonode/messaging/management/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
26 changes: 18 additions & 8 deletions geonode/qgis_server/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -277,14 +271,27 @@ 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)
query_string = {
'SERVICE': 'MAPCOMPOSITION',
'PROJECT': project_path,
'FILES': ';'.join(files),
'NAMES': ';'.join(names)
'NAMES': ';'.join(names),
'OVERWRITE': 'true',
}

url = qgis_server + '?'
Expand All @@ -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(
Expand Down
22 changes: 19 additions & 3 deletions geonode/qgis_server/tasks/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,40 @@

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__)


@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)
Expand Down
11 changes: 2 additions & 9 deletions geonode/tests/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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
"""
Expand Down

0 comments on commit 344f893

Please sign in to comment.