Skip to content

Commit

Permalink
webcli: implement initial film filters
Browse files Browse the repository at this point in the history
Example:

lcli films query --limit 5 --genre horror --country USA --offer criterionchannel
  • Loading branch information
booxter committed Jul 17, 2024
1 parent f2073a1 commit 9ee3c7b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .flox/env/manifest.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1325,4 +1325,4 @@
"nixpkgs"
]
}
}
}
3 changes: 3 additions & 0 deletions src/letsrolld/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/letsrolld/webapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class FilmResource(Resource):
"required": False,
"schema": {"type": "integer", "default": 10},
},
# TODO: make filters accept multiple values
{
"name": "genre",
"in": "query",
Expand Down
57 changes: 53 additions & 4 deletions src/letsrolld/webcli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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
Expand All @@ -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))

Expand All @@ -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()

0 comments on commit 9ee3c7b

Please sign in to comment.