mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-08 16:48:16 -06:00
14731 cache API results
This commit is contained in:
parent
1e26ae79b0
commit
0143a5599a
@ -683,6 +683,40 @@ def get_local_plugins(plugins):
|
|||||||
|
|
||||||
return plugins
|
return plugins
|
||||||
|
|
||||||
|
def get_feed(self):
|
||||||
|
# Fetch RSS content from cache if available
|
||||||
|
if feed_content := cache.get(self.cache_key):
|
||||||
|
return {
|
||||||
|
'feed': feedparser.FeedParserDict(feed_content),
|
||||||
|
}
|
||||||
|
|
||||||
|
# Fetch feed content from remote server
|
||||||
|
try:
|
||||||
|
response = requests.get(
|
||||||
|
url=self.config['feed_url'],
|
||||||
|
headers={'User-Agent': f'NetBox/{settings.RELEASE.version}'},
|
||||||
|
proxies=settings.HTTP_PROXIES,
|
||||||
|
timeout=3
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
return {
|
||||||
|
'error': e,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Parse feed content
|
||||||
|
feed = feedparser.parse(response.content)
|
||||||
|
if not feed.bozo:
|
||||||
|
# Cap number of entries
|
||||||
|
max_entries = self.config.get('max_entries')
|
||||||
|
feed['entries'] = feed['entries'][:max_entries]
|
||||||
|
# Cache the feed content
|
||||||
|
cache.set(self.cache_key, dict(feed), self.config.get('cache_timeout'))
|
||||||
|
|
||||||
|
return {
|
||||||
|
'feed': feed,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_catalog_plugins(plugins):
|
def get_catalog_plugins(plugins):
|
||||||
url = 'https://api.netbox.oss.netboxlabs.com/v1/plugins'
|
url = 'https://api.netbox.oss.netboxlabs.com/v1/plugins'
|
||||||
@ -729,6 +763,20 @@ def get_catalog_plugins(plugins):
|
|||||||
return plugins
|
return plugins
|
||||||
|
|
||||||
|
|
||||||
|
def get_plugins():
|
||||||
|
if plugins := cache.get('plugins-catalog-feed'):
|
||||||
|
return plugins
|
||||||
|
|
||||||
|
plugins = {}
|
||||||
|
plugins = get_local_plugins(plugins)
|
||||||
|
plugins = get_catalog_plugins(plugins)
|
||||||
|
plugins = [v for k, v in plugins.items()]
|
||||||
|
plugins = sorted(plugins, key=lambda d: d['name'])
|
||||||
|
|
||||||
|
cache.set('plugins-catalog-feed', plugins, 3600)
|
||||||
|
return plugins
|
||||||
|
|
||||||
|
|
||||||
class PluginListView(UserPassesTestMixin, View):
|
class PluginListView(UserPassesTestMixin, View):
|
||||||
|
|
||||||
def test_func(self):
|
def test_func(self):
|
||||||
@ -737,11 +785,7 @@ class PluginListView(UserPassesTestMixin, View):
|
|||||||
def get(self, request):
|
def get(self, request):
|
||||||
|
|
||||||
# Plugins
|
# Plugins
|
||||||
plugins = {}
|
plugins = get_plugins()
|
||||||
plugins = get_local_plugins(plugins)
|
|
||||||
plugins = get_catalog_plugins(plugins)
|
|
||||||
plugins = [v for k, v in plugins.items()]
|
|
||||||
plugins = sorted(plugins, key=lambda d: d['name'])
|
|
||||||
|
|
||||||
return render(request, 'core/plugin_list.html', {
|
return render(request, 'core/plugin_list.html', {
|
||||||
'plugins': plugins,
|
'plugins': plugins,
|
||||||
|
Loading…
Reference in New Issue
Block a user