Skip to content

Commit

Permalink
完成 邀请用户消息和重置密码消息的重构,待处理模板卡片消息的icon_url
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbow-studio-solution committed Jan 31, 2022
1 parent a68bee6 commit fa877b9
Show file tree
Hide file tree
Showing 7 changed files with 461 additions and 73 deletions.
2 changes: 1 addition & 1 deletion wecom_base/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ResConfigSettings(models.TransientModel):
related="company_id.is_wecom_organization", readonly=False
)
company_name = fields.Char(
related="company_id.display_name", string="Company Name", readonly=False
related="company_id.name", string="Company Name", readonly=False
)
abbreviated_name = fields.Char(
related="company_id.abbreviated_name", readonly=False
Expand Down
72 changes: 9 additions & 63 deletions wecom_message/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import base64
import io
import functools

import werkzeug.utils
import odoo
from odoo import api, http, models, fields, SUPERUSER_ID, _
from odoo.modules import get_module_path, get_resource_path
Expand Down Expand Up @@ -39,67 +39,13 @@ class WxworkBinary(Binary):
auth="none",
cors="*",
)
def company_wecom_message_web_logo(self, dbname=None, **kw):
imgname = "wecom_message_logo"
imgext = ".png"
placeholder = functools.partial(
get_resource_path, "web", "static", "src", "img"
)
uid = None
if request.session.db:
dbname = request.session.db
uid = request.session.uid
elif dbname is None:
dbname = db_monodb()

if not uid:
uid = odoo.SUPERUSER_ID
def company_wecom_message_web_logo(self, **kw):

if not dbname:
response = http.send_file(placeholder(imgname + imgext))
else:
try:
# create an empty registry
registry = odoo.modules.registry.Registry(dbname)
with registry.cursor() as cr:
company = int(kw["company"]) if kw and kw.get("company") else False

if company:
cr.execute(
"""SELECT wecom_message_logo_web, write_date
FROM res_company
WHERE id = %s
""",
(company,),
)
else:
cr.execute(
"""SELECT c.wecom_message_logo_web, c.write_date
FROM res_users u
LEFT JOIN res_company c
ON c.id = u.company_id
WHERE u.id = %s
""",
(uid,),
)
row = cr.fetchone()

if row and row[0]:
image_base64 = base64.b64decode(row[0])
image_data = io.BytesIO(image_base64)
mimetype = guess_mimetype(image_base64, default="image/png")
imgext = "." + mimetype.split("/")[1]
if imgext == ".svg+xml":
imgext = ".svg"
response = http.send_file(
image_data,
filename=imgname + imgext,
mimetype=mimetype,
mtime=row[1],
)
else:
response = http.send_file(placeholder("nologo.png"))
except Exception:
response = http.send_file(placeholder(imgname + imgext))
response = (
request.env["res.company"]
.sudo()
.browse(int(kw["company"]))
.message_app_id.square_logo_url
)
return werkzeug.utils.redirect(response)

return response
105 changes: 105 additions & 0 deletions wecom_message/controllers/main_bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-

import base64
import io
import functools

import odoo
from odoo import api, http, models, fields, SUPERUSER_ID, _
from odoo.modules import get_module_path, get_resource_path
from odoo.http import request
from odoo.addons.web.controllers.main import db_monodb, Binary
from odoo.tools.mimetypes import guess_mimetype


class WxworkBinary(Binary):
"""[summary]
获取方块Logo
Args:
Binary ([type]): [description]
"""

# @http.route(
# [
# "/web/binary/wecom_message_logo",
# "/wecom_message_logo",
# "/wecom_message_logo.png",
# ],
# type="http",
# auth="none",
# cors="*",
# )
@http.route(
[
"/web/binary/wecom_message_logo",
"/wecom_message_logo",
"/wecom_message_logo.png",
],
type="http",
auth="none",
cors="*",
)
def company_wecom_message_web_logo(self, dbname=None, **kw):
imgname = "wecom_message_logo"
imgext = ".png"
placeholder = functools.partial(
get_resource_path, "web", "static", "src", "img"
)
uid = None
if request.session.db:
dbname = request.session.db
uid = request.session.uid
elif dbname is None:
dbname = db_monodb()

if not uid:
uid = odoo.SUPERUSER_ID

if not dbname:
response = http.send_file(placeholder(imgname + imgext))
else:
try:
# create an empty registry
registry = odoo.modules.registry.Registry(dbname)
with registry.cursor() as cr:
company = int(kw["company"]) if kw and kw.get("company") else False

if company:
cr.execute(
"""SELECT wecom_message_logo_web, write_date
FROM res_company
WHERE id = %s
""",
(company,),
)
else:
cr.execute(
"""SELECT c.wecom_message_logo_web, c.write_date
FROM res_users u
LEFT JOIN res_company c
ON c.id = u.company_id
WHERE u.id = %s
""",
(uid,),
)
row = cr.fetchone()

if row and row[0]:
image_base64 = base64.b64decode(row[0])
image_data = io.BytesIO(image_base64)
mimetype = guess_mimetype(image_base64, default="image/png")
imgext = "." + mimetype.split("/")[1]
if imgext == ".svg+xml":
imgext = ".svg"
response = http.send_file(
image_data,
filename=imgname + imgext,
mimetype=mimetype,
mtime=row[1],
)
else:
response = http.send_file(placeholder("nologo.png"))
except Exception:
response = http.send_file(placeholder(imgname + imgext))

return response
66 changes: 65 additions & 1 deletion wecom_message/data/auth_signup_data.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="0">

<!-- 1. 重置密码的消息模板-->
<record id="auth_signup.reset_password_email" model="mail.template">
<field name="msgtype">template_card</field>
Expand Down Expand Up @@ -87,9 +88,72 @@ Hello! First of all, please allow me to welcome you on behalf of our company. Th
</record>

<!-- 3. 发送帐户创建确认电子邮件-->
<record id="auth_signup.mail_template_user_signup_account_created" model="mail.template">
<field name="msgtype">template_card</field>
<field name="media_id" ref="wecom_material.wecom_material_image_account_created"/>
<field name="message_to_user">${object.wecom_userid | safe}</field>
<field name="body_json">{
"card_type": "news_notice",
"source": {
"icon_url": "${object.get_base_url()}/wecom_message_logo.png?company=${object.company_id.id}",
"desc": "${object.company_id.name}"
},
"main_title": {
"title": "Dear ${object.name},",
"desc": "Your account has been successfully created!"
},
"card_image": {
"url": "${object.get_base_url()}/wecom_material/static/src/img/account_created.png",
"aspect_ratio": 2.25
},
"vertical_content_list": [
{
"title": "Your account has been successfully created!",
"desc": "To gain access to your account, you can use the following link."
}
],
"horizontal_content_list": [
{
"keyname": "Your account",
"value": "${object.login}"
},
{
"type": 1,
"keyname": "Go to My Account",
"value": "Go to My Account",
"url": "${object.get_base_url()}/web/login?auth_login=${object.login}"
}
],
"jump_list": [
{
"type": 1,
"title": "Go to My Account",
"url": "${object.get_base_url()}/web/login?auth_login=${object.login}"
}
],
"card_action": {
"type": 1,
"url": "${object.get_base_url()}/web/login?auth_login=${object.login}",
"appid": "",
"pagepath": ""
}
} </field>
<field name="safe">1</field>
<field name="enable_id_trans" eval="False"/>
<field name="enable_duplicate_check" eval="False"/>
<field name="duplicate_check_interval">1800</field>
</record>

<!-- 4. 用于提醒未注册用户的消息模板-->

<record id="auth_signup.mail_template_data_unregistered_users" model="mail.template">
<field name="msgtype">mpnews</field>
<field name="media_id" ref="wecom_material.wecom_material_image_account_created"/>
<field name="message_to_user">${object.wecom_userid | safe}</field>
<field name="safe">1</field>
<field name="enable_id_trans" eval="False"/>
<field name="enable_duplicate_check" eval="False"/>
<field name="duplicate_check_interval">1800</field>
</record>

</data>

Expand Down
Loading

0 comments on commit fa877b9

Please sign in to comment.