email.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from django.contrib.auth import get_user_model
  2. from django.db.models import Q
  3. from django.utils import timezone
  4. from django.utils.translation import gettext_lazy as _
  5. from glitchtip.email import DetailEmail
  6. from users.models import ProjectAlertStatus, User
  7. from .models import MonitorCheck
  8. User = get_user_model()
  9. class MonitorEmail(DetailEmail):
  10. html_template_name = "uptime/alert.html"
  11. text_template_name = "uptime/alert.txt"
  12. subject_template_name = "uptime/alert-subject.txt"
  13. model = MonitorCheck
  14. went_down = True
  15. last_change = None
  16. def get_context_data(self, **kwargs):
  17. context = super().get_context_data(**kwargs)
  18. if self.went_down:
  19. context["status_msg"] = _("is down")
  20. else:
  21. context["status_msg"] = _("is up")
  22. if self.last_change:
  23. context["time_since_last_change"] = timezone.now() - self.last_change
  24. return context
  25. def get_users(self):
  26. monitor = self.object.monitor
  27. return User.objects.filter(
  28. organizations_ext_organization__projects__monitor=monitor
  29. ).exclude(
  30. Q(
  31. userprojectalert__project=monitor.project,
  32. userprojectalert__status=ProjectAlertStatus.OFF,
  33. )
  34. | Q(subscribe_by_default=False, userprojectalert=None),
  35. )