فهرست منبع

Closes #12062: Avoid caching invalid RSS feed content

jeremystretch 2 سال پیش
والد
کامیت
669cfe8952
2فایلهای تغییر یافته به همراه28 افزوده شده و 17 حذف شده
  1. 7 5
      netbox/extras/dashboard/widgets.py
  2. 21 12
      netbox/templates/extras/dashboard/widgets/rssfeed.html

+ 7 - 5
netbox/extras/dashboard/widgets.py

@@ -193,14 +193,16 @@ class RSSFeedWidget(DashboardWidget):
         return f'dashboard_rss_{url_checksum}'
 
     def get_feed(self):
-        # Fetch RSS content from cache
+        # Fetch RSS content from cache if available
         if feed_content := cache.get(self.cache_key):
             feed = feedparser.FeedParserDict(feed_content)
         else:
             feed = feedparser.parse(self.config['feed_url'])
-            # Cap number of entries
-            max_entries = self.config.get('max_entries')
-            feed['entries'] = feed['entries'][:max_entries]
-            cache.set(self.cache_key, dict(feed), self.config.get('cache_timeout'))
+            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

+ 21 - 12
netbox/templates/extras/dashboard/widgets/rssfeed.html

@@ -1,13 +1,22 @@
-<div class="list-group list-group-flush">
-  {% for entry in feed.entries %}
-    <div class="list-group-item px-1">
-      <h6><a href="{{ entry.link }}">{{ entry.title }}</a></h6>
-      <div>
-        {{ entry.summary|safe }}
+{% if not feed.bozo %}
+  <div class="list-group list-group-flush">
+    {% for entry in feed.entries %}
+      <div class="list-group-item px-1">
+        <h6><a href="{{ entry.link }}">{{ entry.title }}</a></h6>
+        <div>
+          {{ entry.summary|safe }}
+        </div>
       </div>
-    </div>
-  {% empty %}
-    <div class="list-group-item text-muted">No content found</div>
-  {% endfor %}
-</div>
-
+    {% empty %}
+      <div class="list-group-item text-muted">No content found</div>
+    {% endfor %}
+  </div>
+{% else %}
+  {# There was an error retrieving/parsing the feed #}
+  <span class="text-danger">
+    <i class="mdi mdi-alert"></i> There was a problem fetching the RSS feed:
+  </span>
+  <pre class="m-2">
+Response status: {{ feed.status }}
+Error: {{ feed.bozo_exception|escape }}</pre>
+{% endif %}