def dge_api_swagger_url(): ''' Returns endpoint of swagger, set in ckanext.dge.api.swagger.url config property. Returns emtpy string if property does not exist. ''' return h.url_for_static_or_external( config.get('ckanext.dge.api.swagger.url', '').lower())
def dge_sparql_yasgui_endpoint(): ''' Returns endpoint of sparql, set in ckanext.dge.api.yasgui.url config property. Returns emtpy string if property does not exist. ''' return h.url_for_static_or_external( config.get('ckanext.dge.sparql.yasgui.endpoint', '').lower())
def _resource_persistent_identifier(data_dict): """If it is necessary, create a persistent handle identifier and add it to resource metadata. :param data_dict: The full package dictionary :type id: string :rtype: list of dicts """ hdl = HandleService() for res in data_dict.get('resources', None): res_pid = res.get(hdl.resource_field, None) if not hdl.hdl_exists_from_url(res_pid): # If there is no res_pid -> Create new res_pid if not res_pid: res_pid = hdl.create_unique_hdl_url() # Create Link for package res_link = h.url_for_static_or_external(controller='package', action='resource_read', id=res.get('package_id'), resource_id=res.get('id'), qualified=True) # Register package link hdl.register_hdl_url(res_pid, res_link) res[hdl.resource_field] = res_pid
def _package_persistent_identifier(data_dict): """If it is necessary, create a persistent handle identifier and add it to package metadata. :param data_dict: The full package dictionary :type id: string :rtype: list of dicts """ hdl = HandleService() # Get pkg pid, None if there is no pid pkg_pid = data_dict.get(hdl.package_field, None) if not hdl.hdl_exists_from_url(pkg_pid): # If there is no pkg_pid -> Create new pkg_pid if not pkg_pid: pkg_pid = hdl.create_unique_hdl_url() # Create Link for package pkg_link = h.url_for_static_or_external(controller='package', action='read', id=data_dict.get('id'), qualified=True) # Register package link hdl.register_hdl_url(pkg_pid, pkg_link) data_dict[hdl.package_field] = pkg_pid
def current_disqus_url(cls, ): '''If `disqus.disqus_url` is defined, return a fully qualified url for the current page with `disqus.disqus_url` as the base url,''' if cls.disqus_url is None: return None return url_for_static_or_external(request.environ['CKAN_CURRENT_URL'], qualified=True, host=cls.disqus_url)
def url_for_wet(self, *args, **kw): file = args[0] or '' theme = kw.get('theme', False) if not WET_URL: return h.url_for_static_or_external( (self.wet_theme() if theme else 'wet-boew') + file ) return WET_URL + '/' + (self.wet_theme() if theme else 'wet-boew') + file
def url_for_wet(self, *args, **kw): file = args[0] or '' theme = kw.get('theme', False) if not WET_URL: return h.url_for_static_or_external( (self.wet_theme() if theme else 'wet-boew') + file) return WET_URL + '/' + (self.wet_theme() if theme else 'wet-boew') + file
def is_resource_to_large(url): url = h.url_for_static_or_external(url) resp = requests.head(url) length = int(resp.headers.get('content-length', 0)) if not length: range = resp.headers.get('content-range') if not range: # unable to identify resource's size return False try: from_, to_ = range.split().pop().split('/').pop(0).split('-') length = int(to_) - int(from_) except Exception: return False size = length / 1024 / 1024 max_size = int(config.get('pdf_view.max_preview_size', 1024)) return size > max_size
def include_asset(name: str) -> None: from ckan.lib.helpers import url_for_static_or_external try: if not g.webassets: raise AttributeError(u'WebAssets not initialized yet') except AttributeError: g.webassets = _make_asset_collection() if name in g.webassets[u'included']: return assert env try: bundle: Any = env[name] except KeyError: logger.error(u'Trying to include unknown asset: <{}>'.format(name)) return deps: list[str] = bundle.extra.get(u'preload', []) # Using DFS may lead to infinite recursion(unlikely, because # extensions rarely depends on each other), so there is a sense to # memoize visited routes. # TODO: consider infinite loop prevention for assets that depends # on each other for dep in deps: include_asset(dep) # Add `site_root` if configured urls = [url_for_static_or_external(url) for url in bundle.urls()] type_ = None for url in urls: link = url.split(u'?')[0] if link.endswith(u'.css'): type_ = u'style' break elif link.endswith(u'.js'): type_ = u'script' break else: logger.warn(u'Undefined asset type: {}'.format(urls)) return g.webassets[type_].extend(urls) g.webassets[u'included'].add(name)
def include_asset(name): from ckan.lib.helpers import url_for_static_or_external try: if not g.webassets: raise AttributeError(u'WebAssets not initialized yet') except AttributeError: g.webassets = _make_asset_collection() if name in g.webassets[u'included']: return try: bundle = env[name] except KeyError: logger.error(u'Trying to include unknown asset: <{}>'.format(name)) return deps = bundle.extra.get(u'preload', []) # Using DFS may lead to infinite recursion(unlikely, because # extensions rarely depends on each other), so there is a sense to # memoize visited routes. # TODO: consider infinite loop prevention for assets that depends # on each other for dep in deps: include_asset(dep) # Add `site_root` if configured urls = [url_for_static_or_external(url) for url in bundle.urls()] type_ = None for url in urls: link = url.split(u'?')[0] if link.endswith(u'.css'): type_ = u'style' break elif link.endswith(u'.js'): type_ = u'script' break else: logger.warn(u'Undefined asset type: {}'.format(urls)) return g.webassets[type_].extend(urls) g.webassets[u'included'].add(name)
def test_url_for_static_or_external_converts_unicode_to_strings(self): url = u'/ckan.jpg' assert isinstance(h.url_for_static_or_external(url), str)
def test_url_for_static_or_external_works_with_protocol_relative_url(self): url = '//assets.ckan.org/ckan.jpg' eq_(h.url_for_static_or_external(url), url)
def test_url_for_static_or_external_adds_starting_slash_if_needed(self): slashless_url = 'ckan.jpg' url = '/' + slashless_url eq_(h.url_for_static_or_external(slashless_url), url)
def test_url_for_static_or_external(self): url = '/assets/ckan.jpg' eq_(h.url_for_static_or_external(url), url)
def test_url_for_static_or_external(url): assert h.url_for_static_or_external(url) == url assert isinstance(h.url_for_static_or_external(url), str)
def test_url_for_static_or_external_works_with_external_urls(self): url = 'http://assets.ckan.org/ckan.jpg' eq_(h.url_for_static_or_external(url), url)
def test_url_for_static_or_external(url): generated = h.url_for_static_or_external(url) assert generated == url assert isinstance(generated, str)