refactor: use BASE_DIR for project_root

This commit is contained in:
Juro Oravec 2024-04-14 20:21:13 +02:00
parent 752567bf7c
commit 9104a43944

View file

@ -4,6 +4,7 @@ import os
from pathlib import Path
import django
from django.conf import settings
from django.utils.module_loading import autodiscover_modules
from django_components.logger import logger
@ -45,7 +46,10 @@ def _filepath_to_python_module(file_path: Path) -> str:
- Then the path relative to project root is `app/components/mycomp.py`
- Which we then turn into python import path `app.components.mycomp`
"""
# Get the root dir, see https://stackoverflow.com/a/16413955/9788634
if hasattr(settings, "BASE_DIR"):
project_root = str(settings.BASE_DIR)
else:
# Fallback for getting the root dir, see https://stackoverflow.com/a/16413955/9788634
project_root = os.path.abspath(os.path.dirname(__name__))
rel_path = os.path.relpath(file_path, start=project_root)