From 396536b673eaf0e048e46c728a47495a25272f2d Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Thu, 3 Mar 2016 16:25:00 +0100 Subject: [PATCH] Cache images' URL. --- base_multi_image/models/image.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/base_multi_image/models/image.py b/base_multi_image/models/image.py index f402ad38f..187cbbf90 100644 --- a/base_multi_image/models/image.py +++ b/base_multi_image/models/image.py @@ -104,13 +104,19 @@ class Image(models.Model): @api.multi 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: - (filename, header) = urllib.urlretrieve(self.url) + (filename, header) = urllib.urlretrieve(url) with open(filename, 'rb') as f: return base64.b64encode(f.read()) except: - _logger.error("URL %s cannot be fetched", self.url, + _logger.error("URL %s cannot be fetched", url, exc_info=True) return False