diff options
Diffstat (limited to 'ishtar_common/utils.py')
| -rw-r--r-- | ishtar_common/utils.py | 44 | 
1 files changed, 44 insertions, 0 deletions
| diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index c6a4032f0..5d9e85c60 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -104,9 +104,12 @@ def cached_label_changed(sender, **kwargs):              setattr(instance, cached_label, lbl)              changed = True      if changed: +        instance._search_updated = False          if hasattr(instance, '_cascade_change') and instance._cascade_change:              instance.skip_history_when_saving = True          instance.save() +    if hasattr(instance, 'update_search_vector'): +        instance.update_search_vector()      updated = False      if hasattr(instance, '_cached_labels_bulk_update'):          updated = instance._cached_labels_bulk_update() @@ -117,6 +120,7 @@ def cached_label_changed(sender, **kwargs):                  item.test_obj = instance.test_obj              cached_label_changed(item.__class__, instance=item) +  SHORTIFY_STR = ugettext(" (...)") @@ -289,3 +293,43 @@ def get_all_related_objects(model):          and f.auto_created and not f.concrete      ] + +def merge_tsvectors(vectors): +    """ +    Parse tsvector to merge them in one string +    :param vectors: list of tsvector string +    :return: merged tsvector +    """ +    result_dict = {} +    for vector in vectors: +        if not vector: +            continue + +        current_position = 0 +        if result_dict: +            for key in result_dict: +                max_position = max(result_dict[key]) +                if max_position > current_position: +                    current_position = max_position + +        for dct_member in vector.split(" "): +            splitted = dct_member.split(':') +            key = ":".join(splitted[:-1]) +            positions = splitted[-1] +            key = key[1:-1]  # remove quotes +            positions = [int(pos) + current_position +                         for pos in positions.split(',')] +            if key in result_dict: +                result_dict[key] += positions +            else: +                result_dict[key] = positions + +    # {'lamelie': [1, 42, 5]} => {'lamelie': "1,42,5"} +    result_dict = {k: ",".join([str(val) for val in result_dict[k]]) +                   for k in result_dict} +    # {'lamelie': "1,5", "hagarde": "2", "regarde": "4"} => +    # "'lamelie':1,5 'hagarde':2 'regarde':4" +    result = " ".join(["'{}':{}".format(k, result_dict[k]) +                       for k in result_dict]) + +    return result | 
