Skip to content

Commit

Permalink
Review item_enclosure properties, now considering that Entry.image is…
Browse files Browse the repository at this point in the history
… not obviously an ImageField, close #499
  • Loading branch information
Fantomas42 committed Feb 28, 2017
1 parent 41601d0 commit 04adc9a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions zinnia/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ def item_enclosure_url(self, item):
"""
Return an image for enclosure.
"""
if item.image:
try:
url = item.image.url
else:
except (AttributeError, ValueError):
img = BeautifulSoup(item.html_content, 'html.parser').find('img')
url = img.get('src') if img else None
self.cached_enclosure_url = url
Expand All @@ -143,24 +143,26 @@ def item_enclosure_url(self, item):

def item_enclosure_length(self, item):
"""
Try to obtain the size of the enclosure
if the enclosure is present on the FS,
Try to obtain the size of the enclosure if it's present on the FS,
otherwise returns an hardcoded value.
Note: this method is only called if item_enclosure_url
has returned something.
"""
if item.image:
try:
return str(item.image.size)
except (os.error, NotImplementedError):
pass
try:
return str(item.image.size)
except (AttributeError, ValueError, os.error):
pass
return '100000'

def item_enclosure_mime_type(self, item):
"""
Guess the enclosure's mimetype.
Note: this method is only called if item_enclosure_url
has returned something.
"""
mimetype, encoding = guess_type(self.cached_enclosure_url)
if mimetype:
return mimetype
mime_type, encoding = guess_type(self.cached_enclosure_url)
if mime_type:
return mime_type
return 'image/jpeg'


Expand Down

0 comments on commit 04adc9a

Please sign in to comment.