Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

mail_change_mail_task.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task # type: ignore
  5. from extensions.ext_mail import mail
  6. from libs.email_i18n import EmailType, get_email_i18n_service
  7. @shared_task(queue="mail")
  8. def send_change_mail_task(language: str, to: str, code: str, phase: str) -> None:
  9. """
  10. Send change email notification with internationalization support.
  11. Args:
  12. language: Language code for email localization
  13. to: Recipient email address
  14. code: Email verification code
  15. phase: Change email phase ('old_email' or 'new_email')
  16. """
  17. if not mail.is_inited():
  18. return
  19. logging.info(click.style("Start change email mail to {}".format(to), fg="green"))
  20. start_at = time.perf_counter()
  21. try:
  22. email_service = get_email_i18n_service()
  23. email_service.send_change_email(
  24. language_code=language,
  25. to=to,
  26. code=code,
  27. phase=phase,
  28. )
  29. end_at = time.perf_counter()
  30. logging.info(
  31. click.style("Send change email mail to {} succeeded: latency: {}".format(to, end_at - start_at), fg="green")
  32. )
  33. except Exception:
  34. logging.exception("Send change email mail to {} failed".format(to))
  35. @shared_task(queue="mail")
  36. def send_change_mail_completed_notification_task(language: str, to: str) -> None:
  37. """
  38. Send change email completed notification with internationalization support.
  39. Args:
  40. language: Language code for email localization
  41. to: Recipient email address
  42. """
  43. if not mail.is_inited():
  44. return
  45. logging.info(click.style("Start change email completed notify mail to {}".format(to), fg="green"))
  46. start_at = time.perf_counter()
  47. try:
  48. email_service = get_email_i18n_service()
  49. email_service.send_email(
  50. email_type=EmailType.CHANGE_EMAIL_COMPLETED,
  51. language_code=language,
  52. to=to,
  53. template_context={
  54. "to": to,
  55. "email": to,
  56. },
  57. )
  58. end_at = time.perf_counter()
  59. logging.info(
  60. click.style(
  61. "Send change email completed mail to {} succeeded: latency: {}".format(to, end_at - start_at),
  62. fg="green",
  63. )
  64. )
  65. except Exception:
  66. logging.exception("Send change email completed mail to {} failed".format(to))