Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Adding feed stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Sinchok committed Jun 9, 2014
1 parent 52de1db commit 520da22
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 2 deletions.
Empty file added bulbs/feeds/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions bulbs/feeds/templates/feeds/_rss.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% load betty %}<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title>{{ title|default:"RSS Feed"}}</title>
<link>{{ search_url }}</link>
<description>{{ description|default:"RSS Feed"}}</description>
<atom:link href="{{ feed_url|escape }}" rel="self"></atom:link>
<lastBuildDate>{{ build_date|date:"D, d M Y H:i:s O" }}</lastBuildDate>
{% for content in page_obj.object_list %}<item>
<title>{{ content.feature_type }}: {{ content.title|striptags }}</title>
{% if content.authors.all %}<author>{% for author in content.authors.all %}{{ author.get_full_name }}{% if not forloop.last %}, {% endif %}{% endfor%}</author>{% endif %}
<link>{{ content.feed_url }}</link>
<description>
<![CDATA[
{{ content.description|safe }}
]]>
</description>
{% if full %}
<content:encoded>
{{ content.description|safe }}
</content:encoded>
{% endif %}
<pubDate>{{ content.published|date:"D, d M Y H:i:s O" }}</pubDate>
<guid>{{ content.feed_url }}</guid>
</item>{% endfor%}
</channel>
</rss>
8 changes: 8 additions & 0 deletions bulbs/feeds/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls import url, patterns

from .views import RSSView


urlpatterns = patterns("",
url(r"^rss", RSSView.as_view(), name="rss-feed") # noqa
)
42 changes: 42 additions & 0 deletions bulbs/feeds/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.template import RequestContext
from django.utils.timezone import now
from django.views.decorators.cache import cache_control

from bulbs.content.views import ContentListView


class RSSView(ContentListView):
"""Really simply, this syndicates Content."""
template_name = "feeds/rss.xml"
paginate_by = 20
feed_title = "RSS Feed"
utm_params = "utm_medium=RSS&amp;utm_campaign=feeds"

def get_template_names(self):
return ["feeds/rss.xml", "feeds/_rss.xml"]

@cache_control(max_age=600)
def get(self, request, *args, **kwargs):
response = super(RSSView, self).get(request, *args, **kwargs)
response["Content-Type"] = "application/rss+xml"
return response

def get_queryset(self):
return super(RSSView, self).get_queryset().full()

def get_context_data(self, *args, **kwargs):
context = super(RSSView, self).get_context_data(*args, **kwargs)
context["full"] = (self.request.GET.get("full", "false").lower() == "true")
context["images"] = (self.request.GET.get("images", "false").lower() == "true")
context["build_date"] = now()
context["title"] = self.feed_title
context["feed_url"] = self.request.build_absolute_uri()
context["search_url"] = self.request.build_absolute_uri(
u"/search?%s" % self.request.META["QUERY_STRING"])

# OK, so this is kinda brutal. Stay with me here.
for content in context["page_obj"].object_list:
feed_path = content.get_absolute_url() + "?" + self.utm_params
content.feed_url = self.request.build_absolute_uri(feed_path)

return RequestContext(self.request, context)
7 changes: 6 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ def pytest_configure():
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",

"djbetty",
"elastimorphic",
"rest_framework",
"polymorphic",
"elastimorphic",

"bulbs.api",
"bulbs.content",
"bulbs.feeds",
"bulbs.promotion",
"bulbs.redirects",

"tests.testcontent",),
ROOT_URLCONF = 'tests.urls',
TEMPLATE_CONTEXT_PROCESSORS = (
Expand Down
23 changes: 23 additions & 0 deletions tests/test_rss_feeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.core.urlresolvers import reverse
from django.test.client import Client

from elastimorphic.tests.base import BaseIndexableTestCase

from model_mommy import mommy

from tests.testcontent.models import TestContentObj


class RSSTestCase(BaseIndexableTestCase):
"""A base test case, allowing tearDown and setUp of the ES index"""

def test_rss_feed(self):

# Let's bust out some content
mommy.make(TestContentObj, _quantity=100)

rss_endpoint = reverse("rss-feed")

client = Client()
response = client.get(rss_endpoint)
self.assertEqual(response.status_code, 200)
3 changes: 2 additions & 1 deletion tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
url(r"^api/v1/", include(api_v1_router.urls)), # noqa
url(r"^content_list_one\.html", ContentListView.as_view(template_name="testapp/content_list.html")),
url(r"^videos/", include("bulbs.videos.urls")),
url(r"^r/", include("bulbs.redirects.urls"))
url(r"^r/", include("bulbs.redirects.urls")),
url(r"^feeds", include("bulbs.feeds.urls"))
)

0 comments on commit 520da22

Please sign in to comment.