diff --git a/promgen/static/js/promgen.vue.js b/promgen/static/js/promgen.vue.js index ae8f6d6ef..5df0eb935 100644 --- a/promgen/static/js/promgen.vue.js +++ b/promgen/static/js/promgen.vue.js @@ -38,6 +38,16 @@ const silenceStore = { } }; +const notifierPreTestResultStore = { + state: { + result: null, + msg: null, + show: false, + }, + show() { this.state.show = true }, + set(key, val) { this.state[key] = val }, +}; + const app = new Vue({ el: '#vue', delimiters: ['[[', ']]'], @@ -263,3 +273,46 @@ const ExporterTest = Vue.component('exporter-test', { } } }) + +const NotifierPreTestResult = Vue.component("notifier-pre-test-result", { + data: () => ({ + state: notifierPreTestResultStore.state, + }), + template: ` + + {{ state.msg }} + `, +}); + +const NotifierPreTest = Vue.component("notifier-pre-test", { + // Notifier Pre Test button for Forms. + // Acts like a regular form submit button, but hijacks the button click and + // submits it to an alternate URL for testing. + props: ["href"], + template: ` + `, + methods: { + onTestSubmit(event) { + // Find the parent form our button belongs to so that we can + // simulate a form submission. + const form = new FormData(event.srcElement.closest("form")); + fetch(this.href, { body: form, method: "post" }) + .then(resp => resp.json()) + .then(resp => { + notifierPreTestResultStore.set("result", resp.result); + notifierPreTestResultStore.set("msg", resp.msg); + notifierPreTestResultStore.show(); + }) + .catch((error) => alert(error)); + }, + }, +}); diff --git a/promgen/templates/promgen/profile.html b/promgen/templates/promgen/profile.html index a628ba9ed..e0221033b 100644 --- a/promgen/templates/promgen/profile.html +++ b/promgen/templates/promgen/profile.html @@ -78,6 +78,12 @@

{{user.username}} ({{user.email}})

@@ -88,6 +94,10 @@

{{user.username}} ({{user.email}})

+
+ +
+ {% if user.is_staff %}
Debug
diff --git a/promgen/urls.py b/promgen/urls.py index eaeeed625..5181ca25f 100644 --- a/promgen/urls.py +++ b/promgen/urls.py @@ -80,6 +80,7 @@ path("notifier//test", views.NotifierTest.as_view(), name="notifier-test"), path("notifier/", views.NotifierUpdate.as_view(), name="notifier-edit"), path("notifier//toggle", views.NotifierToggle.as_view(), name="notifier-toggle"), + path("notifier/pre-test", views.NotifierPreTest.as_view(), name="notifier-pre-test"), # Rules path("rule", views.RulesList.as_view(), name="rules-list"), path("rule/", views.RuleDetail.as_view(), name="rule-detail"), diff --git a/promgen/views.py b/promgen/views.py index c00b318bc..d08514ad0 100644 --- a/promgen/views.py +++ b/promgen/views.py @@ -321,6 +321,28 @@ def get_success_url(self): return reverse("profile") +class NotifierPreTest(LoginRequiredMixin, View): + def post(self, request): + data = request.POST.dict() + sender = models.Sender( + sender=data.get("sender"), + value=data.get("value"), + alias=data.get("alias"), + ) + + try: + sender.test() + except Exception as e: + return JsonResponse({ + "result": "error", + "msg": f"Error sending test message with {sender.sender}" + }) + else: + return JsonResponse({ + "result": "success", + "msg": f"Sent test message with {sender.sender}", + }) + class NotifierTest(LoginRequiredMixin, View): def post(self, request, pk): sender = get_object_or_404(models.Sender, id=pk)