Skip to content

Commit

Permalink
make thumbnail storage and url configurable and move outside static. …
Browse files Browse the repository at this point in the history
…update geonetwork with thumbnail URL and some refactoring/cleanup. closes GeoNode#135 and closes GeoNode#141
  • Loading branch information
Ian Schneider committed Jan 24, 2012
1 parent 2f45386 commit ebc1938
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
37 changes: 31 additions & 6 deletions src/GeoNodePy/geonode/maps/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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):
Expand Down
5 changes: 1 addition & 4 deletions src/GeoNodePy/geonode/maps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. \
Expand Down
7 changes: 7 additions & 0 deletions src/GeoNodePy/geonode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down
8 changes: 5 additions & 3 deletions src/GeoNodePy/geonode/templates/maps/csw/full_metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,22 @@
</gmd:maintenanceAndUpdateFrequency>
</gmd:MD_MaintenanceInformation>
</gmd:resourceMaintenance>
<!--
{% if layer.get_thumbnail %}
<gmd:graphicOverview>
<gmd:MD_BrowseGraphic>
<gmd:fileName>
<gco:CharacterString>thumbnail_s.gif</gco:CharacterString>
<gco:CharacterString>{{ layer.get_thumbnail.get_thumbnail_url }}</gco:CharacterString>
</gmd:fileName>
<gmd:fileDescription>
<gco:CharacterString>thumbnail</gco:CharacterString>
</gmd:fileDescription>
<gmd:fileType>
<gco:CharacterString>gif</gco:CharacterString>
<gco:CharacterString>png</gco:CharacterString>
</gmd:fileType>
</gmd:MD_BrowseGraphic>
</gmd:graphicOverview>
{% endif %}
<!--
<gmd:graphicOverview>
<gmd:MD_BrowseGraphic>
<gmd:fileName>
Expand Down

0 comments on commit ebc1938

Please sign in to comment.