Skip to content

Commit

Permalink
开始重构消息模块
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbow-studio-solution committed Jan 30, 2022
1 parent 2482139 commit a610d91
Show file tree
Hide file tree
Showing 27 changed files with 788 additions and 1,336 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- [路径](/docker)

> TODO :
- 重构企业微信消息功能
- 重构企业微信消息功能,在继承mail和不继承mail反复横跳,最终决定还是继承mail
- 使用ReadtheDocs上线文档

## 功能模块介绍
Expand Down
2 changes: 0 additions & 2 deletions wecom_api/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class ResConfigSettings(models.TransientModel):
module_wecom_material = fields.Boolean("WeCom media material library")
module_wecom_message = fields.Boolean("WeCom send message")
module_wecom_auth_oauth = fields.Boolean("WeCom Authentication")

# 专业版模块
module_wecom_msgaudit = fields.Boolean("Manage Session Content Archive")

# contacts_auto_sync_hr_enabled = fields.Boolean(
Expand Down
4 changes: 2 additions & 2 deletions wecom_api/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
</span>
<widget name='res_config_wecom_navigation_gotop'/>
</h2>
<div class="row mt16 o_settings_container" attrs="{'invisible': [('is_wecom_organization','=',False)]}">
<div class="row mt16 o_settings_container" attrs="{'invisible': [('module_wecom_material','=',False)]}">
<div class="col-12 col-lg-6 o_setting_box" id="wecom_auth_oauth_install">
<div class="o_setting_left_pane">
<field name="module_wecom_auth_oauth"/>
Expand Down Expand Up @@ -200,7 +200,7 @@
</span>
<widget name='res_config_wecom_navigation_gotop'/>
</h2>
<div class="row mt16 o_settings_container" attrs="{'invisible': [('is_wecom_organization','=',False)]}">
<div class="row mt16 o_settings_container" attrs="{'invisible': [('module_wecom_auth_oauth','=',False)]}">
<div class="col-12 col-lg-6 o_setting_box" id="wecom_message_install">
<div class="o_setting_left_pane">
<field name="module_wecom_message"/>
Expand Down
2 changes: 0 additions & 2 deletions wecom_auth_oauth/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
"data/wecom_apps_data.xml",
"data/wecom_app_config_data.xml",
"data/wecom_oauth_data.xml",
# "data/auth_signup_data.xml",
"views/assets_templates.xml",
"views/res_config_settings_views.xml",
"views/res_users_views.xml",
"views/wecom_apps_views.xml",
"views/menu_views.xml",
],
Expand Down
150 changes: 1 addition & 149 deletions wecom_auth_oauth/models/res_users.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
# -*- coding: utf-8 -*-
import logging
import json
import requests

from collections import defaultdict
from dateutil.relativedelta import relativedelta
from odoo import models, fields, api, _
from odoo.exceptions import UserError
from odoo.addons.base.models.ir_mail_server import MailDeliveryException
from odoo.addons.auth_signup.models.res_partner import SignupError, now

from odoo import models, api, _
from odoo.exceptions import AccessDenied


_logger = logging.getLogger(__name__)

from odoo.http import request


class ResUsers(models.Model):
_inherit = "res.users"

Expand Down Expand Up @@ -71,137 +57,3 @@ def _check_credentials(self, password, env):
if not res:
raise

# ---------------------
# 发送消息
# ---------------------
def action_reset_password(self):
"""
为每个用户创建注册令牌,并通过电子邮件发送他们的注册url
增加判断模板对象为企业微信用户,使用企业微信发送模板消息的方法
"""
if self.env.context.get("install_mode", False):
return
if self.filtered(lambda user: not user.active):
raise UserError(_("You cannot perform this action on an archived user."))
# prepare reset password signup
create_mode = bool(self.env.context.get("create_user"))

# no time limit for initial invitation, only for reset password
expiration = False if create_mode else now(days=+1)

self.mapped("partner_id").signup_prepare(
signup_type="reset", expiration=expiration
)

# send email to users with their signup url
template = False
if create_mode:
try:
template = self.env.ref(
"auth_signup.set_password_email", raise_if_not_found=False
)
except ValueError:
pass
if not template:
template = self.env.ref("auth_signup.reset_password_email")
assert template._name == "mail.template"
template_values = {
"email_to": "${object.email|safe}",
"email_cc": False,
"auto_delete": True,
"partner_to": False,
"scheduled_date": False,
}
template.write(template_values)
for user in self:
if user.wecom_userid:
return self.action_reset_password_by_wecom(user, create_mode)
elif not user.email:
raise UserError(
_("Cannot send email: user %s has no email address.", user.name)
)
# TDE FIXME: make this template technical (qweb)
with self.env.cr.savepoint():
force_send = not (self.env.context.get("import_file", False))
template.send_mail(user.id, force_send=force_send, raise_exception=True)
_logger.info(
"Password reset email sent for user <%s> to <%s>",
user.login,
user.email,
)
return super(ResUsers, self).action_reset_password()

def action_reset_password_by_wecom(self, user, create_mode):
"""
通过企业微信的方式发送模板消息
"""
create_mode = bool(self.env.context.get("create_user"))
if create_mode:
template = self.env.ref("wecom_auth_oauth.set_password_message")
else:
template = self.env.ref("wecom_auth_oauth.reset_password_message")
assert template._name == "wecom.message.template"
template_values = {
"message_to_user": "${object.wecom_userid|safe}",
"auto_delete": True,
"partner_to": False,
"scheduled_date": False,
}
template.write(template_values)
with self.env.cr.savepoint():
force_send = not (self.env.context.get("import_file", False))
template.send_message(
user.id, force_send=force_send, raise_exception=True,
)
_logger.info(
_("Password reset message sent to user: <%s>,<%s>, <%s>"),
user.name,
user.login,
user.wecom_userid,
)

def send_unregistered_user_reminder(self, after_days=5):
"""
发送未注册的用户提醒 消息
"""
datetime_min = fields.Datetime.today() - relativedelta(days=after_days)
datetime_max = datetime_min + relativedelta(hours=23, minutes=59, seconds=59)

res_users_with_details = self.env["res.users"].search_read(
[
("share", "=", False),
("create_uid.email", "!=", False),
("create_date", ">=", datetime_min),
("create_date", "<=", datetime_max),
("log_ids", "=", False),
],
["create_uid", "name", "login"],
)

# 分组邀请
invited_users = defaultdict(list)
for user in res_users_with_details:
invited_users[user.get("create_uid")[0]].append(
"%s (%s)" % (user.get("name"), user.get("login"))
)

# 用于向所有邀请者发送有关其邀请用户的邮件
for user in invited_users:
if user.wecom_userid:
return self.send_unregistered_user_reminder_by_wecom(
user, invited_users
)
template = self.env.ref(
"auth_signup.mail_template_data_unregistered_users"
).with_context(dbname=self._cr.dbname, invited_users=invited_users[user])
template.send_mail(
user, notif_layout="mail.mail_notification_light", force_send=False
)

def send_unregistered_user_reminder_by_wecom(self, user, invited_users):
message_template = self.env.ref(
"wecom_auth_oauth.message_template_data_unregistered_users"
).with_context(dbname=self._cr.dbname, invited_users=invited_users[user])
message_template.send_message(
user, notif_layout="mail.mail_notification_light", force_send=False
)
28 changes: 0 additions & 28 deletions wecom_auth_oauth/views/res_users_views.xml

This file was deleted.

6 changes: 6 additions & 0 deletions wecom_contacts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-


# from . import controllers
# from . import models
# from . import wizard
22 changes: 22 additions & 0 deletions wecom_contacts/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
{
"name": "WeCom Contacts",
"author": "RStudio",
"website": "https://gitee.com/rainbowstudio/wecom",
"sequence": 607,
"installable": True,
"application": True,
"auto_install": False,
"category": "WeCom/WeCom",
"version": "14.0.0.1",
"summary": """
WeCom Contacts
""",
"description": """
""",
"depends": ["wecom_message",],
"external_dependencies": {"python": [],},
"qweb": ["static/src/xml/*.xml",],
}
Binary file added wecom_contacts/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 7 additions & 6 deletions wecom_message/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"website": "https://gitee.com/rainbowstudio/wecom",
"sequence": 605,
"installable": True,
"application": False,
"application": True,
"auto_install": False,
"category": "WeCom/WeCom",
"version": "14.0.0.1",
Expand All @@ -20,22 +20,23 @@
"mail",
# "digest",
"rating",
"wecom_base",
"wecom_widget",
"wecom_material",
"wecom_auth_oauth",
],
"external_dependencies": {"python": ["html2text", "lxml"],},
"data": [
"security/ir.model.access.csv",
"data/wecom_apps_data.xml",
"data/message_template_data.xml",
"data/auth_signup_data.xml",
# "data/message_template_data.xml",
# "wizard/mail_template_preview_views.xml",
"views/assets_templates.xml",
# "views/mail_message_views.xml",
# "views/res_company_views.xml",
"views/res_users_views.xml",
"views/res_config_settings_views.xml",
"views/wecom_message_template_views.xml",
"views/wecom_message_message_views.xml",
"views/mail_template_views.xml",
# "views/wecom_message_message_views.xml",
# "views/wecom_apps_views.xml",
# "views/wecom_message_notification_views.xml",
"views/menu.xml",
Expand Down
Loading

0 comments on commit a610d91

Please sign in to comment.