11from http import HTTPStatus
22from typing import Any , Callable , Dict , List , Optional , Union
3+ from urllib .parse import parse_qs , urlparse
34
45from django .contrib .admin import AdminSite
56from django .contrib .auth import REDIRECT_FIELD_NAME
@@ -235,6 +236,7 @@ def password_change(
235236
236237 def get_sidebar_list (self , request : HttpRequest ) -> List [Dict [str , Any ]]:
237238 navigation = get_config (self .settings_name )["SIDEBAR" ].get ("navigation" , [])
239+ tabs = get_config (self .settings_name )["TABS" ]
238240 results = []
239241
240242 for group in navigation :
@@ -246,7 +248,8 @@ def get_sidebar_list(self, request: HttpRequest) -> List[Dict[str, Any]]:
246248 request , item .get ("link_callback" ) or item ["link" ]
247249 )
248250
249- for tab in get_config (self .settings_name )["TABS" ]:
251+ # Checks if any tab item is active and then marks the sidebar link as active
252+ for tab in tabs :
250253 has_primary_link = False
251254 has_tab_link_active = False
252255
@@ -303,7 +306,7 @@ def get_tabs_list(self, request: HttpRequest) -> List[Dict[str, Any]]:
303306 item ["link_callback" ] = lazy (item ["link" ])(request )
304307
305308 item ["active" ] = self ._get_is_active (
306- request , item .get ("link_callback" ) or item ["link" ]
309+ request , item .get ("link_callback" ) or item ["link" ], True
307310 )
308311 allowed_items .append (item )
309312
@@ -392,13 +395,29 @@ def _process_colors(
392395
393396 return colors
394397
395- def _get_is_active (self , request : HttpRequest , link : str ) -> bool :
398+ def _get_is_active (
399+ self , request : HttpRequest , link : str , is_tab : bool = False
400+ ) -> bool :
396401 if not isinstance (link , str ):
397402 link = str (link )
398403
399- if link in request .path and link != reverse_lazy (f"{ self .name } :index" ):
404+ index_path = reverse_lazy (f"{ self .name } :index" )
405+ link_path = urlparse (link ).path
406+
407+ # Dashboard
408+ if link_path == request .path == index_path :
400409 return True
401- elif link == request .path == reverse_lazy (f"{ self .name } :index" ):
410+
411+ if link_path in request .path and link_path != index_path :
412+ query_params = parse_qs (urlparse (link ).query )
413+ request_params = parse_qs (request .GET .urlencode ())
414+
415+ # In case of tabs, we need to check if the query params are the same
416+ if is_tab and not all (
417+ request_params .get (k ) == v for k , v in query_params .items ()
418+ ):
419+ return False
420+
402421 return True
403422
404423 return False
0 commit comments