email.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from django.conf import settings
  2. from django.contrib.auth import get_user_model
  3. from django.core.mail import EmailMultiAlternatives
  4. from django.template.loader import render_to_string
  5. from django.views.generic.base import ContextMixin
  6. from django.views.generic.detail import SingleObjectMixin
  7. User = get_user_model()
  8. class GlitchTipEmail(ContextMixin):
  9. html_template_name = None
  10. text_template_name = None
  11. subject_template_name = None
  12. def __init__(self, **kwargs):
  13. self.kwargs = kwargs
  14. for key, value in kwargs.items():
  15. setattr(self, key, value)
  16. def get_html_template_name(self):
  17. return self.html_template_name
  18. def get_text_template_name(self):
  19. return self.text_template_name
  20. def get_subject_template_name(self):
  21. return self.subject_template_name
  22. def get_text_content(self, context):
  23. return render_to_string(self.get_text_template_name(), context)
  24. def get_html_content(self, context):
  25. return render_to_string(self.get_html_template_name(), context)
  26. def get_subject_content(self, context):
  27. return render_to_string(self.get_subject_template_name(), context).strip("\n")[
  28. :998
  29. ]
  30. def get_context_data(self, **kwargs):
  31. context = super().get_context_data(**kwargs)
  32. context["base_url"] = settings.GLITCHTIP_URL.geturl()
  33. return context
  34. def _send_email(self, context, to, users=None):
  35. subject = self.get_subject_content(context)
  36. msg = EmailMultiAlternatives(subject, self.get_text_content(context), to=to)
  37. if users:
  38. msg.merge_data = {user.email: {"unique_id": user.id} for user in users}
  39. msg.attach_alternative(self.get_html_content(context), "text/html")
  40. msg.send()
  41. def get_users(self):
  42. return User.objects.none()
  43. def send_users_email(self, users=None):
  44. """
  45. Send email to Django users, will set merge data to avoid exposing email addresses
  46. """
  47. context = self.get_context_data()
  48. if users is None:
  49. users = self.get_users()
  50. to = users.values_list("email", flat=True)
  51. self._send_email(context, to, users)
  52. def send_email(self, email: str):
  53. """ Send just one email """
  54. context = self.get_context_data()
  55. self._send_email(context, [email])
  56. class DetailEmail(SingleObjectMixin, GlitchTipEmail):
  57. def get_email(self):
  58. raise NotImplementedError
  59. def send_users_email(self, users=None):
  60. self.object = self.get_object()
  61. super().send_users_email(users)
  62. def send_email(self, email: str = None):
  63. """ Send just one email """
  64. self.object = self.get_object()
  65. context = self.get_context_data()
  66. if email is None:
  67. email = self.get_email()
  68. self._send_email(context, [email])