From 95111ddbc03057739e68423f021460839d4a85cb Mon Sep 17 00:00:00 2001 From: Sharad S Date: Wed, 18 Dec 2024 15:13:31 -0500 Subject: [PATCH] query field type fixes --- specifyweb/express_search/related.py | 3 + specifyweb/stored_queries/batch_edit.py | 1 + specifyweb/stored_queries/query_construct.py | 2 +- specifyweb/stored_queries/queryfield.py | 2 +- specifyweb/stored_queries/queryfieldspec.py | 58 +++++++++++--------- 5 files changed, 38 insertions(+), 28 deletions(-) diff --git a/specifyweb/express_search/related.py b/specifyweb/express_search/related.py index f0c1643afe7..d811a673316 100644 --- a/specifyweb/express_search/related.py +++ b/specifyweb/express_search/related.py @@ -39,6 +39,7 @@ def col_to_fs(col, add_id=False): display=True, format_name=None, sort_type=0, + strict=False ) for col in Rs.columns ] @@ -53,6 +54,7 @@ def col_to_fs(col, add_id=False): display=True, format_name=None, sort_type=0, + strict=False ) ) @@ -66,6 +68,7 @@ def make_filter(f, negate): display=False, format_name=None, sort_type=0, + strict=False ) Rs.filter_fields = [make_filter(f, False) for f in Rs.filters] + [ diff --git a/specifyweb/stored_queries/batch_edit.py b/specifyweb/stored_queries/batch_edit.py index e2d383288e8..337b53574e4 100644 --- a/specifyweb/stored_queries/batch_edit.py +++ b/specifyweb/stored_queries/batch_edit.py @@ -178,6 +178,7 @@ def _query_field(field_spec: QueryFieldSpec, sort_type: int): display=True, format_name=None, sort_type=sort_type, + strict=False ) def _index( diff --git a/specifyweb/stored_queries/query_construct.py b/specifyweb/stored_queries/query_construct.py index 020be600cd3..68c9416147f 100644 --- a/specifyweb/stored_queries/query_construct.py +++ b/specifyweb/stored_queries/query_construct.py @@ -108,7 +108,7 @@ def _predicates_for_node(_node): # We don't want to include treedef if the rank is not present. new_filters = [*query.internal_filters, getattr(node, treedef_column).in_(defs_to_filter_on)] query = query._replace(internal_filters=new_filters) - return query, column + return query, column, current_field_spec.get_field(), table def tables_in_path(self, table, join_path): path = deque(join_path) diff --git a/specifyweb/stored_queries/queryfield.py b/specifyweb/stored_queries/queryfield.py index a54030f1b09..37012ba72d7 100644 --- a/specifyweb/stored_queries/queryfield.py +++ b/specifyweb/stored_queries/queryfield.py @@ -10,7 +10,7 @@ EphemeralField = namedtuple( "EphemeralField", - "stringId isRelFld operStart startValue isNot isDisplay sortType formatName", + "stringId isRelFld operStart startValue isNot isDisplay sortType formatName isStrict", ) diff --git a/specifyweb/stored_queries/queryfieldspec.py b/specifyweb/stored_queries/queryfieldspec.py index dc9c6544a82..5e086aac183 100644 --- a/specifyweb/stored_queries/queryfieldspec.py +++ b/specifyweb/stored_queries/queryfieldspec.py @@ -113,13 +113,15 @@ class TreeRankQuery(Relationship): class QueryFieldSpec( - namedtuple("QueryFieldSpec", "root_table root_sql_table join_path table date_part") + namedtuple("QueryFieldSpec", "root_table root_sql_table join_path table date_part tree_rank tree_field") ): root_table: Table root_sql_table: SQLTable join_path: FieldSpecJoinPath table: Table date_part: Optional[str] + tree_rank: Optional[str] + tree_field: Optional[str] @classmethod def from_path(cls, path_in, add_id=False): @@ -149,6 +151,8 @@ def from_path(cls, path_in, add_id=False): date_part=( "Full Date" if (join_path and join_path[-1].is_temporal()) else None ), + tree_rank=None, + tree_field=None ) @classmethod @@ -177,33 +181,33 @@ def from_stringid(cls, stringid, is_relation): extracted_fieldname, date_part = extract_date_part(field_name) field = node.get_field(extracted_fieldname, strict=False) - if field is None: # try finding tree - tree_rank_name, field = find_tree_and_field(node, extracted_fieldname) - if tree_rank_name: - tree_rank = TreeRankQuery(name=tree_rank_name) - # doesn't make sense to query across ranks of trees. no, it doesn't block a theoretical query like family -> continent - tree_rank.relatedModelName = node.name - tree_rank.type = "many-to-one" - join_path.append(tree_rank) - assert field is not None - field = node.get_field(field) - - if field is not None: + # if field is None: # try finding tree + # tree_rank_name, field = find_tree_and_field(node, extracted_fieldname) + # if tree_rank_name: + # tree_rank = TreeRankQuery(name=tree_rank_name) + # # doesn't make sense to query across ranks of trees. no, it doesn't block a theoretical query like family -> continent + # tree_rank.relatedModelName = node.name + # tree_rank.type = "many-to-one" + # join_path.append(tree_rank) + # assert field is not None + # field = node.get_field(field) + + tree_rank = tree_field = None + if field is None: # TODO: Check if removing below code breaks #5094 # tree_rank = tree_field = None - # if field is None: - # tree_id_match = TREE_ID_FIELD_RE.match(extracted_fieldname) - # if tree_id_match: - # tree_rank = tree_id_match.group(1) - # tree_field = 'ID' - # else: - # tree_field_match = TAXON_FIELD_RE.match(extracted_fieldname) if node is datamodel.get_table('Taxon') else GEOGRAPHY_FIELD_RE.match(extracted_fieldname) if node is datamodel.get_table('Geography') else None - # if tree_field_match: - # tree_rank = tree_field_match.group(1) - # tree_field = tree_field_match.group(2) - # else: - # tree_rank = extracted_fieldname if extracted_fieldname else None - # else: + tree_id_match = TREE_ID_FIELD_RE.match(extracted_fieldname) + if tree_id_match: + tree_rank = tree_id_match.group(1) + tree_field = 'ID' + else: + tree_field_match = TAXON_FIELD_RE.match(extracted_fieldname) if node is datamodel.get_table('Taxon') else GEOGRAPHY_FIELD_RE.match(extracted_fieldname) if node is datamodel.get_table('Geography') else None + if tree_field_match: + tree_rank = tree_field_match.group(1) + tree_field = tree_field_match.group(2) + else: + tree_rank = extracted_fieldname if extracted_fieldname else None + else: join_path.append(field) if field.is_temporal() and date_part is None: date_part = "Full Date" @@ -214,6 +218,8 @@ def from_stringid(cls, stringid, is_relation): join_path=tuple(join_path), table=node, date_part=date_part, + tree_rank=tree_rank, + tree_field=tree_field ) logger.debug(