Ejemplo n.º 1
0
    def before_index(self, dataset_dict):
        '''
        Insert `dcat_theme` into solr
        '''
        
        extra_theme = dataset_dict.get("extras_theme" , None) or ''
        themes =  helpers.dump_dcatapit_subthemes(extra_theme)
        search_terms = [t['theme'] for t in themes]
        if search_terms:
            dataset_dict['dcat_theme'] = search_terms

        search_subthemes = []
        for t in themes:
            search_subthemes.extend(t.get('subthemes') or [])

        if search_terms:
            dataset_dict['dcat_theme'] = search_terms
        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')]))

        licenses = []
        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.warn('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(DEFAULT_ORG_CTX, {'id': org_id})
        else:
            org = {}
        if org.get('region'):

            # multilang!
            region_base = org['region']
            tags = interfaces.get_all_localized_tag_labels(region_base)
            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
Ejemplo n.º 2
0
    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