mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-08 00:28:16 -06:00
17034 add error handling to plugins catalog API
This commit is contained in:
parent
954eadcdc5
commit
f53ff24885
@ -5,6 +5,7 @@ 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 django.utils.translation import gettext_lazy as _
|
||||||
@ -187,15 +188,21 @@ def get_catalog_plugins():
|
|||||||
return plugins
|
return plugins
|
||||||
|
|
||||||
|
|
||||||
def get_plugins():
|
def get_plugins(request):
|
||||||
"""
|
"""
|
||||||
Return a dictionary of all plugins (both catalog and locally installed), mapped by name.
|
Return a dictionary of all plugins (both catalog and locally installed), mapped by name.
|
||||||
"""
|
"""
|
||||||
local_plugins = get_local_plugins()
|
local_plugins = get_local_plugins()
|
||||||
catalog_plugins = cache.get('plugins-catalog-feed')
|
catalog_plugins = cache.get('plugins-catalog-feed', default={})
|
||||||
if not catalog_plugins:
|
catalog_plugins_error = cache.get('plugins-catalog-error', default=False)
|
||||||
catalog_plugins = get_catalog_plugins()
|
if not catalog_plugins and not catalog_plugins_error:
|
||||||
cache.set('plugins-catalog-feed', catalog_plugins, 3600)
|
try:
|
||||||
|
catalog_plugins = get_catalog_plugins()
|
||||||
|
cache.set('plugins-catalog-feed', catalog_plugins, 3600)
|
||||||
|
except requests.exceptions.RequestException:
|
||||||
|
# Cache for 15 minutes to avoid spamming connection
|
||||||
|
cache.set('plugins-catalog-error', True, 900)
|
||||||
|
messages.warning(request, _("Plugins catalog could not be loaded"))
|
||||||
|
|
||||||
plugins = catalog_plugins
|
plugins = catalog_plugins
|
||||||
for k, v in local_plugins.items():
|
for k, v in local_plugins.items():
|
||||||
|
@ -658,7 +658,7 @@ class PluginListView(UserPassesTestMixin, View):
|
|||||||
def get(self, request):
|
def get(self, request):
|
||||||
q = request.GET.get('q', None)
|
q = request.GET.get('q', None)
|
||||||
|
|
||||||
plugins = get_plugins().values()
|
plugins = get_plugins(request).values()
|
||||||
if q:
|
if q:
|
||||||
plugins = [obj for obj in plugins if q.casefold() in obj.title_short.casefold()]
|
plugins = [obj for obj in plugins if q.casefold() in obj.title_short.casefold()]
|
||||||
|
|
||||||
@ -683,7 +683,7 @@ class PluginView(UserPassesTestMixin, View):
|
|||||||
|
|
||||||
def get(self, request, name):
|
def get(self, request, name):
|
||||||
|
|
||||||
plugins = get_plugins()
|
plugins = get_plugins(request)
|
||||||
if name not in plugins:
|
if name not in plugins:
|
||||||
raise Http404(_("Plugin {name} not found").format(name=name))
|
raise Http404(_("Plugin {name} not found").format(name=name))
|
||||||
plugin = plugins[name]
|
plugin = plugins[name]
|
||||||
|
Loading…
Reference in New Issue
Block a user