def test_org_context(): # ensure ctx are separated, and no data is leaked between them ctx1 = helpers.get_org_context() ctx1['test'] = True ctx2 = helpers.get_org_context() assert ctx2.get('test') is None
def _update_pkg_rights_holder(self, pkg_dict, org=None): #log.error(helpers.json_dump(pkg_dict)) if pkg_dict.get('type') != 'dataset': return pkg_dict if not (pkg_dict.get('holder_identifier') and pkg_dict.get('holder_name')): if not pkg_dict.get('owner_org'): return pkg_dict if org is None: get_org = toolkit.get_action('organization_show') ctx = get_org_context() # force multilang use ctx['for_view'] = True org = get_org( ctx, { 'id': pkg_dict['owner_org'], 'include_tags': False, 'include_users': False, 'include_groups': False, 'include_extras': True, 'include_followers': False, 'include_datasets': False, }) pkg_dict['holder_name'] = org['title'] pkg_dict['holder_identifier'] = org.get('identifier') or None return pkg_dict
def before_index(self, dataset_dict): ''' Insert `dcat_theme` into solr ''' extra_theme = dataset_dict.get(f'extras_{FIELD_THEMES_AGGREGATE}', None) or '' aggr_themes = helpers.dcatapit_string_to_aggregated_themes(extra_theme) search_terms = [t['theme'] for t in aggr_themes] if search_terms: dataset_dict['dcat_theme'] = search_terms search_subthemes = [] for t in aggr_themes: search_subthemes.extend(t.get('subthemes') or []) if search_subthemes: dataset_dict['dcat_subtheme'] = search_subthemes localized_subthemes = interfaces.get_localized_subthemes(search_subthemes) for lang, subthemes in localized_subthemes.items(): dataset_dict['dcat_subtheme_{}'.format(lang)] = subthemes ddict = json.loads(dataset_dict['data_dict']) resources = ddict.get('resources') or [] _licenses = list(set([r.get('license_type') for r in resources if r.get('license_type')])) for l in _licenses: lic = License.get(l) if lic: for loclic in lic.get_names(): lname = loclic['name'] lang = loclic['lang'] if lname: dataset_dict['resource_license_{}'.format(lang)] = lname else: log.warning('Bad license: license not found: %r ', l) dataset_dict['resource_license'] = _licenses org_id = dataset_dict['owner_org'] organization_show = plugins.toolkit.get_action('organization_show') if org_id: org = organization_show(get_org_context(), {'id': org_id, 'include_tags': False, 'include_users': False, 'include_groups': False, 'include_extras': True, 'include_followers': False, 'include_datasets': False, }) else: org = {} if org.get('region'): # multilang values # note region can be in {val1,val2} notation for multiple values region_base = org['region'] if not isinstance(region_base, (list, tuple,)): region_base = region_base.strip('{}').split(',') tags = {} for region_name in region_base: ltags = interfaces.get_all_localized_tag_labels(region_name) for tlang, tvalue in ltags.items(): try: tags[tlang].append(tvalue) except KeyError: tags[tlang] = [tvalue] for lang, region in tags.items(): dataset_dict['organization_region_{}'.format(lang)] = region self._update_pkg_rights_holder(dataset_dict, org=org) return dataset_dict