diff --git a/src/api.py b/src/api.py index 3a0c9f1..55f85ce 100644 --- a/src/api.py +++ b/src/api.py @@ -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"]) diff --git a/src/app.py b/src/app.py index bca20f7..44f9ea9 100644 --- a/src/app.py +++ b/src/app.py @@ -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"
No data for dataset {dataset_select}
" @@ -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", diff --git a/src/dataset.py b/src/dataset.py index 8249abb..31de923 100644 --- a/src/dataset.py +++ b/src/dataset.py @@ -1,4 +1,5 @@ import logging +import typing from flask import render_template @@ -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) @@ -50,4 +62,4 @@ def name_lookup( html = f"Error: {err}" log.info("Final HTML:\n{}".format(html)) - return html + return html, strict_search