From 7a66a85d56fe12bc6fff6694f87d5bd9fadb74d1 Mon Sep 17 00:00:00 2001 From: Chill Date: Tue, 31 Oct 2023 12:41:59 +0800 Subject: [PATCH] add base64 img to attachment --- app_common/__manifest__.py | 2 +- app_common/models/base.py | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app_common/__manifest__.py b/app_common/__manifest__.py index fadc9f36..a0f4915d 100644 --- a/app_common/__manifest__.py +++ b/app_common/__manifest__.py @@ -39,7 +39,7 @@ { 'name': "odooAi Common Util and Tools", - 'version': '16.23.10.27', + 'version': '16.23.10.30', '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 59e88455..52774d44 100644 --- a/app_common/models/base.py +++ b/app_common/models/base.py @@ -6,6 +6,7 @@ from odoo.http import request import requests import base64 from io import BytesIO +import uuid from datetime import date, datetime, time import pytz @@ -117,6 +118,18 @@ class Base(models.AbstractModel): else: return False + @api.model + def get_image_base642attachment(self, data): + image, file_name = get_image_base642attachment(data) + if image and file_name: + attachment = self.env['ir.attachment'].create({ + 'datas': image, + 'name': file_name, + }) + return attachment + else: + return False + def get_ua_type(self): return get_ua_type() @@ -144,7 +157,18 @@ def get_image_url2attachment(url): file_name = url.split('/')[-1] return image, file_name - + +def get_image_base642attachment(data): + if not data: + return None + try: + image_data = data.split(',')[1] + file_name = str(uuid.uuid4()) + '.png' + return image_data, file_name + except Exception as e: + return None, None + + def get_ua_type(): ua = request.httprequest.headers.get('User-Agent') # 临时用 agent 处理,后续要前端中正确处理或者都从后台来