email.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from django.db.models import Q
  2. from django.conf import settings
  3. from django.contrib.auth import get_user_model
  4. from users.models import ProjectAlertStatus
  5. from glitchtip.email import GlitchTipEmail
  6. User = get_user_model()
  7. class AlertEmail(GlitchTipEmail):
  8. html_template_name = "alerts/issue.html"
  9. text_template_name = "alerts/issue.txt"
  10. subject_template_name = "alerts/issue-subject.txt"
  11. notification = None
  12. def get_context_data(self, **kwargs):
  13. context = super().get_context_data(**kwargs)
  14. notification = self.notification
  15. first_issue = notification.issues.all().first()
  16. base_url = settings.GLITCHTIP_URL.geturl()
  17. org_slug = first_issue.project.organization.slug
  18. issue_link = f"{base_url}/{org_slug}/issues/{first_issue.id}"
  19. settings_link = (
  20. f"{base_url}/{org_slug}/settings/projects/{first_issue.project.slug}"
  21. )
  22. context["issue_title"] = first_issue.title
  23. context["project_name"] = first_issue.project
  24. context["first_issue"] = first_issue
  25. context["issue_link"] = issue_link
  26. context["issues"] = notification.issues.all()
  27. context["issue_count"] = notification.issues.count()
  28. context["project_notification_settings_link"] = settings_link
  29. context["org_slug"] = org_slug
  30. context[
  31. "project_link"
  32. ] = f"{base_url}/{org_slug}/issues?project={first_issue.project.id}"
  33. return context
  34. def send_email_notification(notification):
  35. email = AlertEmail()
  36. email.notification = notification
  37. users = User.objects.filter(
  38. organizations_ext_organization__projects__projectalert__notification=notification
  39. ).exclude(
  40. Q(
  41. userprojectalert__project=notification.project_alert.project,
  42. userprojectalert__status=ProjectAlertStatus.OFF,
  43. )
  44. | Q(subscribe_by_default=False, userprojectalert=None),
  45. )
  46. if not users.exists():
  47. return
  48. email.send_users_email(users)