From 519a3467908a69e15e97571fe1ef9e556d4b0d66 Mon Sep 17 00:00:00 2001 From: Alice Butcher Date: Mon, 15 Jan 2024 12:36:37 +0000 Subject: [PATCH 1/4] refactor: implement the correct interface methods BREAKING CHANGE: implement the correct interface methods None of the interface methods (info, can_view, thumbnail, image) actually seem to be used anywhere, while two methods that are called elsewhere (image_info and get_images) were never actually defined on the interface. This removes the unused ones and adds the two used ones. Theoretically, this shouldn't break anything because none of this was doing anything in the first place (if it were, we'd be seeing errors), but I've made it a breaking change just in case. --- ckanext/gallery/plugins/interfaces.py | 40 ++++++++++++--------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/ckanext/gallery/plugins/interfaces.py b/ckanext/gallery/plugins/interfaces.py index 19db5c3..3d0ffd5 100644 --- a/ckanext/gallery/plugins/interfaces.py +++ b/ckanext/gallery/plugins/interfaces.py @@ -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 """ From b00ca8ac033a84011071b4604dbc8dc04aa200b0 Mon Sep 17 00:00:00 2001 From: Alice Butcher Date: Mon, 15 Jan 2024 13:24:44 +0000 Subject: [PATCH 2/4] chore: remove commented sections from js --- ckanext/gallery/theme/assets/js/gallery.js | 26 ---------------------- 1 file changed, 26 deletions(-) diff --git a/ckanext/gallery/theme/assets/js/gallery.js b/ckanext/gallery/theme/assets/js/gallery.js index 8449a68..d3dd40a 100644 --- a/ckanext/gallery/theme/assets/js/gallery.js +++ b/ckanext/gallery/theme/assets/js/gallery.js @@ -1,7 +1,3 @@ -/** - * Created by bens3 on 04/11/15. - */ - ckan.module('gallery', function (jQuery, _) { var self; @@ -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(); }, @@ -39,7 +32,6 @@ ckan.module('gallery', function (jQuery, _) { // 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)); if (image.copyright) { $gallery.find('.copyright').html(image.copyright); @@ -52,24 +44,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: [], }, From bc7df2472aa2e95e40b3ef73f370fef274b35e29 Mon Sep 17 00:00:00 2001 From: Alice Butcher Date: Mon, 15 Jan 2024 13:37:54 +0000 Subject: [PATCH 3/4] feat: use download url for download button if available --- ckanext/gallery/theme/assets/js/gallery.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ckanext/gallery/theme/assets/js/gallery.js b/ckanext/gallery/theme/assets/js/gallery.js index d3dd40a..6ff81a0 100644 --- a/ckanext/gallery/theme/assets/js/gallery.js +++ b/ckanext/gallery/theme/assets/js/gallery.js @@ -31,7 +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').attr( + 'href', + image.download || image.href, + ); if (image.copyright) { $gallery.find('.copyright').html(image.copyright); From 50b27a621e67039bca8076b24e6b84580087f485 Mon Sep 17 00:00:00 2001 From: Alice Butcher Date: Mon, 15 Jan 2024 17:20:20 +0000 Subject: [PATCH 4/4] feat: add plugin for iiif images --- README.md | 2 +- ckanext/gallery/plugins/iiif.py | 78 +++++++++++++++++++++++++++++++++ pyproject.toml | 1 + 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 ckanext/gallery/plugins/iiif.py diff --git a/README.md b/README.md index bcb67f8..c519717 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ _A CKAN extension for a dataset gallery view._ # Overview -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). diff --git a/ckanext/gallery/plugins/iiif.py b/ckanext/gallery/plugins/iiif.py new file mode 100644 index 0000000..7cbea23 --- /dev/null +++ b/ckanext/gallery/plugins/iiif.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index d8dd756..5f4b1c6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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]