add base64 img to attachment

This commit is contained in:
Chill
2023-10-31 12:41:59 +08:00
parent 74ac3038b0
commit 7a66a85d56
2 changed files with 26 additions and 2 deletions

View File

@@ -39,7 +39,7 @@
{ {
'name': "odooAi Common Util and Tools", 'name': "odooAi Common Util and Tools",
'version': '16.23.10.27', 'version': '16.23.10.30',
'author': 'odooai.cn', 'author': 'odooai.cn',
'category': 'Base', 'category': 'Base',
'website': 'https://www.odooai.cn', 'website': 'https://www.odooai.cn',

View File

@@ -6,6 +6,7 @@ from odoo.http import request
import requests import requests
import base64 import base64
from io import BytesIO from io import BytesIO
import uuid
from datetime import date, datetime, time from datetime import date, datetime, time
import pytz import pytz
@@ -117,6 +118,18 @@ class Base(models.AbstractModel):
else: else:
return False 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): def get_ua_type(self):
return get_ua_type() return get_ua_type()
@@ -145,6 +158,17 @@ def get_image_url2attachment(url):
return image, file_name 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(): def get_ua_type():
ua = request.httprequest.headers.get('User-Agent') ua = request.httprequest.headers.get('User-Agent')
# 临时用 agent 处理,后续要前端中正确处理或者都从后台来 # 临时用 agent 处理,后续要前端中正确处理或者都从后台来