Skip to content

Commit f956917

Browse files
committed
fix(hypatio): Refactored how active project is determined for navigation to prevent error when scanners attempt to load non-existent paths
1 parent 2b32de5 commit f956917

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

app/hypatio/urls.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
re_path(r'^data-challenges/$', list_data_challenges, name='data-challenges'),
2020
re_path(r'^software-projects/$', list_software_projects, name='software-projects'),
2121
re_path(r'^healthcheck/?', include('health_check.urls')),
22-
re_path(r'^groups/(?P<group_key>[^/]+)/?', GroupView.as_view(), name="group"),
23-
re_path(r'^', index, name='index'),
22+
re_path(r'^groups/(?P<group_key>[^/]+)/?$', GroupView.as_view(), name="group"),
23+
re_path(r'^/?$', index, name='index'),
2424
]

app/hypatio/views.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import os
1+
22
from django.shortcuts import render
33
from django.utils.functional import SimpleLazyObject
4+
from django.urls import resolve, Resolver404
45

56
from hypatio.auth0authenticate import public_user_auth_and_jwt
7+
from projects.apps import ProjectsConfig
68
from projects.models import Group, DataProject
79

10+
import logging
11+
logger = logging.getLogger(__name__)
12+
13+
814
@public_user_auth_and_jwt
915
def index(request, template_name='index.html'):
1016
"""
@@ -32,11 +38,24 @@ def group_context():
3238
# Check for an active project and determine its group
3339
groups = Group.objects.filter(dataproject__isnull=False, dataproject__visible=True).distinct()
3440
active_group = None
35-
project = DataProject.objects.filter(project_key=os.path.basename(os.path.normpath(request.path))).first()
36-
if project:
3741

38-
# Check for group
39-
active_group = next((g for g in groups if project in g.dataproject_set.all()), None)
42+
# Attempt to resolve the current URL
43+
try:
44+
match = resolve(request.path)
45+
46+
# Check if projects
47+
if match and match.app_name == ProjectsConfig.name and "project_key" in match.kwargs:
48+
project = DataProject.objects.filter(project_key=match.kwargs["project_key"]).first()
49+
if project:
50+
51+
# Check for group
52+
active_group = next((g for g in groups if project in g.dataproject_set.all()), None)
53+
54+
except Resolver404:
55+
logger.debug(f"Path could not be resolved: {request.path}")
56+
57+
except Exception as e:
58+
logger.exception(f"Group context error: {e}", exc_info=True)
4059

4160
# Pull out top-level groups
4261
parent_groups_keys = groups.filter(parent__isnull=False).values_list('parent', flat=True)

0 commit comments

Comments
 (0)