def is_datastore_field(value, context): ''' Make sure this field is an actual datastore field :raises: ckan.lib.navl.dictization_functions.Invalid for other inputs or non-whole values ''' fields = get_datastore_fields(context['resource'].id) for field in fields: if field['id'] in value: return value raise Invalid(_('Field {0} not in datastore'.format(value)))
def setup_template_variables(self, context, data_dict): """Setup variables available to templates""" records_per_page = config.get("ckanext.gallery.records_per_page", 32) current_page = request.params.get('page', 1) image_list = [] # Get list of datastore fields and format into a dict, ready for a form datastore_fields = get_datastore_fields(data_dict['resource']['id']) image_plugins = [] # Get gallery image plugins for plugin in p.PluginImplementations(IGalleryImage): image_info = plugin.image_info() # If we have resource type set, make sure the format of the resource matches # Otherwise continue to next record if image_info['resource_type'] and data_dict['resource']['format'].lower() not in image_info['resource_type']: continue image_info['plugin'] = plugin image_plugins.append(image_info) image_field = data_dict['resource_view'].get('image_field', None) image_plugin = data_dict['resource_view'].get('image_plugin', None) image_title_field = data_dict['resource_view'].get('image_title', None) # Set up template variables tpl_variables = { 'images': image_list, 'datastore_fields': [{'value': f['id'], 'text': f['id']} for f in datastore_fields], 'image_plugins': [{'value': f['plugin'].name, 'text': f['title']} for f in image_plugins], 'defaults': {}, 'resource_id': data_dict['resource']['id'], 'package_name': data_dict['package']['name'], 'has_title': True if image_title_field else False } # Load the images # Only try and load images, if an image field has been selected if image_field and image_plugin: offset = (int(current_page) - 1) * records_per_page # We only want to get records that have both the image field populated # So add filters to the datastore search params filters = self._get_request_filters() # TODO - This means the module will only work with NHM extension # Need to copy across the NHM code that handles this filters['_has_image'] = ['true'] params = { 'resource_id': data_dict['resource']['id'], 'limit': records_per_page, 'offset': offset, 'filters': filters, 'sort': '_id' } # Add the full text filter fulltext = request.params.get('q') if fulltext: params['q'] = fulltext # Try and use the solr search if it exists try: search_action = toolkit.get_action('datastore_solr_search') # Otherwise fallback to default except KeyError: search_action = toolkit.get_action('datastore_search') # Perform the actual search context = {'model': model, 'session': model.Session, 'user': c.user or c.author} data = search_action(context, params) item_count = data.get('total', 0) # Get the selected gallery image plugin plugin = p.get_plugin(image_plugin) if plugin: for record in data['records']: image_title = record.get(image_title_field, "") image_defaults = { 'title': image_title, 'record_id': record['_id'], 'description': '' } field_value = record.get(image_field, None) if field_value: images = plugin.get_images(field_value, record, data_dict) for image in images: image_default_copy = copy.copy(image_defaults) # Merge in the plugin image settings to the default image # Using an copy so the defaults do not change for multiple images image_default_copy.update(image) image_list.append(image_default_copy) page_params = { 'collection': data['records'], 'page': current_page, 'url': self.pager_url, 'items_per_page': records_per_page, 'item_count': item_count, } # Add filter params to page links for key in ['q', 'filters']: value = request.params.get(key) if value: page_params[key] = value tpl_variables['page'] = h.Page(**page_params) return tpl_variables