From 9ee3c7b17085cba4e950b771249820eda740ea9b Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Tue, 16 Jul 2024 22:29:17 -0400 Subject: [PATCH] webcli: implement initial film filters Example: lcli films query --limit 5 --genre horror --country USA --offer criterionchannel --- .flox/env/manifest.lock | 2 +- src/letsrolld/db/models.py | 3 ++ src/letsrolld/webapi/app.py | 1 + src/letsrolld/webcli/cli.py | 57 ++++++++++++++++++++++++++++++++++--- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/.flox/env/manifest.lock b/.flox/env/manifest.lock index 65ed4f1..b4c7979 100644 --- a/.flox/env/manifest.lock +++ b/.flox/env/manifest.lock @@ -1325,4 +1325,4 @@ "nixpkgs" ] } -} +} \ No newline at end of file diff --git a/src/letsrolld/db/models.py b/src/letsrolld/db/models.py index 963b749..4ad2d15 100644 --- a/src/letsrolld/db/models.py +++ b/src/letsrolld/db/models.py @@ -84,6 +84,9 @@ class Film(Base): countries: Mapped[list[Country]] = relationship( secondary=film_country_association_table ) + offers: Mapped[list[Offer]] = relationship( + secondary="film_offer_association_table" + ) directors = relationship( "Director", diff --git a/src/letsrolld/webapi/app.py b/src/letsrolld/webapi/app.py index 6a46043..e5ccfe5 100644 --- a/src/letsrolld/webapi/app.py +++ b/src/letsrolld/webapi/app.py @@ -157,6 +157,7 @@ class FilmResource(Resource): "required": False, "schema": {"type": "integer", "default": 10}, }, + # TODO: make filters accept multiple values { "name": "genre", "in": "query", diff --git a/src/letsrolld/webcli/cli.py b/src/letsrolld/webcli/cli.py index 920cc8e..cd441d0 100644 --- a/src/letsrolld/webcli/cli.py +++ b/src/letsrolld/webcli/cli.py @@ -9,6 +9,17 @@ # from letsrolld_api_client.api.default import get_films_id +DEFAULT_OFFERS = { + # TODO: use constants for offer names + "criterionchannel", + "amazon", + "kanopy", + "hoopla", + "amazonprime", + "youtube", +} + + # TODO: make the url configurable client = Client(base_url="http://localhost:8000") @@ -18,16 +29,21 @@ ) -def report_film(film): +def list_film(film): template = env.get_template("film.template") return template.render(film=film) -def report_director(director): +def list_director(director): template = env.get_template("director.template") return template.render(director=director) +def report_film(film): + template = env.get_template("film-full.template") + return template.render(film=film, offers=DEFAULT_OFFERS) + + @click.group() def cli(): pass @@ -44,7 +60,7 @@ def directors_get(): with client as client: director_reports = [] for director in get_directors.sync(client=client): - director_reports.append(report_director(director)) + director_reports.append(list_director(director)) print("\n\n".join(director_reports)) @@ -60,10 +76,43 @@ def films_get(): with client as client: film_reports = [] for film in get_films.sync(client=client): - film_reports.append(report_film(film)) + film_reports.append(list_film(film)) print("\n".join(film_reports)) +def _get_query_args(limit, genre, country, offer): + args = {"limit": limit} + if genre: + args["genre"] = genre + if country: + args["country"] = country + if offer: + args["offer"] = offer + return args + + +@films.command(name="query") +# TODO: build options from the API model definition +@click.option("--limit", default=10) +@click.option("--genre", default=None) +@click.option("--country", default=None) +@click.option("--offer", default=None) +def films_query( + limit: int, + genre: str, + country: str, + offer: str, +): + global client + with client as client: + args = _get_query_args(limit, genre, country, offer) + film_reports = [] + for film in get_films.sync(client=client, **args): + film_reports.append(report_film(film)) + + print("\n\n".join(film_reports)) + + if __name__ == "__main__": cli()