Cache images' URL.

pull/3010/head
Jairo Llopis 2016-03-03 16:25:00 +01:00 committed by Ignacio J. Ortega
parent 06a49cc9e9
commit c7c5682b08
1 changed files with 9 additions and 3 deletions

View File

@ -104,13 +104,19 @@ class Image(models.Model):
@api.multi @api.multi
def _get_image_from_url(self): def _get_image_from_url(self):
if self.url: return self._get_image_from_url_cached(self.url)
@api.model
@tools.ormcache(skiparg=1)
def _get_image_from_url_cached(self, url):
"""Allow to download an image and cache it by its URL."""
if url:
try: try:
(filename, header) = urllib.urlretrieve(self.url) (filename, header) = urllib.urlretrieve(url)
with open(filename, 'rb') as f: with open(filename, 'rb') as f:
return base64.b64encode(f.read()) return base64.b64encode(f.read())
except: except:
_logger.error("URL %s cannot be fetched", self.url, _logger.error("URL %s cannot be fetched", url,
exc_info=True) exc_info=True)
return False return False