mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-08 08:38:16 -06:00
Misc cleanup
This commit is contained in:
parent
65cd124f6f
commit
09024283b8
@ -5,15 +5,14 @@ from dataclasses import dataclass, field
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from django.contrib import messages
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.utils.translation import gettext_lazy as _
|
|
||||||
|
|
||||||
from netbox.plugins import PluginConfig
|
from netbox.plugins import PluginConfig
|
||||||
from utilities.datetime import datetime_from_timestamp
|
from utilities.datetime import datetime_from_timestamp
|
||||||
|
|
||||||
USER_AGENT_STRING = f'NetBox/{settings.RELEASE.version} {settings.RELEASE.edition}'
|
USER_AGENT_STRING = f'NetBox/{settings.RELEASE.version} {settings.RELEASE.edition}'
|
||||||
|
CACHE_KEY_CATALOG_FEED = 'plugins-catalog-feed'
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -69,11 +68,14 @@ class Plugin:
|
|||||||
installed_version: str = ''
|
installed_version: str = ''
|
||||||
|
|
||||||
|
|
||||||
def get_local_plugins(plugins={}):
|
def get_local_plugins(plugins=None):
|
||||||
"""
|
"""
|
||||||
Return a dictionary of all locally-installed plugins, mapped by name.
|
Return a dictionary of all locally-installed plugins, mapped by name.
|
||||||
"""
|
"""
|
||||||
|
plugins = plugins or {}
|
||||||
local_plugins = {}
|
local_plugins = {}
|
||||||
|
|
||||||
|
# Gather all locally-installed plugins
|
||||||
for plugin_name in settings.PLUGINS:
|
for plugin_name in settings.PLUGINS:
|
||||||
plugin = importlib.import_module(plugin_name)
|
plugin = importlib.import_module(plugin_name)
|
||||||
plugin_config: PluginConfig = plugin.config
|
plugin_config: PluginConfig = plugin.config
|
||||||
@ -88,6 +90,7 @@ def get_local_plugins(plugins={}):
|
|||||||
installed_version=plugin_config.version,
|
installed_version=plugin_config.version,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Update catalog entries for local plugins, or add them to the list if not listed
|
||||||
for k, v in local_plugins.items():
|
for k, v in local_plugins.items():
|
||||||
if k in plugins:
|
if k in plugins:
|
||||||
plugins[k].is_local = True
|
plugins[k].is_local = True
|
||||||
@ -196,11 +199,11 @@ def get_catalog_plugins():
|
|||||||
|
|
||||||
return plugins
|
return plugins
|
||||||
|
|
||||||
catalog_plugins = cache.get('plugins-catalog-feed', default={})
|
catalog_plugins = cache.get(CACHE_KEY_CATALOG_FEED, default={})
|
||||||
if not catalog_plugins:
|
if not catalog_plugins:
|
||||||
try:
|
try:
|
||||||
catalog_plugins = make_plugin_dict()
|
catalog_plugins = make_plugin_dict()
|
||||||
cache.set('plugins-catalog-feed', catalog_plugins, 3600)
|
cache.set(CACHE_KEY_CATALOG_FEED, catalog_plugins, 3600)
|
||||||
except requests.exceptions.RequestException:
|
except requests.exceptions.RequestException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -651,18 +651,19 @@ class SystemView(UserPassesTestMixin, View):
|
|||||||
#
|
#
|
||||||
|
|
||||||
class BasePluginView(UserPassesTestMixin, View):
|
class BasePluginView(UserPassesTestMixin, View):
|
||||||
|
CACHE_KEY_CATALOG_ERROR = 'plugins-catalog-error'
|
||||||
|
|
||||||
def test_func(self):
|
def test_func(self):
|
||||||
return self.request.user.is_staff
|
return self.request.user.is_staff
|
||||||
|
|
||||||
def get_cached_plugins(self, request):
|
def get_cached_plugins(self, request):
|
||||||
catalog_plugins = {}
|
catalog_plugins = {}
|
||||||
catalog_plugins_error = cache.get('plugins-catalog-error', default=False)
|
catalog_plugins_error = cache.get(self.CACHE_KEY_CATALOG_ERROR, default=False)
|
||||||
if not catalog_plugins_error:
|
if not catalog_plugins_error:
|
||||||
catalog_plugins = get_catalog_plugins()
|
catalog_plugins = get_catalog_plugins()
|
||||||
if not catalog_plugins:
|
if not catalog_plugins:
|
||||||
# Cache for 5 minutes to avoid spamming connection
|
# Cache for 5 minutes to avoid spamming connection
|
||||||
cache.set('plugins-catalog-error', True, 300)
|
cache.set(self.CACHE_KEY_CATALOG_ERROR, True, 300)
|
||||||
messages.warning(request, _("Plugins catalog could not be loaded"))
|
messages.warning(request, _("Plugins catalog could not be loaded"))
|
||||||
|
|
||||||
return get_local_plugins(catalog_plugins)
|
return get_local_plugins(catalog_plugins)
|
||||||
|
Loading…
Reference in New Issue
Block a user