def post(self, request): form = AlertTestPluginForm(request.POST) if form.is_valid(): data = form.clean() with transaction.atomic(): sid = transaction.savepoint() service = Service.objects.create(name='test-alert-service') check = StatusCheck(name='ALERT_TEST', calculated_status=data['new_status']) check.save() service.status_checks.add(check) service.alerts.add(data['alert_plugin']) service.update_status() service.overall_status = data['new_status'] service.old_overall_status = data['old_status'] self.trigger_alert_to_user(service, request.user) transaction.savepoint_rollback(sid) return JsonResponse({"result": "ok"}) return JsonResponse({"result": "error"}, status=400)
def trigger_alert_to_user(self, service, user, old_status, new_status): """ Clear out all service users and duty shifts, and disable all fallback users. Then add a single shift for this user, and add this user to users-to-notify. This should ensure we never alert anyone except the user triggering the alert test. """ with transaction.atomic(): sid = transaction.savepoint() service.update_status() service.status_checks.update(active=False) service.overall_status = new_status service.old_overall_status = old_status service.last_alert_sent = None check = StatusCheck(name='ALERT_TEST') check.save() StatusCheckResult.objects.create( status_check=check, time=timezone.now(), time_complete=timezone.now(), succeeded=new_status == Service.PASSING_STATUS) check.last_run = timezone.now() check.save() service.status_checks.add(check) service.users_to_notify.clear() service.users_to_notify.add(user) service.unexpired_acknowledgements().delete() Shift.objects.update(deleted=True) UserProfile.objects.update(fallback_alert_user=False) Shift( start=timezone.now() - timedelta(days=1), end=timezone.now() + timedelta(days=1), uid='test-shift', last_modified=timezone.now(), user=user ).save() service.alert() transaction.savepoint_rollback(sid)