From 73f02022674932049cfda128aac12162a7cff5a3 Mon Sep 17 00:00:00 2001 From: Liam Brydon Date: Wed, 11 Feb 2026 23:19:20 +1300 Subject: [PATCH] [Fix] Filter Scenarios with Parent (#311) (#323) The scenarios lists both in /scenarios and /package//scenario no longer show related scenarios (children). All related scenarios are shown on the scenario page under Related Scenarios if there are any. {C2D38DED-A402-4A27-A241-BC2302C62A50}.png Co-authored-by: Tim Lorsbach Reviewed-on: https://git.envipath.com/enviPath/enviPy/pulls/323 Co-authored-by: Liam Brydon Co-committed-by: Liam Brydon --- epapi/v1/endpoints/scenarios.py | 16 ++- epapi/v1/schemas.py | 6 ++ epdb/models.py | 6 +- epdb/views.py | 2 + templates/collections/paginated_base.html | 4 +- templates/objects/scenario.html | 113 ++++++++++++++++++++++ 6 files changed, 139 insertions(+), 8 deletions(-) diff --git a/epapi/v1/endpoints/scenarios.py b/epapi/v1/endpoints/scenarios.py index ee93c096..b333f812 100644 --- a/epapi/v1/endpoints/scenarios.py +++ b/epapi/v1/endpoints/scenarios.py @@ -12,7 +12,11 @@ from epdb.models import Scenario from epdb.logic import PackageManager from epdb.views import _anonymous_or_real from ..pagination import EnhancedPageNumberPagination -from ..schemas import ReviewStatusFilter, ScenarioOutSchema, ScenarioCreateSchema +from ..schemas import ( + ScenarioOutSchema, + ScenarioCreateSchema, + ScenarioReviewStatusAndRelatedFilter, +) from ..dal import get_user_entities_for_read, get_package_entities_for_read from envipy_additional_information import registry @@ -25,11 +29,12 @@ router = Router() @paginate( EnhancedPageNumberPagination, page_size=s.API_PAGINATION_DEFAULT_PAGE_SIZE, - filter_schema=ReviewStatusFilter, + filter_schema=ScenarioReviewStatusAndRelatedFilter, ) def list_all_scenarios(request): user = request.user - return get_user_entities_for_read(Scenario, user).order_by("name").all() + items = get_user_entities_for_read(Scenario, user) + return items.order_by("name").all() @router.get( @@ -39,11 +44,12 @@ def list_all_scenarios(request): @paginate( EnhancedPageNumberPagination, page_size=s.API_PAGINATION_DEFAULT_PAGE_SIZE, - filter_schema=ReviewStatusFilter, + filter_schema=ScenarioReviewStatusAndRelatedFilter, ) def list_package_scenarios(request, package_uuid: UUID): user = request.user - return get_package_entities_for_read(Scenario, package_uuid, user).order_by("name").all() + items = get_package_entities_for_read(Scenario, package_uuid, user) + return items.order_by("name").all() @router.post("/package/{uuid:package_uuid}/scenario/", response=ScenarioOutSchema) diff --git a/epapi/v1/schemas.py b/epapi/v1/schemas.py index 85632aae..466aac82 100644 --- a/epapi/v1/schemas.py +++ b/epapi/v1/schemas.py @@ -22,6 +22,12 @@ class StructureReviewStatusFilter(FilterSchema): review_status: Annotated[Optional[bool], FilterLookup("compound__package__reviewed")] = None +class ScenarioReviewStatusAndRelatedFilter(ReviewStatusFilter): + """Filter schema for review_status and parent query parameter.""" + + exclude_related: Annotated[Optional[bool], FilterLookup("parent__isnull")] = None + + # Base schema for all package-scoped entities class PackageEntityOutSchema(Schema): """Base schema for entities belonging to a package.""" diff --git a/epdb/models.py b/epdb/models.py index 60b04082..d15b03cf 100644 --- a/epdb/models.py +++ b/epdb/models.py @@ -3962,8 +3962,12 @@ class Scenario(EnviPathModel): yield inst def related_pathways(self): + scens = [self] + if self.parent is not None: + scens.append(self.parent) + return Pathway.objects.filter( - scenarios__in=[self], package__reviewed=True, package=self.package + scenarios__in=scens, package__reviewed=True, package=self.package ).distinct() diff --git a/epdb/views.py b/epdb/views.py index fadbb1f4..0ffa0564 100644 --- a/epdb/views.py +++ b/epdb/views.py @@ -2479,6 +2479,8 @@ def package_scenario(request, package_uuid, scenario_uuid): context["breadcrumbs"] = breadcrumbs(current_package, "scenario", current_scenario) context["scenario"] = current_scenario + # Get scenarios that have current_scenario as a parent + context["children"] = current_scenario.scenario_set.order_by("name") # Note: Modals now fetch schemas and data from API endpoints # Keeping these for backwards compatibility if needed elsewhere diff --git a/templates/collections/paginated_base.html b/templates/collections/paginated_base.html index 5806a1ec..9e328374 100644 --- a/templates/collections/paginated_base.html +++ b/templates/collections/paginated_base.html @@ -98,7 +98,7 @@ class="mt-6" x-show="activeTab === 'reviewed' && !isEmpty" x-data="remotePaginatedList({ - endpoint: '{{ api_endpoint }}?review_status=true', + endpoint: '{{ api_endpoint }}?review_status=true{% if entity_type == 'scenario' %}&exclude_related=true{% endif %}', instanceId: '{{ entity_type }}_reviewed', isReviewed: true, perPage: {{ per_page|default:50 }} @@ -113,7 +113,7 @@ class="mt-6" x-show="activeTab === 'unreviewed' && !isEmpty" x-data="remotePaginatedList({ - endpoint: '{{ api_endpoint }}?review_status=false', + endpoint: '{{ api_endpoint }}?review_status=false{% if entity_type == 'scenario' %}&exclude_related=true{% endif %}', instanceId: '{{ entity_type }}_unreviewed', isReviewed: false, perPage: {{ per_page|default:50 }} diff --git a/templates/objects/scenario.html b/templates/objects/scenario.html index 5b227380..1dd54bd5 100644 --- a/templates/objects/scenario.html +++ b/templates/objects/scenario.html @@ -171,6 +171,82 @@ + {% if scenario.parent %} +
+
+

+ Parent Scenario Additional Information +

+
+ + + + + + + + +
+
+
+ {% endif %} + {% if scenario.related_pathways %}
@@ -189,6 +265,43 @@
{% endif %} + + + {% if children.exists %} +
+ +
Related Scenarios
+
+ +
+
+ {% endif %} + + + {% if scenario.parent %} + + {% endif %}