예제 #1
0
def jsonify_fields(fields):
    new_fields = []
    for field in fields:
        new_fields.append(
            get_taxonomy_term(code=field.taxonomy.slug, slug=field.slug))
    fields = new_fields
    return fields
예제 #2
0
def get_term(slug):
    res = current_flask_taxonomies_es.get('licenses', slug)
    if not res:
        res = get_taxonomy_term(code="licenses", slug=slug)
    if not res:
        res = current_flask_taxonomies_es.get('licenses', "copyright")
    return get_ref_es(res)
def get_ref_by_provider(provider):
    provider_slug = get_slug(provider)
    if not provider_slug:
        return
    provider = current_flask_taxonomies_es.get('institutions', provider_slug)
    if provider:
        return provider
    return get_taxonomy_term(TAXONOMY_CODE, provider_slug)
예제 #4
0
    def set(self, taxonomy_term: TaxonomyTerm, timestamp=None) -> None:
        """
        Save serialized taxonomy into Elasticsearch. It create new or update old taxonomy record.

        :param taxonomy_term: Taxonomy term class from flask-taxonomies
        :type taxonomy_term: TaxonomyTerm
        :param timestamp: Datetime class
        :type timestamp: Datetime class
        :return: None
        :rtype: None
        """
        if taxonomy_term.parent:
            body = get_taxonomy_term(code=taxonomy_term.taxonomy.slug,
                                     slug=taxonomy_term.slug,
                                     timestamp=timestamp)
            current_search_client.index(index=self.index,
                                        id=taxonomy_term.id,
                                        body=body)
예제 #5
0
 def _taxonomy_terms_generator(self, timestamp, taxonomies: list = None):
     index_ = self.index
     path = self.app.config["TAXONOMY_ELASTICSEARCH_LOG_DIR"]
     if taxonomies is None:
         query = TaxonomyTerm.query.all()
     else:
         tree_ids = _get_tree_ids(taxonomies)
         query = TaxonomyTerm.query.filter(
             TaxonomyTerm.tree_id.in_(tree_ids))
     if not os.path.exists(path):
         os.mkdir(path)
     for node in query:
         try:
             if node.parent:
                 body = get_taxonomy_term(code=node.taxonomy.slug,
                                          slug=node.slug,
                                          timestamp=timestamp)
                 logger.info(
                     f"Taxonomy: {node.taxonomy.code}, Slug: {node.slug}")
                 yield {
                     '_op_type': 'index',
                     '_index': index_,
                     '_id': node.id,
                     '_source': body
                 }
         except:
             exc_traceback = traceback.format_exc()
             print(exc_traceback)
             print("\n\n\n")
             if timestamp is None:
                 timestamp = datetime.utcnow()
             file_name = f'{timestamp.strftime("%Y%m%dT%H%M%S")}.err'
             file_path = os.path.join(path, file_name)
             with open(file_path, "a") as f:
                 f.write(
                     f"TAXONOMY CODE: {node.taxonomy.slug}; SLUG: {node.slug}\n\n"
                     f"{exc_traceback}\n\n\n\n")
             continue
def find_org_by_slug(slug):
    result = current_flask_taxonomies_es.get(TAXONOMY_CODE, slug)
    if not result:
        result = get_taxonomy_term(code=TAXONOMY_CODE, slug=slug)
    return get_ref_es(result)
def test_get_taxonomy_term_3(app, db, child_term, child_term_dict):
    res = get_taxonomy_term(code=child_term.taxonomy.slug,
                            slug=child_term.slug)
    del res["date_of_serialization"]
    assert res == child_term_dict
def test_get_taxonomy_term_2(app, db, sample_term, sample_term_dict):
    res = get_taxonomy_term(code=sample_term.taxonomy.slug,
                            slug=sample_term.slug)
    del res["date_of_serialization"]
    assert res == sample_term_dict
def test_get_taxonomy_term(app):
    with pytest.raises(ValueError):
        get_taxonomy_term(code="root", slug="something")