def load(g, name, uri, eurovoc):

    if name == LICENSES_NAME:
        ret = {'licenses_deleted': License.count()}
        clear_licenses()
        load_licenses(g)
        ret['licenses_created'] = License.count()
        Session.commit()
        return ret

    if name == SUBTHEME_NAME:
        ret = {'subthemes_deleted': Subtheme.count()}
        clear_subthemes()
        load_subthemes(None, eurovoc, themes_g=g)
        ret['subthemes_created'] = Subtheme.count()
        Session.commit()
        return ret

    return do_load(g, name)
Example #2
0
def get_localized_subthemes(subthemes):

    q = Subtheme.get_localized(*subthemes)
    out = {}
    for item in q:
        lang, label = item # .lang, item.label
        try:
            out[lang].append(label)
        except KeyError:
            out[lang] = [label]
    return out
Example #3
0
def get_localized_subtheme(subtheme, lang):
    localized = Subtheme.get_any(subtheme)
    if not localized:
        return
    return localized.get_name(lang)
def add_subtheme(eurovoc, theme_ref, subtheme_ref, parent=None):
    def info(theme, inst):
        return f"T:{theme} id:{inst.id:4} dpth:{inst.depth} par:{inst.parent_id or '':5} P:{inst.path}"

    theme = Subtheme.normalize_theme(theme_ref)
    existing = Subtheme.q().filter_by(uri=str(subtheme_ref)).first()
    theme_tag = ThemeToSubtheme.get_tag(theme)

    # several themes may refer to this subtheme, so we'll just return
    # exising instance
    if existing:
        if not theme_tag in existing.themes:
            existing.themes.append(theme_tag)
        Subtheme.Session.flush()
        log.error(
            f'Subtheme {subtheme_ref} already exists - {info(theme, existing)}. Skipping'
        )
        return existing

    labels = {}
    for pref_label in eurovoc.objects(subtheme_ref, SKOS.prefLabel):
        labels[pref_label.language] = str(pref_label)
    if not labels:
        log.error(
            f'No labels found in EUROVOC for subtheme {subtheme_ref}. Skipping'
        )
        return
    version = eurovoc.value(subtheme_ref, OWL.versionInfo) or ''
    identifier = eurovoc.value(subtheme_ref, DCT.identifier) or ''
    default_label = labels[DEFAULT_LANG]
    inst = Subtheme(version=str(version),
                    identifier=str(identifier),
                    uri=str(subtheme_ref),
                    default_label=default_label,
                    parent_id=parent.id if parent else None,
                    depth=parent.depth + 1 if parent else 0)
    inst.update_path()

    inst.add()
    Subtheme.Session.flush()

    log.info(f"Added sub {info(theme, inst)}")

    if parent is None:
        inst.parent_id = inst.id

    theme_m = ThemeToSubtheme(tag_id=theme_tag.id, subtheme_id=inst.id)
    theme_m.add()

    for lang, label in labels.items():
        l = SubthemeLabel(subtheme_id=inst.id, lang=lang, label=label)
        l.add()
    Subtheme.Session.flush()
    # handle children

    # make sure that we have all the intermediate items from the subtheme upto the main theme
    for child in eurovoc.objects(subtheme_ref, SKOS.hasTopConcept):
        try:
            add_subtheme(eurovoc, theme_ref, child, inst)
        except IntegrityError as e:
            # same parent may have already been added
            log.error(
                f'Not adding subtheme parent "{child}" for "{theme_ref}" and sub "{subtheme_ref}"'
            )

    return inst