Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default strict search to true if only strict fields are searched #56

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def get_results(
return records


def get_query_fields(metadata: DatasetMetadata) -> List[Entity]:
def get_query_fields(metadata: Optional[DatasetMetadata]) -> List[Entity]:
if metadata is None:
return []

search_routes = metadata["search_routes"]
fields = metadata["fields"]
exact_params = set(search_routes["exact"]["query_params"])
Expand Down
6 changes: 3 additions & 3 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ def name_page():
"""Officer name lookup form render"""
params = request.args.copy()
# Pop the parameters we know will be present
strict_search = params.pop("strict_search", False)
strict_search = False
dataset_select = params.pop("dataset_select", DEFAULT_DATASET)
datasets = api.get_datasets()
metadata = datasets.get(dataset_select, "")
entities = api.get_query_fields(metadata)
if not metadata:
# This shouldn't happen, but return *something* if the dataset is borked.
html = f"<p><b>No data for dataset {dataset_select}</b></p>"
Expand All @@ -72,8 +73,7 @@ def name_page():
# Don't call the backend API unless we have something.
html = ""
else:
html = dataset.name_lookup(metadata, **request.args)
entities = api.get_query_fields(metadata)
html, strict_search = dataset.name_lookup(metadata, entities, **request.args)

return render_template(
"index.html",
Expand Down
18 changes: 15 additions & 3 deletions src/dataset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import typing

from flask import render_template

Expand Down Expand Up @@ -39,9 +40,20 @@ def license_lookup(license: str) -> str:

def name_lookup(
metadata: api_types.DatasetMetadata,
strict_search: bool = False,
entities: typing.List[api_types.Entity],
strict_search: bool = None,
**kwargs,
) -> str:
) -> typing.Tuple[str, bool]:
if strict_search is None:
strict_search = True
for fuzzy_entity in [entity for entity in entities if entity["is_fuzzy"]]:
if (
fuzzy_entity["query_param"] in kwargs
and kwargs[fuzzy_entity["query_param"]] != ""
):
strict_search = False
break

try:
records = api.get_results(metadata, strict_search, **kwargs)
html = api.render_officers(records, metadata)
Expand All @@ -50,4 +62,4 @@ def name_lookup(
html = f"<p><b>Error:</b> {err}"

log.info("Final HTML:\n{}".format(html))
return html
return html, strict_search