import logging import _thread from django.dispatch import receiver from django.core.mail import send_mail from django.utils.html import strip_tags from django.template.loader import render_to_string from django.conf import settings from django.db.models.signals import post_save from sms import send_sms_message from pingcap_sso import signals as pingcap_signals from pingcap_sso.discourse import api as discourse_api from pingcap_sso.models import LarkMessage, SiteSetting, TUGMemberApplication from pingcap_sso.utils import time as ptime logger = logging.getLogger(__name__) @receiver(pingcap_signals.join_tug) def _tug_application_approved_notify(sender, user, application, **kwargs): _thread.start_new_thread( _async_tug_application_approved_notify, (user.username, application.email, application.phone) ) def _async_tug_application_approved_notify(username, email, phone): title = '审核通过,恭喜你正式成为 TUG 成员!' if not (settings.DEBUG or settings.STAGING): send_sms_message(phone, {}, 'SMS_223561380') html_message = render_to_string('pingcap_sso/email/approve_tug_application.html') send_mail( title, strip_tags(html_message), None, [email], html_message=html_message ) discourse_api.send_private_message( [username], title, """今天是值得铭记的一天,恭喜你正式成为 TUG 的一员,在接下来的日子,让我们一起去探索技术问题,随时随地交流成长,解决问题;发表技术见解,收获前沿知识。 请加小助手优优,诚邀你加入 TUG 实名交流群。 ![TUG 助手微信二维码](https://img2.pingcap.com/forms/6/e/6e789feeedb2b2e4af3004239a5b9a0dcc4575e1.png) 这里有一份关于 [TUG ](https://asktug.com/t/topic/153121) 的详细介绍,请查收一下。""" ) @receiver(pingcap_signals.reject_tug_applications) def _tug_application_reject_notify(sender, application, **kwargs): user = application.user _thread.start_new_thread( _async_tug_application_reject_notify, (user.username, application.email, application.phone, application.review_comment) ) def _async_tug_application_reject_notify(username, email, phone, review_comment): title = 'TUG 申请审核不通过' if not (settings.DEBUG or settings.STAGING): send_sms_message(phone, {}, 'SMS_223561381') html_message = render_to_string( 'pingcap_sso/email/reject_tug_application.html', { 'review_comment': review_comment } ) send_mail( title, strip_tags(html_message), None, [email], html_message=html_message ) discourse_api.send_private_message( [username], title, """{review_comment} 什么样的人可以可以加入 TUG? - 技术人才(开发、研发、运维、架构师、DBA 等职业) - 在用、有意向使用 TiDB 或者对 TiDB 感兴趣 - 乐于分享 如果你满足条件,请重新提交资料申请。如有问题可以私信 billmay。""".format(review_comment=review_comment) ) @receiver(post_save, sender=TUGMemberApplication) def _notify_new_member_application(sender, instance, created, **kwargs): if not created: return notify_lark_webhook = SiteSetting.tug_new_application_notify_lark_webhook if not notify_lark_webhook: return application = instance message = f"""**TUG 新成员申请** - 用户: [{ application.user.username }](https://accounts.pingcap.cn/manage#/users/{ application.user.username }/info) - 真实姓名: { application.real_name } - 手机号: { application.phone } - 邮箱: { application.email } - 公司: { application.company_name } - 职位: { application.job_title_text } - 公司使用 TiDB 处在什么阶段: { application.stage_of_company_use_of_tidb } - 乐于分享的方式: { ','.join(application.preferred_way_of_sharing) } - 希望担任的角色: { ','.join(application.roles_want_to_play) } - 了解的渠道: { application.channel } - 入群自我介绍: { application.bio } - 微信 ID: { application.wechat_id } - 推荐人: { application.referrer } - 申请时间: { ptime.local_time_format(application.created_at) } *[审核链接](https://accounts.pingcap.cn/manage#/tug/applications/{ application.id })* """ LarkMessage.send( webhook=notify_lark_webhook, message_type=LarkMessage.MESSAGE_TYPE_MARKDOWN, content=message, remark='TUG 新成员申请通知' )