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 test_resolve_json(app, db, child_term, child_term_dict):
    current_flask_taxonomies_es.set(child_term)
    time.sleep(1)
    term = _resolve_json(child_term.taxonomy.slug, child_term.slug)
    child_term_dict = child_term_dict
    del child_term_dict["taxonomy"]
    assert term == child_term_dict
def test_set_remove_3(app, db, root_taxonomy, sample_term):
    current_flask_taxonomies_es.set(sample_term)
    time.sleep(1)
    with pytest.raises(InvalidTermIdentification):
        current_flask_taxonomies_es.remove(
            taxonomy_code=sample_term.taxonomy.slug,
        )
def test_get_ref(app, db, root_taxonomy, child_term, child_term_dict):
    current_flask_taxonomies_es.set(child_term)
    time.sleep(1)
    res = current_flask_taxonomies_es.get(root_taxonomy.slug, child_term.slug)
    res2 = current_flask_taxonomies_es.get_ref(res["links"]["self"])
    del res['date_of_serialization']
    del res2['date_of_serialization']
    assert res == res2 == child_term_dict
def test_set_remove_2(app, db, root_taxonomy, sample_term):
    current_flask_taxonomies_es.set(sample_term)
    time.sleep(1)
    current_flask_taxonomies_es.remove(
        taxonomy_code=sample_term.taxonomy.slug,
        slug=sample_term.slug
    )
    time.sleep(1)
    result = current_flask_taxonomies_es.get(root_taxonomy.slug, sample_term.slug)
    assert result is None
def test_set_get_remove(app, db, root_taxonomy, sample_term, sample_term_dict):
    current_flask_taxonomies_es.set(sample_term)
    time.sleep(1)
    result = current_flask_taxonomies_es.get(root_taxonomy.slug, sample_term.slug)
    del result['date_of_serialization']
    assert result == sample_term_dict
    current_flask_taxonomies_es.remove(sample_term)
    time.sleep(1)
    result = current_flask_taxonomies_es.get(root_taxonomy.slug, sample_term.slug)
    assert result is None
def test_list(app, db, root_taxonomy, sample_term, sample_term_2, sample_term_dict,
              sample_term_2_dict):
    current_flask_taxonomies_es.set(sample_term)
    current_flask_taxonomies_es.set(sample_term_2)
    time.sleep(1)
    results = current_flask_taxonomies_es.list("root")
    new_results = []
    for result in results:
        del result["date_of_serialization"]
        new_results.append(result)
    assert new_results == [sample_term_dict, sample_term_2_dict]
Exemple #8
0
def test_before_taxonomy_jsonresolve(app, db, sample_term, sample_term_dict):
    taxonomy_code = sample_term.taxonomy.slug
    slug = sample_term.slug
    current_flask_taxonomies_es.set(sample_term)
    time.sleep(1)
    taxonomy_dict = current_flask_taxonomies_es.get(taxonomy_code, slug)
    del taxonomy_dict['date_of_serialization']
    assert taxonomy_dict == sample_term_dict
    resp = before_taxonomy_jsonresolve.send(None,
                                            code=taxonomy_code,
                                            slug=slug)
    sample_term_dict = sample_term_dict
    del sample_term_dict["taxonomy"]
    assert resp[0][1] == sample_term_dict
Exemple #9
0
def test_after_taxonomy_term_deleted(app, db, sample_term, sample_term_dict):
    taxonomy_code = sample_term.taxonomy.slug
    slug = sample_term.slug
    current_flask_taxonomies_es.set(sample_term)
    time.sleep(1)
    taxonomy_dict = current_flask_taxonomies_es.get(taxonomy_code, slug)
    del taxonomy_dict['date_of_serialization']
    assert taxonomy_dict == sample_term_dict
    after_taxonomy_term_deleted.connect(delete_taxonomy_term)
    after_taxonomy_term_deleted.send(term=sample_term)
    time.sleep(1)
    term_dict = current_flask_taxonomies_es.get(taxonomy_code, slug)
    time.sleep(1)
    assert term_dict is None
def test_reindex(app, db, root_taxonomy, sample_term, child_term):
    current_flask_taxonomies_es.set(sample_term)
    time.sleep(1)
    term1 = current_flask_taxonomies_es.get(sample_term.taxonomy.slug, sample_term.slug)
    timestamp1 = term1['date_of_serialization']
    time.sleep(1)
    reindex_timestamp = current_flask_taxonomies_es.reindex()
    time.sleep(1)
    term2 = current_flask_taxonomies_es.get(sample_term.taxonomy.slug, sample_term.slug)
    time.sleep(1)
    timestamp2 = term2["date_of_serialization"]
    assert timestamp1 != timestamp2
    time.sleep(1)
    terms = current_flask_taxonomies_es.list("root")
    assert len(terms) == 2
    for term in terms:
        assert term['date_of_serialization'] == str(reindex_timestamp)
def studyfield_ref(study_title, tax):
    # https://docs.sqlalchemy.org/en/13/dialects/postgresql.html#sqlalchemy.dialects.postgresql.JSON
    # https://github.com/sqlalchemy/sqlalchemy/issues/3859  # issuecomment-441935478
    if study_title is None:
        return
    fields = es_search_title(study_title, tax.slug)
    if len(fields) == 0:
        fields = db_search(study_title,
                           tax,
                           json_address=("title", 0, "value"))
    if len(fields) > 0:
        fields_filtered = [
            field for field in fields if field["slug"].startswith("P")
        ]
        if len(fields_filtered) > 0:
            fields = fields_filtered
        return {
            "studyField": [get_ref_es(field) for field in fields],
        }
    else:
        result = studyfield_ref(aliases(study_title), tax)
        if result:
            return result
        else:
            slug = "O_" + slugify(study_title)
            extra_data = {
                "title": [{
                    "value": study_title,
                    "lang": "cze"
                }],
                "approved": False
            }
            if len(slug) > 64:
                slug = slug[:64]
            term = tax.get_term(slug=slug)
            if term:
                terms = [term]
                fields = jsonify_fields(terms)
                return {
                    "studyField": [get_ref_es(field) for field in fields],
                }
            term = tax.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 studyfield_ref(study_title, tax)
def test_set_get_child_term(app, db, root_taxonomy, child_term):
    current_flask_taxonomies_es.set(child_term)
    time.sleep(1)
    result = current_flask_taxonomies_es.get(root_taxonomy.slug, child_term.slug)
    del result['date_of_serialization']
    assert result["path"] == '/1/3'
Exemple #13
0
def set_(taxonomy: str, slug: str):
    taxonomy = Taxonomy.get(taxonomy)
    term = taxonomy.get_term(slug)
    current_flask_taxonomies_es.set(term)