Skip to content

Commit

Permalink
improve: search images table result (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts authored Jan 10, 2024
2 parents 65c1c34 + 1b11944 commit 393a46b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
37 changes: 29 additions & 8 deletions geotribu_cli/cli_results_rich_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def format_output_result_search_content(
search_term=search_term,
search_results_total=len(result),
search_results_displayed=count,
hint="Astuce : ctrl+clic sur le titre pour ouvrir le contenu",
)

table = Table(
Expand Down Expand Up @@ -149,6 +150,7 @@ def format_output_result_search_image(
search_term: Optional[str] = None,
format_type: Optional[str] = None,
count: int = 5,
search_filter_type: Optional[str] = None,
) -> Union[list[dict], Table]:
"""Format result according to output option.
Expand All @@ -162,9 +164,17 @@ def format_output_result_search_image(
str: formatted result ready to print
"""
if format_type == "table":
titre = add_search_criteria_as_str(
in_txt="Recherche dans les images",
search_term=search_term,
search_filter_type=search_filter_type,
search_results_total=len(result),
search_results_displayed=count,
hint="Astuce : ctrl+clic sur le nom pour ouvrir l'image",
)

table = Table(
title=f"Recherche d'images - {len(result)} résultats "
f"avec le terme : {search_term}\n(ctrl+clic sur le nom pour ouvrir l'image)",
title=titre,
show_lines=True,
highlight=True,
caption=f"{__title__} {__version__}",
Expand Down Expand Up @@ -204,21 +214,23 @@ def format_output_result_search_image(
@lru_cache
def add_search_criteria_as_str(
in_txt: str,
search_filter_dates: tuple,
search_filter_dates: Optional[tuple] = None,
search_results_total: Optional[int] = None,
search_results_displayed: Optional[int] = None,
search_term: Optional[str] = None,
search_filter_type: Optional[str] = None,
hint: Optional[str] = None,
) -> str:
"""Prettify a title with search criterias.
Args:
in_txt: initial title text
search_filter_dates: tuple of dates used to filter search
search_results_total: total of results of search. Defaults to None.
search_results_displayed: totla of results to display. Defaults to None.
search_results_displayed: total of results to display. Defaults to None.
search_term: search terms. Defaults to None.
search_filter_type: search filter type. Defaults to None.
hint: optional short hint text to display before the table. Defaults to None.
Returns:
formatted title
Expand All @@ -229,15 +241,24 @@ def add_search_criteria_as_str(
if search_term:
in_txt += f" avec le terme : {search_term}"

if any([search_filter_dates[0], search_filter_dates[1], search_filter_type]):
if any(
[
isinstance(search_filter_dates, tuple) and search_filter_dates[0],
isinstance(search_filter_dates, tuple) and search_filter_dates[1],
search_filter_type,
],
):
in_txt += "\nFiltres : "
if search_filter_type:
in_txt += f"de type {search_filter_type}, "
if search_filter_dates[0]:
if isinstance(search_filter_dates, tuple) and search_filter_dates[0]:
in_txt += f"plus récents que {search_filter_dates[0]:%d %B %Y}, "
if search_filter_dates[1]:
if isinstance(search_filter_dates, tuple) and search_filter_dates[1]:
in_txt += f"plus anciens que {search_filter_dates[1]:%d %B %Y}"
else:
in_txt += "Aucun filtre de recherche appliqué."
in_txt += "\nAucun filtre de recherche appliqué."

if hint:
in_txt += f"\n{hint}"

return in_txt
2 changes: 0 additions & 2 deletions geotribu_cli/comments/comments_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ def run(args: argparse.Namespace):
else:
open_uri(in_filepath=comment_obj.url_to_comment)

sys.exit(0)


# -- Stand alone execution
if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion geotribu_cli/search/search_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def parser_search_image(subparser: argparse.ArgumentParser) -> argparse.Argument
Returns:
argparse.ArgumentParser: parser ready to use
"""

subparser.add_argument(
"search_term",
help="Terme de recherche.",
Expand Down Expand Up @@ -237,6 +236,7 @@ def run(args: argparse.Namespace):
format_type=args.format_output,
count=args.results_number,
search_term=args.search_term,
search_filter_type=args.filter_type,
)
)
else:
Expand Down
5 changes: 3 additions & 2 deletions requirements/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
# --------------------

Pillow>=10.0.1,<11
pytest-cov>=4,<5
validators>=0.19,<0.23
pytest-cov>=4
pytest-retry>=1.6
validators>=0.20,<0.23
33 changes: 33 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ def test_cli_run_comments_latest(capsys):
assert Path(Path().home() / ".geotribu/comments/latest.json").exists()


def test_cli_run_comments_open(capsys):
"""Test nested subcommand comments latest."""
cli.main(["comments", "open"])

out, err = capsys.readouterr()

assert err == ""
assert Path(Path().home() / ".geotribu/comments/latest.json").exists()


@pytest.mark.flaky(retries=3, delay=1, only_on=[SystemExit])
def test_cli_run_comments_open_specific(capsys):
"""Test nested subcommand comments latest."""
cli.main(["comments", "open", "--page-size", "25", "--comment-id", "15"])

out, err = capsys.readouterr()

assert err == ""
assert Path(Path().home() / ".geotribu/comments/latest.json").exists()


def test_cli_run_contenus_articles_ubuntu(capsys):
"""Test CLI images."""
cli.main(
Expand Down Expand Up @@ -102,6 +123,18 @@ def test_cli_run_images_logo_news(capsys):
assert Path(Path().home() / ".geotribu/search/cdn_search_index.json").exists()


def test_cli_run_si_post_table(capsys):
"""Test CLI images."""
cli.main(["si", "--no-prompt", "post*"])

out, err = capsys.readouterr()

assert out.strip().startswith("Recherche dans les images")
assert err == ""

assert Path(Path().home() / ".geotribu/search/cdn_search_index.json").exists()


def test_cli_run_new_article(capsys):
"""Test subcommand creating new article."""

Expand Down

0 comments on commit 393a46b

Please sign in to comment.