From ebc19380e33bddd7e508cf76c1b524fbdc78e741 Mon Sep 17 00:00:00 2001 From: Ian Schneider Date: Tue, 24 Jan 2012 10:29:17 -0700 Subject: [PATCH] make thumbnail storage and url configurable and move outside static. update geonetwork with thumbnail URL and some refactoring/cleanup. closes #135 and closes #141 --- src/GeoNodePy/geonode/maps/models.py | 37 ++++++++++++++++--- src/GeoNodePy/geonode/maps/views.py | 5 +-- src/GeoNodePy/geonode/settings.py | 7 ++++ .../templates/maps/csw/full_metadata.xml | 8 ++-- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/GeoNodePy/geonode/maps/models.py b/src/GeoNodePy/geonode/maps/models.py index 8ecbeee056d..39128cb0e55 100644 --- a/src/GeoNodePy/geonode/maps/models.py +++ b/src/GeoNodePy/geonode/maps/models.py @@ -570,6 +570,26 @@ def get_csw(): _csw = CatalogueServiceWeb(csw_url) return _csw +class ThumbnailMixin: + def get_thumbnail(self): + """Obtain the thumbnail, if one exists for this object""" + return Thumbnail.objects.get_thumbnail(self) + def set_thumbnail(self, spec): + """Update the thumbnail specification for this object + and generate a new thumbnail image. Returns the thumbnail object. + """ + thumb = Thumbnail.objects.get_thumbnail(self,allow_null=False) + thumb.thumb_spec = spec + thumb.generate_thumbnail() + # if newly created, the thumb will not have an id + created = thumb.id is not None + thumb.save() + self._thumbnail_updated(thumb, created) + return thumb + def _thumbnail_updated(self, thumb, created): + """Hook for update behavior""" + pass + class LayerManager(models.Manager): def __init__(self): @@ -656,7 +676,7 @@ def get_wms(self,layer_typename=None,reload=False): self._wms = _get_wms() return self._wms -class Layer(models.Model, PermissionLevelMixin): +class Layer(models.Model, PermissionLevelMixin, ThumbnailMixin): """ Layer Object loosely based on ISO 19115:2003 """ @@ -995,6 +1015,10 @@ def _get_styles(self): def _set_styles(self, styles): self.publishing.styles = styles + + def _thumbnail_updated(self, thumb, created): + if created: + self.save_to_geonetwork() styles = property(_get_styles, _set_styles) @@ -1180,7 +1204,7 @@ def set_default_permissions(self): self.set_user_level(self.owner, self.LEVEL_ADMIN) -class Map(models.Model, PermissionLevelMixin): +class Map(models.Model, PermissionLevelMixin, ThumbnailMixin): """ A Map aggregates several layers together and annotates them with a viewport configuration. @@ -1722,8 +1746,8 @@ class ThumbnailManager(models.Manager): def __init__(self): models.Manager.__init__(self) self.storage = FileSystemStorage( - location = os.path.join(settings.PROJECT_ROOT, "static","thumbs"), - base_url = settings.STATIC_URL + "thumbs/" + location = os.path.join(settings.THUMBNAIL_STORAGE), + base_url = settings.THUMBNAIL_URL ) if not os.path.exists(self.storage.location): os.makedirs(self.storage.location) @@ -1748,7 +1772,7 @@ def get_thumbnails(self,objs): ids = [ o.id for o in objs] thumbs = thumbs.filter(object_id__in=ids) return dict([ (t.content_object.id,t) for t in thumbs]) - + class Thumbnail(models.Model): objects = ThumbnailManager() @@ -1764,7 +1788,8 @@ def _path(self): parts = "layer",self.content_object.uuid return "".join(parts) + ".png" def get_thumbnail_url(self): - return Thumbnail.objects.storage.url(self._path()) + relative = Thumbnail.objects.storage.url(self._path()) + return settings.SITEURL[:-1] + relative def get_thumbnail_path(self): return Thumbnail.objects.storage.path(self._path()) def delete(self): diff --git a/src/GeoNodePy/geonode/maps/views.py b/src/GeoNodePy/geonode/maps/views.py index abaf3c8d2c5..f5a9a2ffc2e 100644 --- a/src/GeoNodePy/geonode/maps/views.py +++ b/src/GeoNodePy/geonode/maps/views.py @@ -862,10 +862,7 @@ def _handleThumbNail(req, obj): else: raise HttpResponse(status=404) elif req.method == 'POST': - thumb = Thumbnail.objects.get_thumbnail(obj,allow_null=False) - thumb.thumb_spec = req.raw_post_data - thumb.generate_thumbnail() - thumb.save() + thumb = obj.set_thumbnail(req.raw_post_data) return HttpResponseRedirect(thumb.get_thumbnail_url()) GENERIC_UPLOAD_ERROR = _("There was an error while attempting to upload your data. \ diff --git a/src/GeoNodePy/geonode/settings.py b/src/GeoNodePy/geonode/settings.py index b7e9a8d584e..2ec430d44e1 100644 --- a/src/GeoNodePy/geonode/settings.py +++ b/src/GeoNodePy/geonode/settings.py @@ -91,9 +91,16 @@ # Example: "http://media.lawrence.com" STATIC_URL = "/media/" +# Location of generated thumbnails on filesystem - will be automatically created +THUMBNAIL_STORAGE = os.path.join(PROJECT_ROOT, 'thumbs') + +# Prefix for thumbnail URLs +THUMBNAIL_URL = '/thumbs/' + # Additional directories which hold static files STATICFILES_DIRS = [ os.path.join(PROJECT_ROOT, "media"), + THUMBNAIL_STORAGE ] GEONODE_UPLOAD_PATH = os.path.join(STATIC_URL, "upload/") diff --git a/src/GeoNodePy/geonode/templates/maps/csw/full_metadata.xml b/src/GeoNodePy/geonode/templates/maps/csw/full_metadata.xml index d2ea7101802..4699df2505b 100644 --- a/src/GeoNodePy/geonode/templates/maps/csw/full_metadata.xml +++ b/src/GeoNodePy/geonode/templates/maps/csw/full_metadata.xml @@ -142,20 +142,22 @@ -