Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

mail_owner_transfer_task.py 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task # type: ignore
  5. from flask import render_template
  6. from extensions.ext_mail import mail
  7. from services.feature_service import FeatureService
  8. @shared_task(queue="mail")
  9. def send_owner_transfer_confirm_task(language: str, to: str, code: str, workspace: str):
  10. """
  11. Async Send owner transfer confirm mail
  12. :param language: Language in which the email should be sent (e.g., 'en', 'zh')
  13. :param to: Recipient email address
  14. :param workspace: Workspace name
  15. """
  16. if not mail.is_inited():
  17. return
  18. logging.info(click.style("Start change email mail to {}".format(to), fg="green"))
  19. start_at = time.perf_counter()
  20. # send change email mail using different languages
  21. try:
  22. if language == "zh-Hans":
  23. template = "transfer_workspace_owner_confirm_template_zh-CN.html"
  24. system_features = FeatureService.get_system_features()
  25. if system_features.branding.enabled:
  26. template = "without-brand/transfer_workspace_owner_confirm_template_zh-CN.html"
  27. html_content = render_template(template, to=to, code=code, WorkspaceName=workspace)
  28. mail.send(to=to, subject="验证您转移工作空间所有权的请求", html=html_content)
  29. else:
  30. html_content = render_template(template, to=to, code=code, WorkspaceName=workspace)
  31. mail.send(to=to, subject="验证您转移工作空间所有权的请求", html=html_content)
  32. else:
  33. template = "transfer_workspace_owner_confirm_template_en-US.html"
  34. system_features = FeatureService.get_system_features()
  35. if system_features.branding.enabled:
  36. template = "without-brand/transfer_workspace_owner_confirm_template_en-US.html"
  37. html_content = render_template(template, to=to, code=code, WorkspaceName=workspace)
  38. mail.send(to=to, subject="Verify Your Request to Transfer Workspace Ownership", html=html_content)
  39. else:
  40. html_content = render_template(template, to=to, code=code, WorkspaceName=workspace)
  41. mail.send(to=to, subject="Verify Your Request to Transfer Workspace Ownership", html=html_content)
  42. end_at = time.perf_counter()
  43. logging.info(
  44. click.style(
  45. "Send owner transfer confirm mail to {} succeeded: latency: {}".format(to, end_at - start_at),
  46. fg="green",
  47. )
  48. )
  49. except Exception:
  50. logging.exception("owner transfer confirm email mail to {} failed".format(to))
  51. @shared_task(queue="mail")
  52. def send_old_owner_transfer_notify_email_task(language: str, to: str, workspace: str, new_owner_email: str):
  53. """
  54. Async Send owner transfer confirm mail
  55. :param language: Language in which the email should be sent (e.g., 'en', 'zh')
  56. :param to: Recipient email address
  57. :param workspace: Workspace name
  58. :param new_owner_email: New owner email
  59. """
  60. if not mail.is_inited():
  61. return
  62. logging.info(click.style("Start change email mail to {}".format(to), fg="green"))
  63. start_at = time.perf_counter()
  64. # send change email mail using different languages
  65. try:
  66. if language == "zh-Hans":
  67. template = "transfer_workspace_old_owner_notify_template_zh-CN.html"
  68. system_features = FeatureService.get_system_features()
  69. if system_features.branding.enabled:
  70. template = "without-brand/transfer_workspace_old_owner_notify_template_zh-CN.html"
  71. html_content = render_template(template, to=to, WorkspaceName=workspace, NewOwnerEmail=new_owner_email)
  72. mail.send(to=to, subject="工作区所有权已转移", html=html_content)
  73. else:
  74. html_content = render_template(template, to=to, WorkspaceName=workspace, NewOwnerEmail=new_owner_email)
  75. mail.send(to=to, subject="工作区所有权已转移", html=html_content)
  76. else:
  77. template = "transfer_workspace_old_owner_notify_template_en-US.html"
  78. system_features = FeatureService.get_system_features()
  79. if system_features.branding.enabled:
  80. template = "without-brand/transfer_workspace_old_owner_notify_template_en-US.html"
  81. html_content = render_template(template, to=to, WorkspaceName=workspace, NewOwnerEmail=new_owner_email)
  82. mail.send(to=to, subject="Workspace ownership has been transferred", html=html_content)
  83. else:
  84. html_content = render_template(template, to=to, WorkspaceName=workspace, NewOwnerEmail=new_owner_email)
  85. mail.send(to=to, subject="Workspace ownership has been transferred", html=html_content)
  86. end_at = time.perf_counter()
  87. logging.info(
  88. click.style(
  89. "Send owner transfer confirm mail to {} succeeded: latency: {}".format(to, end_at - start_at),
  90. fg="green",
  91. )
  92. )
  93. except Exception:
  94. logging.exception("owner transfer confirm email mail to {} failed".format(to))
  95. @shared_task(queue="mail")
  96. def send_new_owner_transfer_notify_email_task(language: str, to: str, workspace: str):
  97. """
  98. Async Send owner transfer confirm mail
  99. :param language: Language in which the email should be sent (e.g., 'en', 'zh')
  100. :param to: Recipient email address
  101. :param code: Change email code
  102. :param workspace: Workspace name
  103. """
  104. if not mail.is_inited():
  105. return
  106. logging.info(click.style("Start change email mail to {}".format(to), fg="green"))
  107. start_at = time.perf_counter()
  108. # send change email mail using different languages
  109. try:
  110. if language == "zh-Hans":
  111. template = "transfer_workspace_new_owner_notify_template_zh-CN.html"
  112. system_features = FeatureService.get_system_features()
  113. if system_features.branding.enabled:
  114. template = "without-brand/transfer_workspace_new_owner_notify_template_zh-CN.html"
  115. html_content = render_template(template, to=to, WorkspaceName=workspace)
  116. mail.send(to=to, subject=f"您现在是 {workspace} 的所有者", html=html_content)
  117. else:
  118. html_content = render_template(template, to=to, WorkspaceName=workspace)
  119. mail.send(to=to, subject=f"您现在是 {workspace} 的所有者", html=html_content)
  120. else:
  121. template = "transfer_workspace_new_owner_notify_template_en-US.html"
  122. system_features = FeatureService.get_system_features()
  123. if system_features.branding.enabled:
  124. template = "without-brand/transfer_workspace_new_owner_notify_template_en-US.html"
  125. html_content = render_template(template, to=to, WorkspaceName=workspace)
  126. mail.send(to=to, subject=f"You are now the owner of {workspace}", html=html_content)
  127. else:
  128. html_content = render_template(template, to=to, WorkspaceName=workspace)
  129. mail.send(to=to, subject=f"You are now the owner of {workspace}", html=html_content)
  130. end_at = time.perf_counter()
  131. logging.info(
  132. click.style(
  133. "Send owner transfer confirm mail to {} succeeded: latency: {}".format(to, end_at - start_at),
  134. fg="green",
  135. )
  136. )
  137. except Exception:
  138. logging.exception("owner transfer confirm email mail to {} failed".format(to))