import os import re from logging import getLogger from pylons import config from pylons.i18n import _ from genshi.input import HTML from genshi.filters import Transformer import ckan.lib.helpers as h from ckan.lib.search import SearchError from ckan.lib.helpers import json from ckan import model from ckan.plugins import implements, SingletonPlugin from ckan.plugins import IRoutes from ckan.plugins import IConfigurable, IConfigurer from ckan.plugins import IGenshiStreamFilter from ckan.plugins import IPackageController from ckan.logic import ValidationError import html from ckanext.spatial.lib import save_package_extent,validate_bbox, bbox_query from ckanext.spatial.model.package_extent import setup as setup_model log = getLogger(__name__) def package_error_summary(error_dict): ''' Do some i18n stuff on the error_dict keys ''' def prettify(field_name): field_name = re.sub('(?' map_attribution = html.MAP_ATTRIBUTION_OSM elif map_type == 'os': js_library_links = '' map_attribution = '' # done in the js instead data = {'extent': extent, 'title': _(title), 'map_type': map_type, 'js_library_links': js_library_links, 'map_attribution': map_attribution, 'element_id': map_element_id} stream = stream | Transformer('body//div[@id="%s"]' % map_element_id)\ .append(HTML(body_html % data)) stream = stream | Transformer('head')\ .append(HTML(html.PACKAGE_MAP_EXTRA_HEADER % data)) stream = stream | Transformer('body')\ .append(HTML(html.PACKAGE_MAP_EXTRA_FOOTER % data)) return stream def update_config(self, config): here = os.path.dirname(__file__) template_dir = os.path.join(here, 'templates') public_dir = os.path.join(here, 'public') if config.get('extra_template_paths'): config['extra_template_paths'] += ','+template_dir else: config['extra_template_paths'] = template_dir if config.get('extra_public_paths'): config['extra_public_paths'] += ','+public_dir else: config['extra_public_paths'] = public_dir class CatalogueServiceWeb(SingletonPlugin): implements(IConfigurable) implements(IRoutes) def configure(self, config): config.setdefault("cswservice.title", "Untitled Service - set cswservice.title in config") config.setdefault("cswservice.abstract", "Unspecified service description - set cswservice.abstract in config") config.setdefault("cswservice.keywords", "") config.setdefault("cswservice.keyword_type", "theme") config.setdefault("cswservice.provider_name", "Unnamed provider - set cswservice.provider_name in config") config.setdefault("cswservice.contact_name", "No contact - set cswservice.contact_name in config") config.setdefault("cswservice.contact_position", "") config.setdefault("cswservice.contact_voice", "") config.setdefault("cswservice.contact_fax", "") config.setdefault("cswservice.contact_address", "") config.setdefault("cswservice.contact_city", "") config.setdefault("cswservice.contact_region", "") config.setdefault("cswservice.contact_pcode", "") config.setdefault("cswservice.contact_country", "") config.setdefault("cswservice.contact_email", "") config.setdefault("cswservice.contact_hours", "") config.setdefault("cswservice.contact_instructions", "") config.setdefault("cswservice.contact_role", "") config["cswservice.rndlog_threshold"] = float(config.get("cswservice.rndlog_threshold", "0.01")) def before_map(self, route_map): c = "ckanext.spatial.controllers.csw:CatalogueServiceWebController" route_map.connect("/csw", controller=c, action="dispatch_get", conditions={"method": ["GET"]}) route_map.connect("/csw", controller=c, action="dispatch_post", conditions={"method": ["POST"]}) return route_map def after_map(self, route_map): return route_map class HarvestMetadataApi(SingletonPlugin): ''' Harvest Metadata API (previously called "InspireApi") A way for a user to view the harvested metadata XML, either as a raw file or styled to view in a web browser. ''' implements(IRoutes) def before_map(self, route_map): controller = "ckanext.spatial.controllers.api:HarvestMetadataApiController" route_map.connect("/api/2/rest/harvestobject/:id/xml", controller=controller, action="display_xml") route_map.connect("/api/2/rest/harvestobject/:id/html", controller=controller, action="display_html") return route_map def after_map(self, route_map): return route_map