Enable Multiple media:thumbnail Elements
As with media:content, there can be multiple media:thumbnail elements in an item and in multiple groups. This patch adds the ability to do this in the same manner as with the content. Part of #58
This commit is contained in:
parent
4970dab6d7
commit
4a0a2663e9
2 changed files with 83 additions and 26 deletions
|
@ -31,7 +31,7 @@ class MediaEntryExtension(BaseEntryExtension):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__media_content = []
|
self.__media_content = []
|
||||||
self.__media_thumbnail = None
|
self.__media_thumbnail = []
|
||||||
|
|
||||||
def extend_atom(self, entry):
|
def extend_atom(self, entry):
|
||||||
'''Add additional fields to an RSS item.
|
'''Add additional fields to an RSS item.
|
||||||
|
@ -54,16 +54,17 @@ class MediaEntryExtension(BaseEntryExtension):
|
||||||
if media_content.get(attr):
|
if media_content.get(attr):
|
||||||
content.set(attr, media_content[attr])
|
content.set(attr, media_content[attr])
|
||||||
|
|
||||||
if self.__media_thumbnail:
|
for media_thumbnail in self.__media_thumbnail:
|
||||||
|
# Define current media:group
|
||||||
|
group = groups.get(media_thumbnail.get('group'))
|
||||||
|
if group is None:
|
||||||
|
group = etree.SubElement(entry, '{%s}group' % MEDIA_NS)
|
||||||
|
groups[media_thumbnail.get('group')] = group
|
||||||
|
# Add thumbnails
|
||||||
thumbnail = etree.SubElement(group, '{%s}thumbnail' % MEDIA_NS)
|
thumbnail = etree.SubElement(group, '{%s}thumbnail' % MEDIA_NS)
|
||||||
if self.__media_thumbnail.get('url'):
|
for attr in ('url', 'height', 'width', 'time'):
|
||||||
thumbnail.set('url', self.__media_thumbnail.get('url'))
|
if media_thumbnail.get(attr):
|
||||||
if self.__media_thumbnail.get('height'):
|
thumbnail.set(attr, media_thumbnail[attr])
|
||||||
thumbnail.set('height', self.__media_thumbnail.get('height'))
|
|
||||||
if self.__media_thumbnail.get('width'):
|
|
||||||
thumbnail.set('width', self.__media_thumbnail.get('width'))
|
|
||||||
if self.__media_thumbnail.get('lang'):
|
|
||||||
thumbnail.set('lang', self.__media_thumbnail.get('lang'))
|
|
||||||
|
|
||||||
return entry
|
return entry
|
||||||
|
|
||||||
|
@ -134,28 +135,50 @@ class MediaEntryExtension(BaseEntryExtension):
|
||||||
set(['url', 'group']))
|
set(['url', 'group']))
|
||||||
return self.__media_content
|
return self.__media_content
|
||||||
|
|
||||||
def thumbnail(self, url=None, height=None, width=None, time=None):
|
def thumbnail(self, thumbnail=None, replace=False, group='default',
|
||||||
'''Allows particular images to be used as representative images for
|
**kwargs):
|
||||||
|
'''Get or set media:thumbnail data.
|
||||||
|
|
||||||
|
This method can be called with:
|
||||||
|
- the fields of a media:content as keyword arguments
|
||||||
|
- the fields of a media:content as a dictionary
|
||||||
|
- a list of dictionaries containing the media:content fields
|
||||||
|
|
||||||
|
Allows particular images to be used as representative images for
|
||||||
the media object. If multiple thumbnails are included, and time
|
the media object. If multiple thumbnails are included, and time
|
||||||
coding is not at play, it is assumed that the images are in order
|
coding is not at play, it is assumed that the images are in order
|
||||||
of importance. It has one required attribute and three optional
|
of importance. It has one required attribute and three optional
|
||||||
attributes.
|
attributes.
|
||||||
|
|
||||||
:param url: should specify the direct URL to the media object.
|
media:thumbnail has the following fields:
|
||||||
:param height: height of the media object.
|
- *url* should specify the direct URL to the media object.
|
||||||
:param width: width of the media object.
|
- *height* height of the media object.
|
||||||
:param time: specifies the time offset in relation to the media object.
|
- *width* width of the media object.
|
||||||
|
- *time* specifies the time offset in relation to the media object.
|
||||||
|
|
||||||
|
:param thumbnail: Dictionary or list of dictionaries with thumbnail
|
||||||
|
data.
|
||||||
|
:param replace: Add or replace old data.
|
||||||
|
:param group: Media group to put this content in.
|
||||||
|
|
||||||
:returns: The media thumbnail tag.
|
:returns: The media thumbnail tag.
|
||||||
'''
|
'''
|
||||||
|
# Handle kwargs
|
||||||
if url is not None:
|
if thumbnail is None and kwargs:
|
||||||
self.__media_thumbnail = {'url': url}
|
thumbnail = kwargs
|
||||||
if height is not None:
|
# Handle new data
|
||||||
self.__media_thumbnail['height'] = height
|
if thumbnail is not None:
|
||||||
if width is not None:
|
# Reset data if we want to replace them
|
||||||
self.__media_thumbnail['width'] = width
|
if replace or self.__media_thumbnail is None:
|
||||||
if time is not None:
|
self.__media_thumbnail = []
|
||||||
self.__media_thumbnail['time'] = time
|
# Ensure list
|
||||||
|
if not isinstance(thumbnail, list):
|
||||||
|
thumbnail = [thumbnail]
|
||||||
|
# Define media group
|
||||||
|
for t in thumbnail:
|
||||||
|
t['group'] = t.get('group', group)
|
||||||
|
self.__media_thumbnail += ensure_format(
|
||||||
|
thumbnail,
|
||||||
|
set(['url', 'height', 'width', 'time', 'group']),
|
||||||
|
set(['url', 'group']))
|
||||||
return self.__media_thumbnail
|
return self.__media_thumbnail
|
||||||
|
|
|
@ -238,3 +238,37 @@ class TestExtensionMedia(unittest.TestCase):
|
||||||
|
|
||||||
fe.media.content(content=[], replace=True)
|
fe.media.content(content=[], replace=True)
|
||||||
assert fe.media.content() == []
|
assert fe.media.content() == []
|
||||||
|
|
||||||
|
def test_media_thumbnail(self):
|
||||||
|
fe = self.fg.add_item()
|
||||||
|
fe.id('id')
|
||||||
|
fe.title('title')
|
||||||
|
fe.content('content')
|
||||||
|
fe.media.thumbnail(url='file1.xy')
|
||||||
|
fe.media.thumbnail(url='file2.xy')
|
||||||
|
fe.media.thumbnail(url='file1.xy', group=2)
|
||||||
|
fe.media.thumbnail(url='file2.xy', group=2)
|
||||||
|
fe.media.thumbnail(url='file.xy', group=None)
|
||||||
|
|
||||||
|
ns = {'media': 'http://search.yahoo.com/mrss/',
|
||||||
|
'a': 'http://www.w3.org/2005/Atom'}
|
||||||
|
# Check that we have the item in the resulting RSS
|
||||||
|
root = etree.fromstring(self.fg.rss_str())
|
||||||
|
url = root.xpath(
|
||||||
|
'/rss/channel/item/media:group/media:thumbnail[1]/@url',
|
||||||
|
namespaces=ns)
|
||||||
|
assert url == ['file1.xy', 'file1.xy']
|
||||||
|
|
||||||
|
# There is one without a group
|
||||||
|
url = root.xpath('/rss/channel/item/media:thumbnail[1]/@url',
|
||||||
|
namespaces=ns)
|
||||||
|
assert url == ['file.xy']
|
||||||
|
|
||||||
|
# Check that we have the item in the resulting Atom feed
|
||||||
|
root = etree.fromstring(self.fg.atom_str())
|
||||||
|
url = root.xpath('/a:feed/a:entry/media:group/media:thumbnail[1]/@url',
|
||||||
|
namespaces=ns)
|
||||||
|
assert url == ['file1.xy', 'file1.xy']
|
||||||
|
|
||||||
|
fe.media.thumbnail(thumbnail=[], replace=True)
|
||||||
|
assert fe.media.thumbnail() == []
|
||||||
|
|
Loading…
Reference in a new issue