opt app_common 及app_saas 相关,但没检查

This commit is contained in:
Ivan Office
2025-01-26 00:42:35 +08:00
parent 0c93e27528
commit ec4186a53d
13 changed files with 421 additions and 286 deletions

View File

@@ -1,18 +1,19 @@
# -*- coding: utf-8 -*-
from odoo import models, fields, api, _
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
from odoo.http import request
import requests
import base64
from io import BytesIO
import uuid
from PIL import Image
from datetime import date, datetime, time
import pytz
import logging
from odoo import models, fields, api, _
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
from odoo.http import request
_logger = logging.getLogger(__name__)
# 常规的排除的fields
@@ -52,7 +53,7 @@ EXCLU_FIELDS = [
class Base(models.AbstractModel):
_inherit = 'base'
@api.model
def _app_check_sys_op(self):
if self.env.user.has_group('base.group_erp_manager'):
@@ -75,7 +76,10 @@ class Base(models.AbstractModel):
else:
if not domain:
domain = self._fields[fieldname].domain or []
rec = self.env[self._fields[fieldname].comodel_name].sudo().search(domain, limit=1)
try:
rec = self.env[self._fields[fieldname].comodel_name].search(domain, limit=1)
except Exception as e:
rec = self.env[self._fields[fieldname].comodel_name].search([], limit=1)
return rec.id if rec else False
return False
@@ -127,6 +131,7 @@ class Base(models.AbstractModel):
'website_id': False,
'res_model': self._name,
'res_id': self.id,
'public': True,
})
attachment.generate_access_token()
return attachment
@@ -149,6 +154,7 @@ class Base(models.AbstractModel):
'website_id': False,
'res_model': self._name,
'res_id': self.id,
'public': True,
})
attachment.generate_access_token()
return attachment
@@ -157,11 +163,33 @@ class Base(models.AbstractModel):
return False
else:
return False
@api.model
def _get_video_url2attachment(self, url):
if not self._app_check_sys_op():
return False
video, file_name = get_video_url2attachment(url)
if video and file_name:
try:
attachment = self.env['ir.attachment'].create({
'datas': video,
'name': file_name,
'website_id': False,
'res_model': self._name,
'res_id': self.id,
'public': True,
})
attachment.generate_access_token()
return attachment
except Exception as e:
_logger.error('get_video_url2attachment error: %s' % str(e))
return False
else:
return False
def get_ua_type(self):
return get_ua_type()
def get_image_from_url(url):
if not url:
return None
@@ -179,7 +207,7 @@ def get_image_url2attachment(url):
try:
if url.startswith('//'):
url = 'https:%s' % url
response = requests.get(url, timeout=30)
response = requests.get(url, timeout=90)
except Exception as e:
return None, None
# 返回这个图片的base64编码
@@ -193,11 +221,31 @@ def get_image_base642attachment(data):
return None
try:
image_data = data.split(',')[1]
file_name = str(uuid.uuid4()) + '.png'
return image_data, file_name
img = Image.open(BytesIO(base64.b64decode(image_data)))
img = img.convert('RGB')
output = BytesIO()
img.save(output, format='JPEG')
file_name = str(uuid.uuid4()) + '.jpeg'
jpeg_data = output.getvalue()
jpeg_base64 = base64.b64encode(jpeg_data)
return jpeg_base64, file_name
except Exception as e:
return None, None
def get_video_url2attachment(url):
if not url:
return None
try:
if url.startswith('//'):
url = 'https:%s' % url
response = requests.get(url, timeout=90)
video_content = response.content
except Exception as e:
return None, None
# return this video in base64
base64_video = base64.b64encode(video_content)
file_name = url.split('/')[-1]
return base64_video, file_name
def get_ua_type():
ua = request.httprequest.headers.get('User-Agent')
@@ -232,11 +280,15 @@ def get_ua_type():
and ('miniProgram' in ua or 'MiniProgram' in ua or 'MiniProgramEnv' in ua or 'wechatdevtools' in ua):
# 微信小程序及开发者工具
utype = 'wxapp'
elif 'wxwork' in ua:
utype = 'qwapp'
elif 'MicroMessenger' in ua:
# 微信浏览器
utype = 'wxweb'
elif 'cn.erpapp.o20sticks.App' in ua:
# 安卓app
utype = 'native_android'
elif 'BytedanceWebview' in ua:
utype = 'dyweb'
# _logger.warning('=========get ua %s,%s' % (utype, ua))
return utype