Skip to content

Commit

Permalink
RSS/Atom for Blog (MozillaFoundation#3681)
Browse files Browse the repository at this point in the history
* rss/atom for /blog/ posts
  • Loading branch information
Pomax authored Sep 25, 2019
1 parent 32ff278 commit 183011f
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 38 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ verify_ssl = true
name = "pypi"

[packages]
beautifulsoup4 = "*"
"boto3" = "*"
Django = "==2.2.5"
dj-database-url = "*"
Expand Down
78 changes: 40 additions & 38 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions network-api/networkapi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
CLOUDINARY_CLOUD_NAME=(str, ''),
CLOUDINARY_API_KEY=(str, ''),
CLOUDINARY_API_SECRET=(str, ''),
FEED_LIMIT=(int, 10),
)

# Read in the environment
Expand Down Expand Up @@ -587,3 +588,6 @@

# Use network_url to check if we're running prod or not
NETWORK_SITE_URL = env('NETWORK_SITE_URL')

# RSS / ATOM settings
FEED_LIMIT = env('FEED_LIMIT')
7 changes: 7 additions & 0 deletions network-api/networkapi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic.base import RedirectView
from django.urls import path

from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
Expand All @@ -12,6 +13,7 @@

from networkapi.views import EnvVariablesView, review_app_help_view
from networkapi.buyersguide import views as buyersguide_views
from networkapi.wagtailpages.rss import RSSFeed, AtomFeed
from experiments import views as experiment_views

admin.autodiscover()
Expand Down Expand Up @@ -55,6 +57,11 @@
# Buyer's Guide / Privacy Not Included
url(r'^privacynotincluded/', include('networkapi.buyersguide.urls')),

# Blog RSS feed
path('blog/rss/', RSSFeed()),
path('blog/atom/', AtomFeed()),

# wagtail-managed data
url(r'', include(wagtail_urls)),
)

Expand Down
48 changes: 48 additions & 0 deletions network-api/networkapi/wagtailpages/rss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django.conf import settings
from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Atom1Feed

from bs4 import BeautifulSoup

from .models import IndexPage


class RSSFeed(Feed):
"""
Blog page RSS feed, using the content:encoded serializer above
"""

title = 'Mozilla Foundation Blog'
link = 'https://foundation.mozilla.org/blog/'
feed_url = 'https://foundation.mozilla.org/blog/rss/'
description = 'The Mozilla Foundation Blog'

def items(self):
blog_index = IndexPage.objects.get(title__iexact='blog')
blog_pages = blog_index.get_all_entries()
return blog_pages[:settings.FEED_LIMIT]

def item_title(self, item):
return item.title

def item_link(self, item):
return item.full_url

def item_description(self, item):
page = item.specific
html = str(page.body)
parsed = BeautifulSoup(html, 'html.parser')
text = parsed.get_text()[:1000]
if len(text) == 1000:
text = f'{text}[...]'
return text

def item_pubdate(self, item):
return item.first_published_at


class AtomFeed(RSSFeed):
feed_type = Atom1Feed
link = RSSFeed.link
feed_url = 'https://foundation.mozilla.org/blog/atom/'
subtitle = RSSFeed.description

0 comments on commit 183011f

Please sign in to comment.