def create_taxonomy_term(slug, cz_keyword, en_keyword, type):
    tax = Taxonomy.get("subjects")
    parent_term = tax.get_term(type)
    if not parent_term:
        parent_term = tax.create_term(slug=type)
        db.session.add(parent_term)
        db.session.commit()
    extra_data = {
        "title": [{
            "value": cz_keyword,
            "lang": "cze"
        }, {
            "value": en_keyword,
            "lang": "eng"
        }],
        "approved":
        False
    }
    term = tax.get_term(slug=slug)
    if not term:
        term = parent_term.create_term(slug=slug, extra_data=extra_data)
        db.session.add(term)
        db.session.commit()
        current_flask_taxonomies_es.set(term, timestamp=datetime.utcnow())
    return term
def transform_degree_discipline(paths, el, results, phase, **kwargs):
    value_array = el["cs_CZ"][0]["value"]
    assert len(value_array) == 1
    tax = Taxonomy.get("studyfields")
    results[-1]["studyField"] = studyfield_ref(value_array[0].strip(),
                                               tax)["studyField"]
    return OAITransformer.PROCESSED
Exemplo n.º 3
0
def test_db_search(app, db):
    tax = Taxonomy.get("studyfields", required=True)
    fields = db_search("Arteterapie", tax, json_address=("title", 0, "value"))
    assert isinstance(fields, list)
    for field in fields:
        assert "slug" in field.keys()
        assert "title" in field.keys()
        assert "links" in field.keys()
Exemplo n.º 4
0
def get_ref(slug, code):
    res = current_flask_taxonomies_es.get(code, slug)
    if len(res) > 0 and isinstance(res, dict):
        return get_ref_es(res)
    tax = Taxonomy.get(code)
    term = tax.get_term(slug)
    if term is None:
        return None
    return {"$ref": link_self(tax.slug, term)}
Exemplo n.º 5
0
def get_taxonomy_term(code=None, slug=None):
    try:
        taxonomy = Taxonomy.get(code)
        term = taxonomy.find_term(slug)
    except:
        term = None
        taxonomy = None
    return jsonify_taxonomy_term(term, taxonomy.code, term.tree_path,
                                 term.parent.tree_path or '')
def doctype(self, key, values):
    doc_type = values[0].get('a')
    es_result = current_flask_taxonomies_es.get("doctypes", doc_type)
    if es_result:
        return get_ref_es(es_result)
    doctype_tax = Taxonomy.get("doctypes", required=True)
    doctype = doctype_tax.descendants.filter(
        TaxonomyTerm.slug == doc_type).one()

    return {"$ref": link_self(doctype_tax.slug, doctype)}
def study_programme_field(self, key, value):
    """Study programme."""
    source = value.get("a")
    tax = Taxonomy.get("studyfields", required=True)
    if "/" not in source:
        return studyfield_ref(source.strip(), tax)
    else:
        programme, field = source.split("/", maxsplit=1)
        field = field.strip()
        return studyfield_ref(field, tax)
Exemplo n.º 8
0
def get_ref(slug):
    res = current_flask_taxonomies_es.get("accessRights", slug)
    if len(res) > 0 and isinstance(res, dict):
        return get_ref_es(res)
    tax = Taxonomy.get("accessRights", required=True)
    res = tax.get_term(slug)
    if res is None:
        return None
    return {
        "$ref": link_self(res.slug, tax)
    }
def get_role(role):
    res = current_flask_taxonomies_es.get("contributor-type", role)
    if not res:
        tax = Taxonomy.get("contributor-type")
        term = tax.get_term(role)
        if term:
            return {
                "$ref": link_self(tax.slug, term)
            }
        else:
            return
    return get_ref_es(res)
def provider(self, key, value):
    nusl_slug = value.get("a")
    slug = get_slug(nusl_slug).strip()
    if not slug:
        return
    provider = current_flask_taxonomies_es.get('institutions', slug)
    if provider:
        return get_ref_es(provider)
    tax = Taxonomy.get("institutions", required=True)
    provider = tax.descendants.filter(TaxonomyTerm.slug == slug).one()  # viz:
    # https://stackoverflow.com/questions/29974143/python-sqlalchemy-and-postgres-how-to-query-a
    # -json-element
    return {"$ref": link_self(tax.slug, provider)}
Exemplo n.º 11
0
def get_taxonomy_term(code=None, slug=None, timestamp=None):
    try:
        taxonomy = Taxonomy.get(code)
        term = taxonomy.find_term(slug)
        parents = [format_ancestor(x) for x in term.ancestors]
    except:
        traceback.print_exc()
        raise ValueError("The taxonomy term does not exist.")
    resp = _fix_links(
        jsonify_taxonomy_term(term,
                              taxonomy.code,
                              term.tree_path,
                              term.parent.tree_path or '',
                              parents,
                              timestamp=timestamp))
    return resp
def fix_keywords(data):
    """
    Converts keywords that match PSH to subject field.
    :param data: GroupableOrderedDict of all fields
    :return: Fixed GroupableOrderedDict
    """
    tax = Taxonomy.get("subject")
    data = dict(data)
    subject_tuple = []
    if "653__" in data:
        values = data.get("653__")
        values = split_keywords(values)
        data["653__"] = values
        delete_position = []
        for idx, value in enumerate(values):
            parsed_keyword = value.get("a")
            psh_list_terms = find_in_json_contains(parsed_keyword, tax,
                                                   "title").all()
            if len(psh_list_terms) == 0:
                psh_list_terms = find_in_json_contains(parsed_keyword, tax,
                                                       "altTitle").all()
            term = psh_term_filter(psh_list_terms, parsed_keyword)
            if len(psh_list_terms) == 0 or term is None:
                continue
            record_dict = {"2": "psh", "7": term.slug, "a": parsed_keyword}
            subject_tuple.append(GroupableOrderedDict(
                OrderedDict(record_dict)))
            delete_position.append(idx)

        if len(delete_position) > 0:
            keyword_list = list(data["653__"])
            for position in reversed(delete_position):
                del keyword_list[position]
            data["653__"] = tuple(keyword_list)

        if len(subject_tuple) > 0:
            if "650_7" in data:
                old_subject_tuple = list(data["650_7"])
                subject_tuple.extend(old_subject_tuple)
            data["650_7"] = tuple(subject_tuple)
    if "6530_" in data:
        values = data.get("6530_")
        values = split_keywords(values)
        data["6530_"] = values
    if "__order__" in data:
        del data["__order__"]
    return GroupableOrderedDict(OrderedDict(data))
Exemplo n.º 13
0
def language(self, key, value):
    """Language Code."""
    languages = []
    tax = Taxonomy.get("languages")
    if not value.get("a"):
        return None
    if "a" in value:
        lang = get_lang("a", value)
        if lang is None:
            return None
        languages.append(get_ref(lang, tax))
    if "b" in value:
        lang = get_lang("b", value)
        if lang is None:
            return None
        languages.append(get_ref(lang, tax))
    return languages
Exemplo n.º 14
0
def get_taxonomy_term(code=None, slug=None):
    resp = before_taxonomy_jsonresolve.send(None, code=code, slug=slug)
    for r in resp:
        if r[1] is not None:
            return r[1]
    try:
        taxonomy = Taxonomy.get(code)
        term = taxonomy.find_term(slug)
        parents = [format_ancestor(x) for x in term.ancestors]
    except:
        traceback.print_exc()
        raise ValueError("The taxonomy term does not exist.")
    resp = jsonify_taxonomy_term(term, taxonomy.code, term.tree_path,
                                 term.parent.tree_path or '', parents)
    after_taxonomy_jsonresolve.send(None,
                                    code=code,
                                    slug=slug,
                                    taxonomy_term=resp)
    return resp
Exemplo n.º 15
0
def create_update_taxonomy(data, drop):
    tax_dict = next(convert_data_to_dict(data))
    if 'code' not in tax_dict:
        raise ValueError('Taxonomy does not contain "code"')
    code = tax_dict.pop('code')
    taxonomy = Taxonomy.get(code=code)
    if taxonomy and drop:
        db.session.delete(taxonomy)
        db.session.commit()
        taxonomy = None

    if taxonomy:
        merged_dict = taxonomy.extra_data
        merged_dict.update(tax_dict)
        taxonomy.update(extra_data=merged_dict)
    else:
        taxonomy = Taxonomy.create_taxonomy(code, tax_dict)
    db.session.commit()
    return taxonomy
def find_uni_by_name(name,
                     parent_name=None,
                     parent_slug=None,
                     query_type="term"):
    if query_type == "match":
        query = Q("term", taxonomy__keyword=TAXONOMY_CODE) & Q(
            query_type, title__value=name)
    else:
        query = Q("term", taxonomy__keyword=TAXONOMY_CODE) & Q(
            query_type, title__value__keyword=name)
    if parent_slug or parent_name:
        if parent_slug:
            parent_query = Q("term", ancestors__slug__keyword=parent_slug)
        else:
            parent_query = Q("term",
                             ancestors__title__value__keyword=parent_slug)
        query = Q('bool', must=[parent_query, query])
    if query_type == "match":
        results = current_flask_taxonomies_es.search(query, match=True)
    else:
        results = current_flask_taxonomies_es.search(query)
    if not results:
        if query_type == "match":
            query1 = Q(query_type, formerNames=name)
            query2 = Q(query_type, aliases=name)
        else:
            query1 = Q(query_type, formerNames__keyword=name)
            query2 = Q(query_type, aliases__keyword=name)
        q = Q('bool', should=[query1, query2])
        if query_type == "match":
            results = current_flask_taxonomies_es.search(q, match=True)
        else:
            results = current_flask_taxonomies_es.search(q)
    if not results:
        tax = Taxonomy.get(TAXONOMY_CODE)
        results = db_search(name, tax, json_address=("title", 0, "value"))
    if results:
        return results[0]
Exemplo n.º 17
0
def delete_taxonomy(code):
    t = Taxonomy.get(code)
    db.session.delete(t)
    db.session.commit()
def get_taxonomy_term(type: str):
    taxonomy = Taxonomy.get("subjects")
    taxonomy_term = taxonomy.get_term(type.lower())
    return taxonomy_term
def subject(self, key, values):
    """Subject."""
    taxonomy = Taxonomy.get("subjects")
    return get_subject(taxonomy, values)
Exemplo n.º 20
0
def set_(taxonomy: str, slug: str):
    taxonomy = Taxonomy.get(taxonomy)
    term = taxonomy.get_term(slug)
    current_flask_taxonomies_es.set(term)
Exemplo n.º 21
0
def delete_taxonomy(code):
    t = Taxonomy.get(code)
    current_flask_taxonomies.delete_taxonomy(t)