@@ -2024,14 +2024,15 @@ def get_finding_group_queryset_for_context(pid=None, eid=None, tid=None):
20242024 """
20252025 Helper function to build finding group queryset based on context hierarchy.
20262026 Context priority: test > engagement > product > global
2027-
2027+
20282028 Args:
20292029 pid: Product ID (least specific)
20302030 eid: Engagement ID
20312031 tid: Test ID (most specific)
2032-
2032+
20332033 Returns:
20342034 QuerySet of Finding_Group filtered by context
2035+
20352036 """
20362037 if tid is not None :
20372038 # Most specific: filter by test
@@ -2238,37 +2239,50 @@ def set_related_object_fields(self, *args: list, **kwargs: dict):
22382239 eid = self .eid ,
22392240 tid = self .tid ,
22402241 )
2241-
2242+
22422243 # Filter by most specific context: test > engagement > product
22432244 if self .tid is not None :
22442245 # Test context: filter finding groups by test
2245- del self .form .fields ["test__engagement__product" ]
2246- del self .form .fields ["test__engagement__product__prod_type" ]
2247- del self .form .fields ["test__engagement" ]
2248- del self .form .fields ["test" ]
2246+ if "test__engagement__product" in self .form .fields :
2247+ del self .form .fields ["test__engagement__product" ]
2248+ if "test__engagement__product__prod_type" in self .form .fields :
2249+ del self .form .fields ["test__engagement__product__prod_type" ]
2250+ if "test__engagement" in self .form .fields :
2251+ del self .form .fields ["test__engagement" ]
2252+ if "test" in self .form .fields :
2253+ del self .form .fields ["test" ]
22492254 elif self .eid is not None :
22502255 # Engagement context: filter finding groups by engagement
2251- del self .form .fields ["test__engagement__product" ]
2252- del self .form .fields ["test__engagement__product__prod_type" ]
2253- del self .form .fields ["test__engagement" ]
2256+ if "test__engagement__product" in self .form .fields :
2257+ del self .form .fields ["test__engagement__product" ]
2258+ if "test__engagement__product__prod_type" in self .form .fields :
2259+ del self .form .fields ["test__engagement__product__prod_type" ]
2260+ if "test__engagement" in self .form .fields :
2261+ del self .form .fields ["test__engagement" ]
22542262 # Filter tests by engagement - get_authorized_tests doesn't support engagement param
2255- engagement = Engagement .objects .get (id = self .eid )
2256- self .form .fields ["test" ].queryset = get_authorized_tests (Permissions .Test_View , product = engagement .product ).filter (engagement_id = self .eid ).prefetch_related ("test_type" )
2263+ engagement = Engagement .objects .filter (id = self .eid ).select_related ("product" ).first ()
2264+ if engagement :
2265+ self .form .fields ["test" ].queryset = get_authorized_tests (Permissions .Test_View , product = engagement .product ).filter (engagement_id = self .eid ).prefetch_related ("test_type" )
22572266 elif self .pid is not None :
22582267 # Product context: filter finding groups by product
2259- del self .form .fields ["test__engagement__product" ]
2260- del self .form .fields ["test__engagement__product__prod_type" ]
2268+ if "test__engagement__product" in self .form .fields :
2269+ del self .form .fields ["test__engagement__product" ]
2270+ if "test__engagement__product__prod_type" in self .form .fields :
2271+ del self .form .fields ["test__engagement__product__prod_type" ]
22612272 # TODO: add authorized check to be sure
2262- self .form .fields ["test__engagement" ].queryset = Engagement .objects .filter (
2263- product_id = self .pid ,
2264- ).all ()
2265- self .form .fields ["test" ].queryset = get_authorized_tests (Permissions .Test_View , product = self .pid ).prefetch_related ("test_type" )
2273+ if "test__engagement" in self .form .fields :
2274+ self .form .fields ["test__engagement" ].queryset = Engagement .objects .filter (
2275+ product_id = self .pid ,
2276+ ).all ()
2277+ if "test" in self .form .fields :
2278+ self .form .fields ["test" ].queryset = get_authorized_tests (Permissions .Test_View , product = self .pid ).prefetch_related ("test_type" )
22662279 else :
22672280 # Global context: show all authorized finding groups
22682281 self .form .fields [
22692282 "test__engagement__product__prod_type" ].queryset = get_authorized_product_types (Permissions .Product_Type_View )
22702283 self .form .fields ["test__engagement" ].queryset = get_authorized_engagements (Permissions .Engagement_View )
2271- del self .form .fields ["test" ]
2284+ if "test" in self .form .fields :
2285+ del self .form .fields ["test" ]
22722286
22732287 if self .form .fields .get ("test__engagement__product" ):
22742288 self .form .fields ["test__engagement__product" ].queryset = get_authorized_products (Permissions .Product_View )
0 commit comments