-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Commit
The ``item_enclosures`` hook returns a list of ``Enclosure`` objects which is then used by the feed builder. If the feed is a RSS feed, an exception is raised as RSS feeds don't allow multiple enclosures per feed item. The ``item_enclosures`` hook defaults to an empty list or, if the ``item_enclosure_url`` hook is defined, to a list with a single ``Enclosure`` built from the ``item_enclosure_url``, ``item_enclosure_length``, and ``item_enclosure_mime_type`` hooks.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,18 +118,30 @@ def __init__(self, title, link, description, language=None, author_email=None, | |
def add_item(self, title, link, description, author_email=None, | ||
author_name=None, author_link=None, pubdate=None, comments=None, | ||
unique_id=None, unique_id_is_permalink=None, enclosure=None, | ||
categories=(), item_copyright=None, ttl=None, updateddate=None, **kwargs): | ||
categories=(), item_copyright=None, ttl=None, updateddate=None, | ||
enclosures=None, **kwargs): | ||
""" | ||
Adds an item to the feed. All args are expected to be Python Unicode | ||
objects except pubdate and updateddate, which are datetime.datetime | ||
objects, and enclosure, which is an instance of the Enclosure class. | ||
objects, and enclosures, which is an iterable of instances of the | ||
Enclosure class. | ||
""" | ||
to_unicode = lambda s: force_text(s, strings_only=True) | ||
if categories: | ||
categories = [to_unicode(c) for c in categories] | ||
if ttl is not None: | ||
# Force ints to unicode | ||
ttl = force_text(ttl) | ||
if enclosure is None: | ||
enclosures = [] if enclosures is None else enclosures | ||
else: | ||
warnings.warn( | ||
"The enclosure keyword argument is deprecated, " | ||
"use enclosures instead.", | ||
RemovedInDjango20Warning, | ||
stacklevel=2, | ||
) | ||
enclosures = [enclosure] | ||
item = { | ||
'title': to_unicode(title), | ||
'link': iri_to_uri(link), | ||
|
@@ -142,7 +154,7 @@ def add_item(self, title, link, description, author_email=None, | |
'comments': to_unicode(comments), | ||
'unique_id': to_unicode(unique_id), | ||
'unique_id_is_permalink': unique_id_is_permalink, | ||
'enclosure': enclosure, | ||
'enclosures': enclosures, | ||
'categories': categories or (), | ||
'item_copyright': to_unicode(item_copyright), | ||
'ttl': ttl, | ||
|
@@ -317,18 +329,27 @@ def add_item_elements(self, handler, item): | |
handler.addQuickElement("ttl", item['ttl']) | ||
|
||
# Enclosure. | ||
if item['enclosure'] is not None: | ||
handler.addQuickElement("enclosure", '', | ||
{"url": item['enclosure'].url, "length": item['enclosure'].length, | ||
"type": item['enclosure'].mime_type}) | ||
if item['enclosures']: | ||
enclosures = list(item['enclosures']) | ||
if len(enclosures) > 1: | ||
raise ValueError( | ||
"RSS feed items may only have one enclosure, see " | ||
"http://www.rssboard.org/rss-profile#element-channel-item-enclosure" | ||
) | ||
enclosure = enclosures[0] | ||
handler.addQuickElement('enclosure', '', { | ||
'url': enclosure.url, | ||
'length': enclosure.length, | ||
'type': enclosure.mime_type, | ||
}) | ||
|
||
# Categories. | ||
for cat in item['categories']: | ||
handler.addQuickElement("category", cat) | ||
|
||
|
||
class Atom1Feed(SyndicationFeed): | ||
# Spec: http://atompub.org/2005/07/11/draft-ietf-atompub-format-10.html | ||
# Spec: https://tools.ietf.org/html/rfc4287 | ||
content_type = 'application/atom+xml; charset=utf-8' | ||
ns = "http://www.w3.org/2005/Atom" | ||
|
||
|
@@ -405,13 +426,14 @@ def add_item_elements(self, handler, item): | |
if item['description'] is not None: | ||
handler.addQuickElement("summary", item['description'], {"type": "html"}) | ||
|
||
# Enclosure. | ||
if item['enclosure'] is not None: | ||
handler.addQuickElement("link", '', | ||
{"rel": "enclosure", | ||
"href": item['enclosure'].url, | ||
"length": item['enclosure'].length, | ||
"type": item['enclosure'].mime_type}) | ||
# Enclosures. | ||
for enclosure in item.get('enclosures') or []: | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
umazalakain
|
||
handler.addQuickElement('link', '', { | ||
'rel': 'enclosure', | ||
'href': enclosure.url, | ||
'length': enclosure.length, | ||
'type': enclosure.mime_type, | ||
}) | ||
|
||
# Categories. | ||
for cat in item['categories']: | ||
|
or
item.get('enclosures', [])