2012-10-01 02:44:27 +08:00
|
|
|
from calendar import timegm
|
|
|
|
|
2014-01-26 04:54:25 +08:00
|
|
|
from django.contrib.sites.shortcuts import get_current_site
|
2010-01-28 21:46:18 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.http import Http404, HttpResponse
|
|
|
|
from django.template import TemplateDoesNotExist, loader
|
2017-01-07 19:11:46 +08:00
|
|
|
from django.utils import feedgenerator
|
2017-04-22 01:52:26 +08:00
|
|
|
from django.utils.encoding import iri_to_uri
|
2010-01-28 21:46:18 +08:00
|
|
|
from django.utils.html import escape
|
2012-10-01 02:44:27 +08:00
|
|
|
from django.utils.http import http_date
|
2013-09-08 16:43:33 +08:00
|
|
|
from django.utils.timezone import get_default_timezone, is_naive, make_aware
|
2019-04-11 05:48:58 +08:00
|
|
|
from django.utils.translation import get_language
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2012-07-07 17:34:04 +08:00
|
|
|
|
2010-10-08 22:14:05 +08:00
|
|
|
def add_domain(domain, url, secure=False):
|
2011-11-19 06:54:24 +08:00
|
|
|
protocol = 'https' if secure else 'http'
|
|
|
|
if url.startswith('//'):
|
|
|
|
# Support network-path reference (see #16753) - RSS requires a protocol
|
|
|
|
url = '%s:%s' % (protocol, url)
|
2015-05-08 15:32:21 +08:00
|
|
|
elif not url.startswith(('http://', 'https://', 'mailto:')):
|
2012-06-08 00:08:47 +08:00
|
|
|
url = iri_to_uri('%s://%s%s' % (protocol, domain, url))
|
2010-01-28 21:46:18 +08:00
|
|
|
return url
|
|
|
|
|
2012-10-01 02:44:27 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class FeedDoesNotExist(ObjectDoesNotExist):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Feed:
|
2010-01-28 21:46:18 +08:00
|
|
|
feed_type = feedgenerator.DefaultFeed
|
|
|
|
title_template = None
|
|
|
|
description_template = None
|
2019-04-11 05:48:58 +08:00
|
|
|
language = None
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
def __call__(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
obj = self.get_object(request, *args, **kwargs)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise Http404('Feed object does not exist.')
|
|
|
|
feedgen = self.get_feed(obj, request)
|
2015-06-04 22:00:39 +08:00
|
|
|
response = HttpResponse(content_type=feedgen.content_type)
|
2013-07-18 03:20:20 +08:00
|
|
|
if hasattr(self, 'item_pubdate') or hasattr(self, 'item_updateddate'):
|
|
|
|
# if item_pubdate or item_updateddate is defined for the feed, set
|
|
|
|
# header so as ConditionalGetMiddleware is able to send 304 NOT MODIFIED
|
2012-10-01 02:44:27 +08:00
|
|
|
response['Last-Modified'] = http_date(
|
|
|
|
timegm(feedgen.latest_post_date().utctimetuple()))
|
2010-01-28 21:46:18 +08:00
|
|
|
feedgen.write(response, 'utf-8')
|
|
|
|
return response
|
|
|
|
|
|
|
|
def item_title(self, item):
|
|
|
|
# Titles should be double escaped by default (see #6533)
|
2017-04-22 01:52:26 +08:00
|
|
|
return escape(str(item))
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
def item_description(self, item):
|
2017-04-22 01:52:26 +08:00
|
|
|
return str(item)
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
def item_link(self, item):
|
|
|
|
try:
|
|
|
|
return item.get_absolute_url()
|
|
|
|
except AttributeError:
|
2014-09-04 20:15:09 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Give your %s class a get_absolute_url() method, or define an '
|
|
|
|
'item_link() method in your Feed class.' % item.__class__.__name__
|
|
|
|
)
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2015-08-21 17:50:43 +08:00
|
|
|
def item_enclosures(self, item):
|
2015-11-24 23:34:03 +08:00
|
|
|
enc_url = self._get_dynamic_attr('item_enclosure_url', item)
|
2015-08-21 17:50:43 +08:00
|
|
|
if enc_url:
|
|
|
|
enc = feedgenerator.Enclosure(
|
2017-04-22 01:52:26 +08:00
|
|
|
url=str(enc_url),
|
|
|
|
length=str(self._get_dynamic_attr('item_enclosure_length', item)),
|
|
|
|
mime_type=str(self._get_dynamic_attr('item_enclosure_mime_type', item)),
|
2015-08-21 17:50:43 +08:00
|
|
|
)
|
|
|
|
return [enc]
|
|
|
|
return []
|
|
|
|
|
2015-11-24 23:34:03 +08:00
|
|
|
def _get_dynamic_attr(self, attname, obj, default=None):
|
2010-01-28 21:46:18 +08:00
|
|
|
try:
|
|
|
|
attr = getattr(self, attname)
|
|
|
|
except AttributeError:
|
|
|
|
return default
|
|
|
|
if callable(attr):
|
2013-03-17 17:19:30 +08:00
|
|
|
# Check co_argcount rather than try/excepting the function and
|
|
|
|
# catching the TypeError, because something inside the function
|
|
|
|
# may raise the TypeError. This technique is more accurate.
|
|
|
|
try:
|
2017-01-07 19:11:46 +08:00
|
|
|
code = attr.__code__
|
2013-03-17 17:19:30 +08:00
|
|
|
except AttributeError:
|
2017-01-07 19:11:46 +08:00
|
|
|
code = attr.__call__.__code__
|
2013-03-17 17:19:30 +08:00
|
|
|
if code.co_argcount == 2: # one argument is 'self'
|
2010-01-28 21:46:18 +08:00
|
|
|
return attr(obj)
|
|
|
|
else:
|
|
|
|
return attr()
|
|
|
|
return attr
|
|
|
|
|
|
|
|
def feed_extra_kwargs(self, obj):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return an extra keyword arguments dictionary that is used when
|
2010-01-28 21:46:18 +08:00
|
|
|
initializing the feed generator.
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def item_extra_kwargs(self, item):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return an extra keyword arguments dictionary that is used with
|
2010-01-28 21:46:18 +08:00
|
|
|
the `add_item` call of the feed generator.
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def get_object(self, request, *args, **kwargs):
|
|
|
|
return None
|
|
|
|
|
2013-02-24 22:00:34 +08:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return a dictionary to use as extra context if either
|
2013-02-24 22:00:34 +08:00
|
|
|
``self.description_template`` or ``self.item_template`` are used.
|
|
|
|
|
|
|
|
Default implementation preserves the old behavior
|
|
|
|
of using {'obj': item, 'site': current_site} as the context.
|
|
|
|
"""
|
|
|
|
return {'obj': kwargs.get('item'), 'site': kwargs.get('site')}
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def get_feed(self, obj, request):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return a feedgenerator.DefaultFeed object, fully populated, for
|
|
|
|
this feed. Raise FeedDoesNotExist for invalid parameters.
|
2010-01-28 21:46:18 +08:00
|
|
|
"""
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
current_site = get_current_site(request)
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2015-11-24 23:34:03 +08:00
|
|
|
link = self._get_dynamic_attr('link', obj)
|
2010-10-08 22:14:05 +08:00
|
|
|
link = add_domain(current_site.domain, link, request.is_secure())
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
feed = self.feed_type(
|
2015-11-24 23:34:03 +08:00
|
|
|
title=self._get_dynamic_attr('title', obj),
|
|
|
|
subtitle=self._get_dynamic_attr('subtitle', obj),
|
2013-11-03 17:22:11 +08:00
|
|
|
link=link,
|
2015-11-24 23:34:03 +08:00
|
|
|
description=self._get_dynamic_attr('description', obj),
|
2019-04-11 05:48:58 +08:00
|
|
|
language=self.language or get_language(),
|
2013-11-03 17:22:11 +08:00
|
|
|
feed_url=add_domain(
|
2010-10-08 22:14:05 +08:00
|
|
|
current_site.domain,
|
2015-11-24 23:34:03 +08:00
|
|
|
self._get_dynamic_attr('feed_url', obj) or request.path,
|
2010-10-08 22:14:05 +08:00
|
|
|
request.is_secure(),
|
|
|
|
),
|
2015-11-24 23:34:03 +08:00
|
|
|
author_name=self._get_dynamic_attr('author_name', obj),
|
|
|
|
author_link=self._get_dynamic_attr('author_link', obj),
|
|
|
|
author_email=self._get_dynamic_attr('author_email', obj),
|
|
|
|
categories=self._get_dynamic_attr('categories', obj),
|
|
|
|
feed_copyright=self._get_dynamic_attr('feed_copyright', obj),
|
|
|
|
feed_guid=self._get_dynamic_attr('feed_guid', obj),
|
|
|
|
ttl=self._get_dynamic_attr('ttl', obj),
|
2010-01-28 21:46:18 +08:00
|
|
|
**self.feed_extra_kwargs(obj)
|
|
|
|
)
|
|
|
|
|
|
|
|
title_tmp = None
|
|
|
|
if self.title_template is not None:
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2010-01-28 21:46:18 +08:00
|
|
|
title_tmp = loader.get_template(self.title_template)
|
2017-09-07 20:16:21 +08:00
|
|
|
except TemplateDoesNotExist:
|
|
|
|
pass
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
description_tmp = None
|
|
|
|
if self.description_template is not None:
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2010-01-28 21:46:18 +08:00
|
|
|
description_tmp = loader.get_template(self.description_template)
|
2017-09-07 20:16:21 +08:00
|
|
|
except TemplateDoesNotExist:
|
|
|
|
pass
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2015-11-24 23:34:03 +08:00
|
|
|
for item in self._get_dynamic_attr('items', obj):
|
2013-02-24 22:00:34 +08:00
|
|
|
context = self.get_context_data(item=item, site=current_site,
|
|
|
|
obj=obj, request=request)
|
2010-01-28 21:46:18 +08:00
|
|
|
if title_tmp is not None:
|
2015-01-08 22:03:43 +08:00
|
|
|
title = title_tmp.render(context, request)
|
2010-01-28 21:46:18 +08:00
|
|
|
else:
|
2015-11-24 23:34:03 +08:00
|
|
|
title = self._get_dynamic_attr('item_title', item)
|
2010-01-28 21:46:18 +08:00
|
|
|
if description_tmp is not None:
|
2015-01-08 22:03:43 +08:00
|
|
|
description = description_tmp.render(context, request)
|
2010-01-28 21:46:18 +08:00
|
|
|
else:
|
2015-11-24 23:34:03 +08:00
|
|
|
description = self._get_dynamic_attr('item_description', item)
|
2010-10-08 22:14:05 +08:00
|
|
|
link = add_domain(
|
|
|
|
current_site.domain,
|
2015-11-24 23:34:03 +08:00
|
|
|
self._get_dynamic_attr('item_link', item),
|
2010-10-08 22:14:05 +08:00
|
|
|
request.is_secure(),
|
|
|
|
)
|
2015-11-24 23:34:03 +08:00
|
|
|
enclosures = self._get_dynamic_attr('item_enclosures', item)
|
|
|
|
author_name = self._get_dynamic_attr('item_author_name', item)
|
2010-01-28 21:46:18 +08:00
|
|
|
if author_name is not None:
|
2015-11-24 23:34:03 +08:00
|
|
|
author_email = self._get_dynamic_attr('item_author_email', item)
|
|
|
|
author_link = self._get_dynamic_attr('item_author_link', item)
|
2010-01-28 21:46:18 +08:00
|
|
|
else:
|
|
|
|
author_email = author_link = None
|
|
|
|
|
2013-09-08 16:43:33 +08:00
|
|
|
tz = get_default_timezone()
|
|
|
|
|
2015-11-24 23:34:03 +08:00
|
|
|
pubdate = self._get_dynamic_attr('item_pubdate', item)
|
2011-11-18 21:01:06 +08:00
|
|
|
if pubdate and is_naive(pubdate):
|
2013-09-08 16:43:33 +08:00
|
|
|
pubdate = make_aware(pubdate, tz)
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2015-11-24 23:34:03 +08:00
|
|
|
updateddate = self._get_dynamic_attr('item_updateddate', item)
|
2013-07-18 03:20:20 +08:00
|
|
|
if updateddate and is_naive(updateddate):
|
2013-09-08 16:43:33 +08:00
|
|
|
updateddate = make_aware(updateddate, tz)
|
2013-07-18 03:20:20 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
feed.add_item(
|
2013-11-03 17:22:11 +08:00
|
|
|
title=title,
|
|
|
|
link=link,
|
|
|
|
description=description,
|
2015-11-24 23:34:03 +08:00
|
|
|
unique_id=self._get_dynamic_attr('item_guid', item, link),
|
|
|
|
unique_id_is_permalink=self._get_dynamic_attr(
|
2013-02-06 18:25:35 +08:00
|
|
|
'item_guid_is_permalink', item),
|
2015-08-21 17:50:43 +08:00
|
|
|
enclosures=enclosures,
|
2013-11-03 17:22:11 +08:00
|
|
|
pubdate=pubdate,
|
|
|
|
updateddate=updateddate,
|
|
|
|
author_name=author_name,
|
|
|
|
author_email=author_email,
|
|
|
|
author_link=author_link,
|
2020-07-23 22:42:25 +08:00
|
|
|
comments=self._get_dynamic_attr('item_comments', item),
|
2015-11-24 23:34:03 +08:00
|
|
|
categories=self._get_dynamic_attr('item_categories', item),
|
|
|
|
item_copyright=self._get_dynamic_attr('item_copyright', item),
|
2010-01-28 21:46:18 +08:00
|
|
|
**self.item_extra_kwargs(item)
|
|
|
|
)
|
|
|
|
return feed
|