1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Refactor repeated import code

This commit is contained in:
Glenn Matthews
2020-07-14 17:15:17 -04:00
parent f807d3a024
commit 0fd3c83861
4 changed files with 58 additions and 64 deletions

View File

@ -1,6 +1,4 @@
from collections import OrderedDict
import importlib
import sys
from django.apps import apps
from django.conf import settings
@ -12,6 +10,8 @@ from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.views import APIView
from extras.plugins.utils import import_object
class InstalledPluginsAdminView(View):
"""
@ -62,22 +62,10 @@ class PluginsAPIRootView(APIView):
@staticmethod
def _get_plugin_entry(plugin, app_config, request, format):
# Check if the plugin specifies any API URLs
spec = importlib.util.find_spec(f"{plugin}.api")
if spec is None:
# There is no plugin.api module
api_app_name = import_object(f"{plugin}.api.urls.app_name")
if api_app_name is None:
# Plugin does not expose an API
return None
spec = importlib.util.find_spec(f"{plugin}.api.urls")
if spec is None:
# There is no plugin.api.urls module
return None
# The plugin has a .api.urls module - import it
api_urls = importlib.util.module_from_spec(spec)
sys.modules[f"{plugin}.api.urls"] = api_urls
spec.loader.exec_module(api_urls)
if not hasattr(api_urls, "app_name"):
# The plugin api.urls does not declare an app_name string
return None
api_app_name = api_urls.app_name
try:
entry = (getattr(app_config, 'base_url', app_config.label), reverse(