Skip to content

Commit

Permalink
Adding exclusion filter before query execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ssadhu-sl committed Sep 27, 2024
1 parent c0d1111 commit 46c7e7c
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions application/data_access/entity_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,51 @@ def get_entity_search(session: Session, parameters: dict):
count_query = session.query(func.count()).select_from(subquery)
count = count_query.scalar()

# get entities
exclude_fields = params.get("exclude_fields", [])

# Get the full list of fields from EntityOrm
all_fields = {column.name for column in EntityOrm.__table__.columns}

include_fields = [field for field in all_fields if field not in exclude_fields]

# Build the query with only the fields we want to include
query_args = [getattr(EntityOrm, field) for field in include_fields]
query_args = [EntityOrm]
query = session.query(*query_args)
query = _apply_base_filters(query, params)
query = _apply_date_filters(query, params)
query = _apply_location_filters(session, query, params)
query = _apply_period_option_filter(query, params)
query = _apply_limit_and_pagination_filters(query, params)
query = _apply_exclusion_filters(
query, params
) # Build the query without excluded params
entities = query.all()
entities = [entity_factory(entity_orm) for entity_orm in entities]
return {"params": params, "count": count, "entities": entities}


def _apply_exclusion_filters(query, params):
exclude_fields = params.get("exclude_field", [])
if exclude_fields:
# Split the comma-separated string into a list of individual fields
split_strings = [
s.strip() for sub in exclude_fields for s in sub.split(",") if s
]
exclude_fields = set(split_strings)
all_columns = {column.name for column in EntityOrm.__table__.columns}

# Exclude fields not present in the table schema
exclude_fields = {field for field in exclude_fields if field in all_columns}
# Dynamically construct the selected columns by excluding the specified fields
selected_columns = [
column
for column in EntityOrm.__table__.columns
if column.name not in exclude_fields
]

if not selected_columns:
raise ValueError(
"No columns left to select after exclusions. Please check the field names."
)

# Modify the query to select only the desired columns
query = query.with_entities(*selected_columns)

return query


def lookup_entity_link(
session: Session, reference: str, dataset: str, organisation_entity: int
):
Expand Down

0 comments on commit 46c7e7c

Please sign in to comment.