Skip to content

Commit

Permalink
merge: #53 from ginger/iiif-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
alycejenni committed Jan 15, 2024
2 parents ca43d2e + 50b27a6 commit ee939cd
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ _A CKAN extension for a dataset gallery view._
# Overview

<!--overview-start-->
Adds a gallery view for resources on a CKAN instance. Two plugins are included in this extension: `gallery` and `gallery_image`.
Adds a gallery view for resources on a CKAN instance. Three plugins are included in this extension: the main plugin (`gallery`) and two view plugins for specific image/data types (`gallery_image` and `gallery_iiif`).

Based on [blueimp Gallery](https://blueimp.github.io/Gallery).

Expand Down
78 changes: 78 additions & 0 deletions ckanext/gallery/plugins/iiif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python
# encoding: utf-8
#
# This file is part of ckanext-gallery
# Created by the Natural History Museum in London, UK

from ckanext.gallery.plugins.interfaces import IGalleryImage

from ckan.plugins import SingletonPlugin, implements, toolkit


class GalleryIIIFPlugin(SingletonPlugin):
"""
Implements the basic image field The URL of an image is present in a text field.
"""

implements(IGalleryImage)

def image_info(self):
'''
:returns: If resource type is set, only dataset of that type will be available
'''
return {
'title': 'IIIF',
'resource_type': ['csv', 'tsv'],
'field_type': ['text'],
}

def get_images(self, field_value, record, data_dict):
"""
Get images from field value and returns them as a list of dicts specifying just
the href.
:param field_value: the value of the record's image field
:param record: the record dict itself
:param data_dict: relevant data in a dict, currently we only use the resource_view contained within
:return: a list of dicts
"""

images = []

# retrieve the delimiter if there is one
delimiter = data_dict['resource_view'].get('image_delimiter', None)
if delimiter:
# split the text by the delimiter if we have one
raw_images = field_value.split(delimiter)
else:
raw_images = [field_value]

title_field = data_dict['resource_view'].get('image_title', None)

for image in raw_images:
title = record.get(title_field)
image_base_url = image.strip().strip('/')

if not image_base_url:
continue

images.append(
{
'href': f'{image_base_url}',
'thumbnail': f'{image_base_url}/thumbnail',
'download': f'{image_base_url}/original',
'link': toolkit.url_for(
'record.view',
package_name=data_dict['package']['name'],
resource_id=data_dict['resource']['id'],
record_id=record['_id'],
),
'description': title,
'title': title,
'record_id': record['_id'],
}
)

return images
40 changes: 17 additions & 23 deletions ckanext/gallery/plugins/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,29 @@

class IGalleryImage(interfaces.Interface):
"""
IGallery plugin for displaying image field.
Can be extended - eg basic image in the gallery plugin
Or DwC image field in the NHM plugin
IGallery plugin for retrieving images from records.
"""

def info(self):
'''
:return: name, title, description
'''

def can_view(self, resource, field):
"""
Can this plugin view.
:param resource: param field:
:param field:
def image_info(self):
"""

def thumbnail(self):
"""
Thumbnail representation of the image.
Returns information about this plugin.
:return:
"""

def image(self):
def get_images(self, field_value, record, data_dict):
"""
Full size representation of the image.
:return:
Retrieve images from a record and present them as a list of dicts. Valid output
fields for each image:
- href (main/preview URL; required)
- thumbnail (URL for a smaller version of the image)
- download (URL for the downloadable version of the image)
- link (e.g. record URL, displayed in popup viewer)
- copyright (licence information; HTML)
- description (displayed under the image thumbnail; HTML)
- title (title in the popup viewer and image alt)
- record_id
:return: list of dicts
"""
31 changes: 4 additions & 27 deletions ckanext/gallery/theme/assets/js/gallery.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* Created by bens3 on 04/11/15.
*/

ckan.module('gallery', function (jQuery, _) {
var self;

Expand All @@ -20,9 +16,6 @@ ckan.module('gallery', function (jQuery, _) {
_openLightbox: function (e) {
var options = {
index: $(this).data('index'),
// onclose: function () {
// self._hideDownloadTooltip()
// },
onslide: function () {
self._onImageUpdate();
},
Expand All @@ -38,8 +31,10 @@ ckan.module('gallery', function (jQuery, _) {
$gallery.data('image', image);

// Update download link
$('#blueimp-gallery a.gallery-control-download').attr('href', image.href);
// $('#blueimp-gallery a.gallery-control-download').on('click', jQuery.proxy(self._downloadImage));
$('#blueimp-gallery a.gallery-control-download').attr(
'href',
image.download || image.href,
);

if (image.copyright) {
$gallery.find('.copyright').html(image.copyright);
Expand All @@ -52,24 +47,6 @@ ckan.module('gallery', function (jQuery, _) {
$gallery.find('.gallery-control-link').hide();
}
},
// _downloadImage: function(e){
// self.downloadFile('http://www.nhm.ac.uk/services/media-store/asset/2d63e01b999aaa0581397d9e629e4bc9f30677a7/contents/preview', function(blob) {
// saveAs(blob, "image.png");
// });
// e.stopPropagation();
// return false;
// },
// downloadFile: function(url, success){
// var xhr = new XMLHttpRequest();
// xhr.open('GET', url, true);
// xhr.responseType = "blob";
// xhr.onreadystatechange = function () {
// if (xhr.readyState == 4) {
// if (success) success(xhr.response);
// }
// };
// xhr.send(null);
// },
options: {
images: [],
},
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ changelog = "https://github.com/NaturalHistoryMuseum/ckanext-gallery/blob/main/C
[project.entry-points."ckan.plugins"]
gallery = "ckanext.gallery.plugins.gallery:GalleryPlugin"
gallery_image = "ckanext.gallery.plugins.image:GalleryImagePlugin"
gallery_iiif = "ckanext.gallery.plugins.iiif:GalleryIIIFPlugin"


[build-system]
Expand Down

0 comments on commit ee939cd

Please sign in to comment.