diff options
Diffstat (limited to 'ishtar_common/views_item.py')
| -rw-r--r-- | ishtar_common/views_item.py | 74 | 
1 files changed, 66 insertions, 8 deletions
diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index 9f2fbb7b5..a622fdb9f 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -976,6 +976,67 @@ def _manage_many_counted_fields(fields, reversed_fields, dct, excluded_dct):              excluded_dct[k] = False +def __manage_relative_search(value): +    """ +    Parse search to manage "=", "=>" and "=<" searches +    :param value: value +    :return: value, search type ("eq", "lte" or "gte") +    """ +    value = value.replace('"', "").strip() +    search_type = "eq" +    if value.startswith(">"): +        value = value[1:] +        search_type = "gte" +    elif value.startswith("<"): +        value = value[1:] +        search_type = "lte" +    return value, search_type + + +def _manage_number(k, dct): +    """ +    Manage number from a search query +    :param k: number key +    :param dct: search dict +    :return: None -> search dict is modified +    """ +    values = dct[k].split(";") +    results = [] +    for value in values: +        value, search_type = __manage_relative_search(value) +        try: +            value = str(float(value.replace(",", "."))) +            if value.endswith(".0"): +                value = value[:-2] +            results.append((search_type, value)) +        except ValueError: +            continue +    mins = [value for dt, value in results if dt == "gte"] +    maxs = [value for dt, value in results if dt == "lte"] +    eqs = [value for dt, value in results if dt == "eq"] +    if eqs and not mins and not maxs:  # TODO: min and max not available +        dct[k] = ";".join(eqs) +        return +    dct.pop(k) +    if mins: +        dct[k + "__gte"] = min(mins) +    if maxs: +        dct[k + "__lte"] = max(maxs) + + +def _manage_number_fields(fields, dct): +    keys = list(dct.keys()) +    for key in fields: +        res = [j for j in keys if j.startswith(key)] +        if not res: +            continue +        for k in res: +            if not dct[k]: +                dct.pop(k) +                continue +            _manage_number(k, dct) + +  today_lbl = pgettext_lazy("key for text search", "today")  TODAYS = ["today"] @@ -995,14 +1056,7 @@ def _manage_date(k, dct):      values = dct[k].split(";")      results = []      for value in values: -        value = value.replace('"', "").strip() -        date_type = "eq" -        if value.startswith(">"): -            value = value[1:] -            date_type = "gte" -        elif value.startswith("<"): -            value = value[1:] -            date_type = "lte" +        value, date_type = __manage_relative_search(value)          has_today = False          for today in TODAYS:              if value.startswith(today): @@ -2020,6 +2074,7 @@ def get_item(          reversed_many_counted_fields = getattr(              model, "REVERSED_MANY_COUNTED_FIELDS", None          ) +        my_number_fields = getattr(model, "NUMBER_FIELDS", [])[:]          if not dated_fields and hasattr(model, "DATED_FIELDS"):              my_dated_fields = model.DATED_FIELDS[:] @@ -2295,6 +2350,9 @@ def get_item(          _manage_dated_fields(my_dated_fields, dct)          _manage_dated_fields(my_dated_fields, excluded_dct) +        _manage_number_fields(my_number_fields, dct) +        _manage_number_fields(my_number_fields, excluded_dct) +          _manage_hierarchic_fields(model, dct, and_reqs)          _manage_hierarchic_fields(model, excluded_dct, exc_and_reqs)  | 
