flask_taxonomies/bulk.py
def bulk_taxonomy_query(
        db,
        taxonomy_code,
        query_data,
        query_function,
        query_combining_function,
        result_preprocess_function,
        result_match_function
) -> List[SingleQueryResult]:
    """
    Selects taxonomy terms by multiple search criteria.

    It works the following way:

      1. query_data is an array of query data (for example, oai-pmh records). Items in this array are
         called 'd'
         Each 'd' is converted to an array of query criteria via the query_function function.
      2. An array of array of query criteria is passed to the query_combining_function that
         creates db query, using TaxonomyTerm class
      3. TaxonomyTerm query selecting terms matching the created query and their ancestors
         is executed on the database.
      4. The resulting terms are split into "all_terms" and "leaf_terms" dictionaries and both
         are passed to the result_preprocess_function. The function returns a context filled
         with (somehow) preprocessed terms
      5. For each 'd', the result_match_function is called with 'd',
         list of criteria from #1 and context. The function must return
         an instance of SingleQueryResult
      6. A list of these (in the same order as input data) is returned

    :param taxonomy_code:       the taxonomy code
    :param query_data:          array of query criteria
    :param query_function:      function converting query criterium to DB query
    :param result_preprocess_function:      function for preprocessing terms from DB
    :param result_match_function:   function that matches pre-processed terms query criteria
    :return:
    """
    call_context = {}
    all_terms = db.session.query(TaxonomyTerm.id, TaxonomyTerm.parent_id).filter(
        TaxonomyTerm.taxonomy_code == taxonomy_code,
        TaxonomyTerm.status == TermStatusEnum.alive
    )
    terms = all_terms
    d_with_criteria = [
        (d, tuple(query_function(d))) for d in query_data
    ]
    terms = terms.filter(
        query_combining_function(
            [d[1] for d in d_with_criteria],
            call_context
        )
    )
    terms = terms.cte('cte', recursive=True)

    ancestors = all_terms.join(terms, TaxonomyTerm.id == terms.c.parent_id)

    recursive_q = terms.union(ancestors)
    q = db.session.query(recursive_q)
    ids = [x[0] for x in q]
    terms = {
        t.id: t for t in
        db.session
            .query(TaxonomyTerm)
            .options(joinedload(TaxonomyTerm.taxonomy, innerjoin=True))
            .filter(TaxonomyTerm.id.in_(ids))
    }
    parent_terms = set(t.parent_id for t in terms.values())
    leaf_terms = {
        k: v for k, v in terms.items() if k not in parent_terms
    }
    ctx = result_preprocess_function(leaf_terms, terms, call_context)
    for d in d_with_criteria:
        yield result_match_function(d[0], d[1], ctx)

    Selects taxonomy terms by multiple search criteria.

    It works the following way:

      1. query_data is an array of query data (for example, oai-pmh records). Items in this array are
         called 'd'
         Each 'd' is converted to an array of query criteria via the query_function function.
      2. An array of array of query criteria is passed to the query_combining_function that
         creates db query, using TaxonomyTerm class
      3. TaxonomyTerm query selecting terms matching the created query and their ancestors
         is executed on the database.
      4. The resulting terms are split into "all_terms" and "leaf_terms" dictionaries and both
         are passed to the result_preprocess_function. The function returns a context filled
         with (somehow) preprocessed terms
      5. For each 'd', the result_match_function is called with 'd',
         list of criteria from #1 and context. The function must return
         an instance of SingleQueryResult
      6. A list of these (in the same order as input data) is returned

    :param taxonomy_code:       the taxonomy code
    :param query_data:          array of query criteria
    :param query_function:      function converting query criterium to DB query
    :param result_preprocess_function:      function for preprocessing terms from DB
    :param result_match_function:   function that matches pre-processed terms query criteria
    :return:
    
Selects taxonomy terms by multiple search criteria.
It works the following way.

1. query_data is an array of query data (for example, oai-pmh records). Items in this array are
called 'd'
Each 'd' is converted to an array of query criteria via the query_function function.
2. An array of array of query criteria is passed to the query_combining_function that
creates db query, using TaxonomyTerm class
3. TaxonomyTerm query selecting terms matching the created query and their ancestors
is executed on the database.
4. The resulting terms are split into "all_terms" and "leaf_terms" dictionaries and both
are passed to the result_preprocess_function. The function returns a context filled
with (somehow) preprocessed terms
5. For each 'd', the result_match_function is called with 'd',
list of criteria from #1 and context. The function must return
an instance of SingleQueryResult
6. A list of these (in the same order as input data) is returned
Selects taxonomy terms by multiple search criteria . It works the following way . 1 . query_data is an array of query data ( for example oai - pmh records ) . Items in this array are called ' d ' Each ' d ' is converted to an array of query criteria via the query_function function . 2 . An array of array of query criteria is passed to the query_combining_function that creates db query using TaxonomyTerm class 3 . TaxonomyTerm query selecting terms matching the created query and their ancestors is executed on the database . 4 . The resulting terms are split into " all_terms " and " leaf_terms " dictionaries and both are passed to the result_preprocess_function . The function returns a context filled with ( somehow ) preprocessed terms 5 . For each ' d ' the result_match_function is called with ' d ' list of criteria from #1 and context . The function must return an instance of SingleQueryResult 6 . A list of these ( in the same order as input data ) is returned
def bulk_taxonomy_query(
        db,
        taxonomy_code,
        query_data,
        query_function,
        query_combining_function,
        result_preprocess_function,
        result_match_function
) -> List[SingleQueryResult]:
    call_context = {}
    all_terms = db.session.query(TaxonomyTerm.id, TaxonomyTerm.parent_id).filter(
        TaxonomyTerm.taxonomy_code == taxonomy_code,
        TaxonomyTerm.status == TermStatusEnum.alive
    )
    terms = all_terms
    d_with_criteria = [
        (d, tuple(query_function(d))) for d in query_data
    ]
    terms = terms.filter(
        query_combining_function(
            [d[1] for d in d_with_criteria],
            call_context
        )
    )
    terms = terms.cte('cte', recursive=True)
    ancestors = all_terms.join(terms, TaxonomyTerm.id == terms.c.parent_id)
    recursive_q = terms.union(ancestors)
    q = db.session.query(recursive_q)
    ids = [x[0] for x in q]
    terms = {
        t.id: t for t in
        db.session
            .query(TaxonomyTerm)
            .options(joinedload(TaxonomyTerm.taxonomy, innerjoin=True))
            .filter(TaxonomyTerm.id.in_(ids))
    }
    parent_terms = set(t.parent_id for t in terms.values())
    leaf_terms = {
        k: v for k, v in terms.items() if k not in parent_terms
    }
    ctx = result_preprocess_function(leaf_terms, terms, call_context)
    for d in d_with_criteria:
        yield result_match_function(d[0], d[1], ctx)
def bulk_taxonomy_query ( db , taxonomy_code , query_data , query_function , query_combining_function , result_preprocess_function , result_match_function ) -> List [ SingleQueryResult ] : call_context = { } all_terms = db . session . query ( TaxonomyTerm . id , TaxonomyTerm . parent_id ) . filter ( TaxonomyTerm . taxonomy_code == taxonomy_code , TaxonomyTerm . status == TermStatusEnum . alive ) terms = all_terms d_with_criteria = [ ( d , tuple ( query_function ( d ) ) ) for d in query_data ] terms = terms . filter ( query_combining_function ( [ d [ 1 ] for d in d_with_criteria ] , call_context ) ) terms = terms . cte ( 'cte' , recursive = True ) ancestors = all_terms . join ( terms , TaxonomyTerm . id == terms . c . parent_id ) recursive_q = terms . union ( ancestors ) q = db . session . query ( recursive_q ) ids = [ x [ 0 ] for x in q ] terms = { t . id : t for t in db . session . query ( TaxonomyTerm ) . options ( joinedload ( TaxonomyTerm . taxonomy , innerjoin = True ) ) . filter ( TaxonomyTerm . id . in_ ( ids ) ) } parent_terms = set ( t . parent_id for t in terms . values ( ) ) leaf_terms = { k : v for k , v in terms . items ( ) if k not in parent_terms } ctx = result_preprocess_function ( leaf_terms , terms , call_context ) for d in d_with_criteria : yield result_match_function ( d [ 0 ] , d [ 1 ] , ctx )
Selects taxonomy terms by multiple search criteria.
Selects taxonomy terms by multiple search criteria .
"""
    Selects taxonomy terms by multiple search criteria.

    It works the following way:

      1. query_data is an array of query data (for example, oai-pmh records). Items in this array are
         called 'd'
         Each 'd' is converted to an array of query criteria via the query_function function.
      2. An array of array of query criteria is passed to the query_combining_function that
         creates db query, using TaxonomyTerm class
      3. TaxonomyTerm query selecting terms matching the created query and their ancestors
         is executed on the database.
      4. The resulting terms are split into "all_terms" and "leaf_terms" dictionaries and both
         are passed to the result_preprocess_function. The function returns a context filled
         with (somehow) preprocessed terms
      5. For each 'd', the result_match_function is called with 'd',
         list of criteria from #1 and context. The function must return
         an instance of SingleQueryResult
      6. A list of these (in the same order as input data) is returned

    :param taxonomy_code:       the taxonomy code
    :param query_data:          array of query criteria
    :param query_function:      function converting query criterium to DB query
    :param result_preprocess_function:      function for preprocessing terms from DB
    :param result_match_function:   function that matches pre-processed terms query criteria
    :return:
    """
None
db
taxonomy_code
the taxonomy code
the taxonomy code
query_data
array of query criteria
array of query criteria
query_function
function converting query criterium to DB query
function converting query criterium to DB query
query_combining_function
result_preprocess_function
function for preprocessing terms from DB
function for preprocessing terms from DB
result_match_function
function that matches pre-processed terms query criteria
function that matches pre - processed terms query criteria