Skip to content

Commit 9319cff

Browse files
committed
Fixes #12384: Add a three-second timeout for RSS reader widget
1 parent 261f5e4 commit 9319cff

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

docs/release-notes/version-3.5.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Bug Fixes
66

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

netbox/extras/dashboard/widgets.py

+32-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from urllib.parse import urlencode
55

66
import feedparser
7+
import requests
78
from django import forms
89
from django.conf import settings
910
from django.contrib.contenttypes.models import ContentType
@@ -269,12 +270,9 @@ class ConfigForm(WidgetConfigForm):
269270
)
270271

271272
def render(self, request):
272-
url = self.config['feed_url']
273-
feed = self.get_feed()
274-
275273
return render_to_string(self.template_name, {
276-
'url': url,
277-
'feed': feed,
274+
'url': self.config['feed_url'],
275+
**self.get_feed()
278276
})
279277

280278
@cached_property
@@ -286,17 +284,33 @@ def cache_key(self):
286284
def get_feed(self):
287285
# Fetch RSS content from cache if available
288286
if feed_content := cache.get(self.cache_key):
289-
feed = feedparser.FeedParserDict(feed_content)
290-
else:
291-
feed = feedparser.parse(
292-
self.config['feed_url'],
293-
request_headers={'User-Agent': f'NetBox/{settings.VERSION}'}
287+
return {
288+
'feed': feedparser.FeedParserDict(feed_content),
289+
}
290+
291+
# Fetch feed content from remote server
292+
try:
293+
response = requests.get(
294+
url=self.config['feed_url'],
295+
headers={'User-Agent': f'NetBox/{settings.VERSION}'},
296+
proxies=settings.HTTP_PROXIES,
297+
timeout=3
294298
)
295-
if not feed.bozo:
296-
# Cap number of entries
297-
max_entries = self.config.get('max_entries')
298-
feed['entries'] = feed['entries'][:max_entries]
299-
# Cache the feed content
300-
cache.set(self.cache_key, dict(feed), self.config.get('cache_timeout'))
301-
302-
return feed
299+
response.raise_for_status()
300+
except requests.exceptions.RequestException as e:
301+
return {
302+
'error': e,
303+
}
304+
305+
# Parse feed content
306+
feed = feedparser.parse(response.content)
307+
if not feed.bozo:
308+
# Cap number of entries
309+
max_entries = self.config.get('max_entries')
310+
feed['entries'] = feed['entries'][:max_entries]
311+
# Cache the feed content
312+
cache.set(self.cache_key, dict(feed), self.config.get('cache_timeout'))
313+
314+
return {
315+
'feed': feed,
316+
}

netbox/templates/extras/dashboard/widgets/rssfeed.html

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% if not feed.bozo %}
1+
{% if feed and not feed.bozo %}
22
<div class="list-group list-group-flush">
33
{% for entry in feed.entries %}
44
<div class="list-group-item px-1">
@@ -16,7 +16,9 @@ <h6><a href="{{ entry.link }}">{{ entry.title }}</a></h6>
1616
<span class="text-danger">
1717
<i class="mdi mdi-alert"></i> There was a problem fetching the RSS feed:
1818
</span>
19-
<pre class="m-2">
20-
Response status: {{ feed.status }}
21-
Error: {{ feed.bozo_exception|escape }}</pre>
19+
{% if feed %}
20+
{{ feed.bozo_exception|escape }} (HTTP {{ feed.status }})
21+
{% else %}
22+
{{ error }}
23+
{% endif %}
2224
{% endif %}

0 commit comments

Comments
 (0)