Browse Source

Cache images' URL.

pull/402/head
Jairo Llopis 9 years ago
parent
commit
396536b673
  1. 12
      base_multi_image/models/image.py

12
base_multi_image/models/image.py

@ -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

Loading…
Cancel
Save