This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git


The following commit(s) were added to refs/heads/develop by this push:
     new 0e2736ba Factored out dynamic django app loading code
0e2736ba is described below

commit 0e2736ba1e72d24fa47b1699a11cbda4dc3fcc4c
Author: Marcus Christie <[email protected]>
AuthorDate: Tue Nov 1 12:50:50 2022 -0400

    Factored out dynamic django app loading code
---
 django_airavata/app_config.py         | 49 -----------------------------------
 django_airavata/context_processors.py | 28 +-------------------
 django_airavata/settings.py           | 43 +++++++-----------------------
 django_airavata/urls.py               | 19 +++-----------
 requirements.txt                      |  1 +
 5 files changed, 14 insertions(+), 126 deletions(-)

diff --git a/django_airavata/app_config.py b/django_airavata/app_config.py
index f34195d2..718e851f 100644
--- a/django_airavata/app_config.py
+++ b/django_airavata/app_config.py
@@ -41,60 +41,11 @@ class AiravataAppConfig(AppConfig, ABC):
         pass
 
 
-def enhance_custom_app_config(app):
-    """As necessary add default values for properties to custom AppConfigs."""
-    app.url_app_name = get_url_app_name(app)
-    app.url_home = get_url_home(app)
-    app.fa_icon_class = get_fa_icon_class(app)
-    app.app_description = get_app_description(app)
-    return app
-
-
 def get_url_app_name(app_config):
     """Return the urls namespace for the given AppConfig instance."""
     urls = get_app_urls(app_config)
     return getattr(urls, 'app_name', None)
 
 
-def get_url_home(app_config):
-    """Get named URL of home page of app."""
-    if hasattr(app_config, 'url_home'):
-        return app_config.url_home
-    else:
-        return get_default_url_home(app_config)
-
-
-def get_default_url_home(app_config):
-    """Return first url pattern as a default."""
-    urls = get_app_urls(app_config)
-    app_name = get_url_app_name(app_config)
-    logger.warning("Custom Django app {} has no URL namespace "
-                   "defined".format(app_config.label))
-    first_named_url = None
-    for urlpattern in urls.urlpatterns:
-        if hasattr(urlpattern, 'name'):
-            first_named_url = urlpattern.name
-            break
-    if not first_named_url:
-        raise Exception(f"{urls} has no named urls, can't figure out default 
home URL")
-    if app_name:
-        return app_name + ":" + first_named_url
-    else:
-        return first_named_url
-
-
-def get_fa_icon_class(app_config):
-    """Return Font Awesome icon class to use for app."""
-    if hasattr(app_config, "fa_icon_class"):
-        return app_config.fa_icon_class
-    else:
-        return 'fa-circle'
-
-
-def get_app_description(app_config):
-    """Return brief description of app."""
-    return getattr(app_config, 'app_description', None)
-
-
 def get_app_urls(app_config):
     return import_module(".urls", app_config.name)
diff --git a/django_airavata/context_processors.py 
b/django_airavata/context_processors.py
index 6fa83380..43cbfcf9 100644
--- a/django_airavata/context_processors.py
+++ b/django_airavata/context_processors.py
@@ -9,10 +9,7 @@ from django.conf import settings
 from django.core.exceptions import ObjectDoesNotExist
 from django.urls import reverse
 
-from django_airavata.app_config import (
-    AiravataAppConfig,
-    enhance_custom_app_config
-)
+from django_airavata.app_config import AiravataAppConfig
 from django_airavata.apps.api.models import User_Notifications
 
 logger = logging.getLogger(__name__)
@@ -99,24 +96,6 @@ def airavata_app_registry(request):
     }
 
 
-def custom_app_registry(request):
-    """Put custom Django apps into the context."""
-    custom_apps = settings.CUSTOM_DJANGO_APPS.copy()
-    custom_apps = [enhance_custom_app_config(app) for app in custom_apps
-                   if (getattr(app, 'enabled', None) is None or
-                       app.enabled(request)
-                       )]
-    custom_apps.sort(key=lambda app: app.verbose_name.lower())
-    current_custom_app = _get_current_app(request, custom_apps)
-    return {
-        # 'custom_apps': list(map(_app_to_dict, custom_apps)),
-        'custom_apps': custom_apps,
-        'current_custom_app': current_custom_app,
-        'custom_app_nav': (_get_app_nav(request, current_custom_app)
-                           if current_custom_app else None)
-    }
-
-
 def _get_current_app(request, apps):
     current_app = [
         app for app in apps
@@ -155,11 +134,6 @@ def _get_app_nav(request, current_app):
     return nav
 
 
-def resolver_match(request):
-    """Put resolver_match (ResolverMatch instance) into the context."""
-    return {'resolver_match': request.resolver_match}
-
-
 def google_analytics_tracking_id(request):
     """Put the Google Analytics tracking id into context."""
     return {'ga_tracking_id':
diff --git a/django_airavata/settings.py b/django_airavata/settings.py
index d17592ca..0f2152b8 100644
--- a/django_airavata/settings.py
+++ b/django_airavata/settings.py
@@ -11,8 +11,9 @@ https://docs.djangoproject.com/en/1.10/ref/settings/
 """
 
 import os
-from importlib import import_module
+import sys
 
+from airavata_django_portal_commons import dynamic_apps
 from pkg_resources import iter_entry_points
 
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@@ -118,11 +119,10 @@ TEMPLATES = [
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
                 'django_airavata.context_processors.airavata_app_registry',
-                'django_airavata.context_processors.custom_app_registry',
+                
'airavata_django_portal_commons.dynamic_apps.context_processors.custom_app_registry',
                 'django_airavata.context_processors.get_notifications',
                 'django_airavata.context_processors.user_session_data',
                 
'django_airavata.context_processors.google_analytics_tracking_id',
-                # 'django_airavata.context_processors.resolver_match',
             ],
         },
     },
@@ -610,10 +610,8 @@ except ImportError:
 # NOTE: custom code must be loaded last so that the above settings take effect
 # for any views, etc. defined or imported by custom code
 
-# AppConfig instances from custom Django apps
-CUSTOM_DJANGO_APPS = []
-
 # Add any custom apps installed in the virtual environment
+
 # Essentially this looks for the entry_points metadata in all installed Python 
packages. The format of the metadata in setup.py is the following:
 #
 #    setuptools.setup(
@@ -625,34 +623,11 @@ CUSTOM_DJANGO_APPS = []
 #        ...
 #    )
 #
-for entry_point in iter_entry_points(group='airavata.djangoapp'):
-    custom_app_class = entry_point.load()
-    custom_app_instance = custom_app_class(
-        entry_point.name, import_module(entry_point.module_name))
-    CUSTOM_DJANGO_APPS.append(custom_app_instance)
-    # Create path to AppConfig class (otherwise the ready() method doesn't get
-    # called)
-    INSTALLED_APPS.append("{}.{}".format(entry_point.module_name,
-                                         entry_point.attrs[0]))
-
-
-def merge_setting(default, custom_setting):
-    # FIXME: only handles dict settings, doesn't handle lists
-    if isinstance(custom_setting, dict):
-        for k in custom_setting.keys():
-            if k not in default:
-                default[k] = custom_setting[k]
-            else:
-                raise Exception("Custom django app setting conflicts with "
-                                "key {} in {}".format(k, default))
-
-
-# Merge settings from custom Django apps
-# FIXME: only handles WEBPACK_LOADER additions
-for custom_django_app in CUSTOM_DJANGO_APPS:
-    if hasattr(custom_django_app, 'settings'):
-        s = custom_django_app.settings
-        merge_setting(WEBPACK_LOADER, getattr(s, 'WEBPACK_LOADER', {}))
+dynamic_apps.load(INSTALLED_APPS, "airavata.djangoapp")
+
+# Merge WEBPACK_LOADER settings from custom Django apps
+settings_module = sys.modules[__name__]
+dynamic_apps.merge_settings(settings_module)
 
 OUTPUT_VIEW_PROVIDERS = {}
 for entry_point in iter_entry_points(group='airavata.output_view_providers'):
diff --git a/django_airavata/urls.py b/django_airavata/urls.py
index 2ee2ac19..5c0dcf78 100644
--- a/django_airavata/urls.py
+++ b/django_airavata/urls.py
@@ -41,24 +41,11 @@ urlpatterns = [
     re_path(r'^403/', views.error403),
     re_path(r'^404/', views.error404),
     re_path(r'^500/', views.error500),
-]
+    path('', include('airavata_django_portal_commons.dynamic_apps.urls')),
+    path('', include(wagtail_urls)),
+] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
 
 handler400 = views.error400
 handler403 = views.error403
 handler404 = views.error404
 handler500 = views.error500
-
-# Add custom Django app urls patterns
-for custom_django_app in settings.CUSTOM_DJANGO_APPS:
-    # Custom Django apps may define a url_prefix, otherwise label will be used
-    # as url prefix
-    url_prefix = getattr(
-        custom_django_app,
-        'url_prefix',
-        custom_django_app.label)
-    urlpatterns.append(re_path(r'^' + url_prefix + '/',
-                               include(custom_django_app.name + ".urls")))
-
-urlpatterns += [
-    re_path(r'', include(wagtail_urls)),
-] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
diff --git a/requirements.txt b/requirements.txt
index 6fb37562..3275f8f2 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -23,5 +23,6 @@ grpcio==1.34.1
 
 airavata-django-portal-sdk==1.6.0
 airavata-python-sdk==1.0.2
+airavata-django-portal-commons==1.0.0
 
 -e "."

Reply via email to