From f818623969518e9db09ac162dd9e8e9be6b7feb1 Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Wed, 20 Nov 2024 18:56:38 -0500 Subject: [PATCH] Don't duplicate movies in different report sections --- src/letsrolld/webapi/app.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/letsrolld/webapi/app.py b/src/letsrolld/webapi/app.py index 81ebd0a..dc5e38f 100644 --- a/src/letsrolld/webapi/app.py +++ b/src/letsrolld/webapi/app.py @@ -227,8 +227,8 @@ def _get_report_config(id): return sections -def _execute_section_plan(db, config): - query = db.session.query(models.Film) +def _execute_section_plan(db, config, seen_films): + query = db.session.query(models.Film).filter(~models.Film.id.in_(seen_films)) if config.services: query = query.join(models.Film.offers).filter( models.Offer.name.in_(config.services) @@ -287,15 +287,13 @@ def get(self, id): # TODO: support multiple reports if id != 0: return {}, 404 - return _get_report( - sections=[ - webapi_models.ReportSection( - name=config.name, - films=_execute_section_plan(db_, config), - ) - for config in _get_report_config(id) - ] - ), 200 + sections = [] + seen_films = set() + for config in _get_report_config(id): + films = _execute_section_plan(db_, config, seen_films) + seen_films.update(f["id"] for f in films) + sections.append(webapi_models.ReportSection(name=config.name, films=films)) + return _get_report(sections=sections), 200 def _api():