def load_themes(): vocab_file_path = _get_path('data-theme-skos.rdf', 'vocabularies') class Opts(object): def __init__(self, filename, name, format): self.filename = filename self.url = None #filename self.name = name self.format = format themes_loader.options = Opts(vocab_file_path, 'eu_themes', None) themes_loader.load() tag_localized = interfaces.get_localized_tag_name('ECON') Session.flush() assert tag_localized q = Session.query(Vocabulary).filter_by(name='eu_themes') vocab = q.first() assert vocab map_f = _get_path(MAPPING_FILE) voc_f = _get_path(EUROVOC_FILE) clear_subthemes() load_subthemes(map_f, voc_f) assert Subtheme.q().first()
def get_vocabulary_items(vocabulary_name, keys=None, lang=None): try: tag_list = toolkit.get_action('tag_list') items = tag_list(data_dict={ 'vocabulary_id': vocabulary_name, 'all_fields': True }) # log.warning(f'ITEMS {items}') tag_list = [] for full_item in items: tag_id = full_item['id'] tag_name = full_item['name'] if keys: for key in keys: if key == tag_name: localized_tag_name = interfaces.get_localized_tag_name( tag_name, lang) tag_list.append(localized_tag_name) else: localized_tag_name = interfaces.get_localized_tag_by_id( tag_id, lang) tag_list.append({ 'text': localized_tag_name, 'value': tag_name }) return tag_list except toolkit.ObjectNotFound: return []
def test_vocabulary_command(self): from ckanext.dcatapit.commands.vocabulary import load_from_file vocab_file_path = get_test_file(SKOS_THEME_FILE) load_from_file(filename=vocab_file_path, format='xml') tag_localized = interfaces.get_localized_tag_name('ECON') self.assertTrue(tag_localized)
def get_vocabulary_items(vocabulary_name, keys=None): try: tag_list = toolkit.get_action('tag_list') items = tag_list(data_dict={'vocabulary_id': vocabulary_name}) tag_list = [] for item in items: if keys: for key in keys: if key == item: localized_tag_name = interfaces.get_localized_tag_name( item) tag_list.append(localized_tag_name) else: localized_tag_name = interfaces.get_localized_tag_name(item) tag_list.append({'text': localized_tag_name, 'value': item}) return tag_list except toolkit.ObjectNotFound: return []
def get_package_resource_dcatapit_format_list(pkg_resources, fallback_lang=None): resources = [] if pkg_resources: resources = h.dict_list_reduce(pkg_resources, 'format') package_res = [] for resource in resources: localized_resource_name = interfaces.get_localized_tag_name( resource, fallback_lang) package_res.append(localized_resource_name) resources = package_res return resources
def dcatapit_string_to_localized_aggregated_themes(value, lang): """ Load json with subthemes and get localized subtheme names. Used in template """ data = dcatapit_string_to_aggregated_themes(value) out = [] for item in data: localized_theme = interfaces.get_localized_tag_name(item['theme'], lang=lang) outitem = {'theme': localized_theme, 'subthemes': []} from_model = Subtheme.for_theme(item['theme'], lang) for st, label in from_model: if st.uri in item['subthemes']: outitem['subthemes'].append(label) out.append(outitem) return out
def test_vocabulary_command(self): dcatapit_commands = themes_loader vocab_file_path = self._get_file_contents('data-theme-skos.rdf') options = BaseOptions({ 'filename': vocab_file_path, 'name': 'eu_themes', }) setattr(dcatapit_commands, 'options', options) dcatapit_commands.initdb() dcatapit_commands.load() tag_localized = interfaces.get_localized_tag_name('ECON') ok_(tag_localized)
def load_themes(): filename = get_test_file(SKOS_THEME_FILE) g = load_graph(path=filename) do_load(g, 'eu_themes') tag_localized = interfaces.get_localized_tag_name('ECON') Session.flush() assert tag_localized q = Session.query(Vocabulary).filter_by(name='eu_themes') vocab = q.first() assert vocab map_f = get_voc_file(MAPPING_FILE) voc_f = get_test_file(EUROVOC_FILE) clear_subthemes() load_subthemes(map_f, voc_f) assert Subtheme.q().first()
def after_search(self, search_results, search_params): ## ##################################################################### # This method moves the dcatapit fields into the extras array (needed for # the CKAN harvester). # Basically it dynamically reverts what is done by the # 'convert_from_extras' to allow harvesting this plugin's custom fields. ## ##################################################################### search_dicts = search_results.get('results', []) dcatapit_schema_fields = dcatapit_schema.get_custom_package_schema() for _dict in search_dicts: _dict_extras = _dict.get('extras', None) if not _dict_extras: _dict_extras = [] _dict['extras'] = _dict_extras for field in dcatapit_schema_fields: field_couple = field.get('couples', []) if len(field_couple) > 0: for couple in field_couple: self.manage_extras_for_search(couple, _dict, _dict_extras) else: self.manage_extras_for_search(field, _dict, _dict_extras) # remove holder info if pkg is local, use org as a source # see https://github.com/geosolutions-it/ckanext-dcatapit/pull/213#issuecomment-410668740 _dict['dataset_is_local'] = helpers.dataset_is_local(_dict['id']) if _dict['dataset_is_local']: _dict.pop('holder_identifier', None) _dict.pop('holder_name', None) self._update_pkg_rights_holder(_dict) lang = interfaces.get_language() facets = search_results['search_facets'] if 'dcat_theme' in facets: themes = facets['dcat_theme'] for item in themes['items']: name = item['name'] label = interfaces.get_localized_tag_name(tag_name=name, lang=lang) item['display_name'] = label return search_results
def get_vocabulary_item(vocabulary_name, key): return interfaces.get_localized_tag_name(key)