Skip to content

Commit

Permalink
Attempt to move include parameter in GET requests to Cases into datab…
Browse files Browse the repository at this point in the history
…ase service.
  • Loading branch information
metroid-samus committed Dec 11, 2024
1 parent 34bb6c1 commit c03f15e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
2 changes: 0 additions & 2 deletions src/dispatch/case/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ class CaseReadMinimal(CaseBase):
project: ProjectRead
reporter: Optional[ParticipantReadMinimal]
reported_at: Optional[datetime] = None
tags: Optional[List[TagRead]] = []
ticket: Optional[TicketRead] = None
total_cost: float | None
triage_at: Optional[datetime] = None

Expand Down
1 change: 1 addition & 0 deletions src/dispatch/case/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def get_cases(
expand: bool = Query(default=False),
):
"""Retrieves all cases."""
common["include_keys"] = include
pagination = search_filter_sort_paginate(model="Case", **common)

if expand:
Expand Down
33 changes: 23 additions & 10 deletions src/dispatch/database/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,7 @@ def apply_filters(query, filter_spec, model_cls=None, do_auto_join=True):
return query


def apply_filter_specific_joins(model: Base, filter_spec: dict, query: orm.query):
"""Applies any model specific implicitly joins."""
def get_model_map(filters: dict) -> dict:
# this is required because by default sqlalchemy-filter's auto-join
# knows nothing about how to join many-many relationships.
model_map = {
Expand Down Expand Up @@ -371,19 +370,21 @@ def apply_filter_specific_joins(model: Base, filter_spec: dict, query: orm.query
(SignalInstance, "EntityType"): (SignalInstance.entities, True),
(Tag, "TagType"): (Tag.tag_type, False),
}
filters = build_filters(filter_spec)

# Replace mapping if looking for commander
if "Commander" in str(filter_spec):
if "Commander" in filters:
model_map.update({(Incident, "IndividualContact"): (Incident.commander, True)})
if "Assignee" in str(filter_spec):
if "Assignee" in filters:
model_map.update({(Case, "IndividualContact"): (Case.assignee, True)})
return model_map

filter_models = get_named_models(filters)

def apply_model_specific_joins(model: Base, models: List[str], query: orm.query):
model_map = get_model_map(models)
joined_models = []
for filter_model in filter_models:
if model_map.get((model, filter_model)):
joined_model, is_outer = model_map[(model, filter_model)]

for include_model in models:
if model_map.get((model, include_model)):
joined_model, is_outer = model_map[(model, include_model)]
try:
if joined_model not in joined_models:
query = query.join(joined_model, isouter=is_outer)
Expand All @@ -394,6 +395,14 @@ def apply_filter_specific_joins(model: Base, filter_spec: dict, query: orm.query
return query


def apply_filter_specific_joins(model: Base, filter_spec: dict, query: orm.query):
"""Applies any model specific implicitly joins."""
filters = build_filters(filter_spec)
filter_models = get_named_models(filters)

return apply_model_specific_joins(model, filter_models, query)


def composite_search(*, db_session, query_str: str, models: List[Base], current_user: DispatchUser):
"""Perform a multi-table search based on the supplied query."""
s = CompositeSearch(db_session, models)
Expand Down Expand Up @@ -537,6 +546,7 @@ def search_filter_sort_paginate(
model,
query_str: str = None,
filter_spec: str | dict | None = None,
include_keys: List[str] = None,
page: int = 1,
items_per_page: int = 5,
sort_by: List[str] = None,
Expand Down Expand Up @@ -574,6 +584,9 @@ def search_filter_sort_paginate(
else:
query = apply_filters(query, filter_spec, model_cls)

if include_keys:
query = apply_model_specific_joins(model_cls, include_keys, query)

if model == "Incident":
query = query.intersect(query_restricted)
for filter in tag_all_filters:
Expand Down

0 comments on commit c03f15e

Please sign in to comment.