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

Fixes #12384: Add a three-second timeout for RSS reader widget

This commit is contained in:
jeremystretch
2023-05-01 16:19:39 -04:00
parent 261f5e4995
commit 9319cffb1c
3 changed files with 39 additions and 22 deletions

View File

@ -5,6 +5,7 @@
### Bug Fixes ### Bug Fixes
* [#12380](https://github.com/netbox-community/netbox/issues/12380) - Allow selecting object change as model under object list widget configuration * [#12380](https://github.com/netbox-community/netbox/issues/12380) - Allow selecting object change as model under object list widget configuration
* [#12384](https://github.com/netbox-community/netbox/issues/12384) - Add a three-second timeout for RSS reader widget
* [#12395](https://github.com/netbox-community/netbox/issues/12395) - Fix "create & add another" action for objects with custom fields * [#12395](https://github.com/netbox-community/netbox/issues/12395) - Fix "create & add another" action for objects with custom fields
* [#12396](https://github.com/netbox-community/netbox/issues/12396) - Provider account should not be a required field in REST API serializer * [#12396](https://github.com/netbox-community/netbox/issues/12396) - Provider account should not be a required field in REST API serializer
* [#12405](https://github.com/netbox-community/netbox/issues/12405) - Fix filtering for VLAN groups displayed under site view * [#12405](https://github.com/netbox-community/netbox/issues/12405) - Fix filtering for VLAN groups displayed under site view

View File

@ -4,6 +4,7 @@ from hashlib import sha256
from urllib.parse import urlencode from urllib.parse import urlencode
import feedparser import feedparser
import requests
from django import forms from django import forms
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
@ -269,12 +270,9 @@ class RSSFeedWidget(DashboardWidget):
) )
def render(self, request): def render(self, request):
url = self.config['feed_url']
feed = self.get_feed()
return render_to_string(self.template_name, { return render_to_string(self.template_name, {
'url': url, 'url': self.config['feed_url'],
'feed': feed, **self.get_feed()
}) })
@cached_property @cached_property
@ -286,17 +284,33 @@ class RSSFeedWidget(DashboardWidget):
def get_feed(self): def get_feed(self):
# Fetch RSS content from cache if available # Fetch RSS content from cache if available
if feed_content := cache.get(self.cache_key): if feed_content := cache.get(self.cache_key):
feed = feedparser.FeedParserDict(feed_content) return {
else: 'feed': feedparser.FeedParserDict(feed_content),
feed = feedparser.parse( }
self.config['feed_url'],
request_headers={'User-Agent': f'NetBox/{settings.VERSION}'}
)
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 # Fetch feed content from remote server
try:
response = requests.get(
url=self.config['feed_url'],
headers={'User-Agent': f'NetBox/{settings.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,
}

View File

@ -1,4 +1,4 @@
{% if not feed.bozo %} {% if feed and not feed.bozo %}
<div class="list-group list-group-flush"> <div class="list-group list-group-flush">
{% for entry in feed.entries %} {% for entry in feed.entries %}
<div class="list-group-item px-1"> <div class="list-group-item px-1">
@ -16,7 +16,9 @@
<span class="text-danger"> <span class="text-danger">
<i class="mdi mdi-alert"></i> There was a problem fetching the RSS feed: <i class="mdi mdi-alert"></i> There was a problem fetching the RSS feed:
</span> </span>
<pre class="m-2"> {% if feed %}
Response status: {{ feed.status }} {{ feed.bozo_exception|escape }} (HTTP {{ feed.status }})
Error: {{ feed.bozo_exception|escape }}</pre> {% else %}
{{ error }}
{% endif %}
{% endif %} {% endif %}