Skip to content

Commit

Permalink
Fixed #29352 -- Allowed specifying a Feed language
Browse files Browse the repository at this point in the history
  • Loading branch information
rixx committed Apr 13, 2019
1 parent 571ab44 commit d34b507
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions django/contrib/syndication/views.py
@@ -1,6 +1,5 @@
from calendar import timegm

from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from django.http import Http404, HttpResponse
Expand All @@ -10,6 +9,7 @@
from django.utils.html import escape
from django.utils.http import http_date
from django.utils.timezone import get_default_timezone, is_naive, make_aware
from django.utils.translation import get_language


def add_domain(domain, url, secure=False):
Expand All @@ -30,6 +30,7 @@ class Feed:
feed_type = feedgenerator.DefaultFeed
title_template = None
description_template = None
language = None

def __call__(self, request, *args, **kwargs):
try:
Expand Down Expand Up @@ -134,7 +135,7 @@ def get_feed(self, obj, request):
subtitle=self._get_dynamic_attr('subtitle', obj),
link=link,
description=self._get_dynamic_attr('description', obj),
language=settings.LANGUAGE_CODE,
language=self.language or get_language(),
feed_url=add_domain(
current_site.domain,
self._get_dynamic_attr('feed_url', obj) or request.path,
Expand Down
5 changes: 5 additions & 0 deletions docs/ref/contrib/syndication.txt
Expand Up @@ -406,6 +406,11 @@ This example illustrates all possible attributes and methods for a
title_template = None
description_template = None

# LANGUAGE -- Optional. This should be a string specifying a
# language code.
# Defaults to :func:`~django.utils.translation.get_language()`
language = 'de'

# TITLE -- One of the following three is required. The framework
# looks for them in this order.

Expand Down
4 changes: 4 additions & 0 deletions tests/syndication_tests/feeds.py
Expand Up @@ -136,6 +136,10 @@ def get_context_data(self, **kwargs):
return context


class TestLanguageFeed(TestRss2Feed):
language = 'de'


class NaiveDatesFeed(TestAtomFeed):
"""
A feed with naive (non-timezone-aware) dates.
Expand Down
5 changes: 5 additions & 0 deletions tests/syndication_tests/tests.py
Expand Up @@ -363,6 +363,11 @@ def test_custom_feed_generator(self):
summary = entry.getElementsByTagName('summary')[0]
self.assertEqual(summary.getAttribute('type'), 'html')

def test_feed_generator_language_attribute(self):
response = self.client.get('/syndication/language/')
feed = minidom.parseString(response.content).firstChild
self.assertEqual(feed.firstChild.getElementsByTagName('language')[0].firstChild.nodeValue, 'de')

def test_title_escaping(self):
"""
Titles are escaped correctly in RSS feeds.
Expand Down
1 change: 1 addition & 0 deletions tests/syndication_tests/urls.py
Expand Up @@ -15,6 +15,7 @@
path('syndication/atom/', feeds.TestAtomFeed()),
path('syndication/latest/', feeds.TestLatestFeed()),
path('syndication/custom/', feeds.TestCustomFeed()),
path('syndication/language/', feeds.TestLanguageFeed()),
path('syndication/naive-dates/', feeds.NaiveDatesFeed()),
path('syndication/aware-dates/', feeds.TZAwareDatesFeed()),
path('syndication/feedurl/', feeds.TestFeedUrlFeed()),
Expand Down

0 comments on commit d34b507

Please sign in to comment.