From a0903b3dbd2676a6be760be318f98f770668ed79 Mon Sep 17 00:00:00 2001 From: Chill Date: Tue, 10 Oct 2023 18:39:28 +0800 Subject: [PATCH] =?UTF-8?q?prepare=20#I84HUG=20app=5Fai=5Fmedia=E7=BD=91?= =?UTF-8?q?=E7=AB=99=E5=9B=BE=E7=89=87=E7=AD=89=E5=A4=9A=E5=AA=92=E4=BD=93?= =?UTF-8?q?=E8=BD=AC=E6=9C=AC=E5=9C=B0--blog=5Fpost?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_common/__manifest__.py | 2 +- app_common/models/base.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app_common/__manifest__.py b/app_common/__manifest__.py index 5339258d..ae90a3c3 100644 --- a/app_common/__manifest__.py +++ b/app_common/__manifest__.py @@ -39,7 +39,7 @@ { 'name': "odooai Odooapp Common Func", - 'version': '16.23.09.27', + 'version': '16.23.10.10', 'author': 'odooai.cn', 'category': 'Base', 'website': 'https://www.odooai.cn', diff --git a/app_common/models/base.py b/app_common/models/base.py index 70472217..e36a1412 100644 --- a/app_common/models/base.py +++ b/app_common/models/base.py @@ -104,6 +104,17 @@ class Base(models.AbstractModel): # 返回这个图片的base64编码 return get_image_from_url(url) + @api.model + def get_image_url2attachment(self, url, mimetype_list=None): + # Todo: mimetype filter + image, file_name = get_image_url2attachment(url) + if image and file_name: + attachment = self.env['ir.attachment'].create({ + 'datas': image, + 'name': file_name, + }) + return attachment + def get_ua_type(self): return get_ua_type() @@ -116,6 +127,19 @@ def get_image_from_url(url): return None # 返回这个图片的base64编码 return base64.b64encode(BytesIO(response.content).read()) + +def get_image_url2attachment(url): + if not url: + return None + try: + response = requests.get(url, timeout=5) + except Exception as e: + return None, None + # 返回这个图片的base64编码 + image = base64.b64encode(BytesIO(response.content).read()) + file_name = url.split('/')[-1] + return image, file_name + def get_ua_type(): ua = request.httprequest.headers.get('User-Agent')