|
1 |
| -import os |
| 1 | + |
2 | 2 | from django.shortcuts import render
|
3 | 3 | from django.utils.functional import SimpleLazyObject
|
| 4 | +from django.urls import resolve, Resolver404 |
4 | 5 |
|
5 | 6 | from hypatio.auth0authenticate import public_user_auth_and_jwt
|
| 7 | +from projects.apps import ProjectsConfig |
6 | 8 | from projects.models import Group, DataProject
|
7 | 9 |
|
| 10 | +import logging |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
8 | 14 | @public_user_auth_and_jwt
|
9 | 15 | def index(request, template_name='index.html'):
|
10 | 16 | """
|
@@ -32,11 +38,24 @@ def group_context():
|
32 | 38 | # Check for an active project and determine its group
|
33 | 39 | groups = Group.objects.filter(dataproject__isnull=False, dataproject__visible=True).distinct()
|
34 | 40 | active_group = None
|
35 |
| - project = DataProject.objects.filter(project_key=os.path.basename(os.path.normpath(request.path))).first() |
36 |
| - if project: |
37 | 41 |
|
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) |
40 | 59 |
|
41 | 60 | # Pull out top-level groups
|
42 | 61 | parent_groups_keys = groups.filter(parent__isnull=False).values_list('parent', flat=True)
|
|
0 commit comments