Skip to content

Commit

Permalink
fix(hypatio): Refactored how active project is determined for navigat…
Browse files Browse the repository at this point in the history
…ion to prevent error when scanners attempt to load non-existent paths
  • Loading branch information
b32147 committed Aug 29, 2024
1 parent 2b32de5 commit f956917
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/hypatio/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
re_path(r'^data-challenges/$', list_data_challenges, name='data-challenges'),
re_path(r'^software-projects/$', list_software_projects, name='software-projects'),
re_path(r'^healthcheck/?', include('health_check.urls')),
re_path(r'^groups/(?P<group_key>[^/]+)/?', GroupView.as_view(), name="group"),
re_path(r'^', index, name='index'),
re_path(r'^groups/(?P<group_key>[^/]+)/?$', GroupView.as_view(), name="group"),
re_path(r'^/?$', index, name='index'),
]
29 changes: 24 additions & 5 deletions app/hypatio/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import os

from django.shortcuts import render
from django.utils.functional import SimpleLazyObject
from django.urls import resolve, Resolver404

from hypatio.auth0authenticate import public_user_auth_and_jwt
from projects.apps import ProjectsConfig
from projects.models import Group, DataProject

import logging
logger = logging.getLogger(__name__)


@public_user_auth_and_jwt
def index(request, template_name='index.html'):
"""
Expand Down Expand Up @@ -32,11 +38,24 @@ def group_context():
# Check for an active project and determine its group
groups = Group.objects.filter(dataproject__isnull=False, dataproject__visible=True).distinct()
active_group = None
project = DataProject.objects.filter(project_key=os.path.basename(os.path.normpath(request.path))).first()
if project:

# Check for group
active_group = next((g for g in groups if project in g.dataproject_set.all()), None)
# Attempt to resolve the current URL
try:
match = resolve(request.path)

# Check if projects
if match and match.app_name == ProjectsConfig.name and "project_key" in match.kwargs:
project = DataProject.objects.filter(project_key=match.kwargs["project_key"]).first()
if project:

# Check for group
active_group = next((g for g in groups if project in g.dataproject_set.all()), None)

except Resolver404:
logger.debug(f"Path could not be resolved: {request.path}")

except Exception as e:
logger.exception(f"Group context error: {e}", exc_info=True)

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

0 comments on commit f956917

Please sign in to comment.