Skip to content

Commit c9513eb

Browse files
Allow testing notifiers before registering them
This commit adds a new "Test" button next to the one for registering a notifier, so the user can use it to test if the notifier they are about to register actually works.
1 parent 3363e91 commit c9513eb

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

promgen/static/js/promgen.vue.js

+53
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ const silenceStore = {
3838
}
3939
};
4040

41+
const notifierPreTestResultStore = {
42+
state: {
43+
result: null,
44+
msg: null,
45+
show: false,
46+
},
47+
show() { this.state.show = true },
48+
set(key, val) { this.state[key] = val },
49+
};
50+
4151
const app = new Vue({
4252
el: '#vue',
4353
delimiters: ['[[', ']]'],
@@ -263,3 +273,46 @@ const ExporterTest = Vue.component('exporter-test', {
263273
}
264274
}
265275
})
276+
277+
const NotifierPreTestResult = Vue.component("notifier-pre-test-result", {
278+
data: () => ({
279+
state: notifierPreTestResultStore.state,
280+
}),
281+
template: `
282+
<bootstrap-panel
283+
:class="{
284+
'panel-info': state.result === 'success',
285+
'panel-danger': state.result === 'error'}
286+
"
287+
heading="Notifier test result"
288+
v-if="state.show"
289+
>
290+
{{ state.msg }}
291+
</bootstrap-panel>`,
292+
});
293+
294+
const NotifierPreTest = Vue.component("notifier-pre-test", {
295+
// Notifier Pre Test button for Forms.
296+
// Acts like a regular form submit button, but hijacks the button click and
297+
// submits it to an alternate URL for testing.
298+
props: ["href"],
299+
template: `
300+
<button @click.prevent="onTestSubmit">
301+
<slot />
302+
</button>`,
303+
methods: {
304+
onTestSubmit(event) {
305+
// Find the parent form our button belongs to so that we can
306+
// simulate a form submission.
307+
const form = new FormData(event.srcElement.closest("form"));
308+
fetch(this.href, { body: form, method: "post" })
309+
.then(resp => resp.json())
310+
.then(resp => {
311+
notifierPreTestResultStore.set("result", resp.result);
312+
notifierPreTestResultStore.set("msg", resp.msg);
313+
notifierPreTestResultStore.show();
314+
})
315+
.catch((error) => alert(error));
316+
},
317+
},
318+
});

promgen/templates/promgen/profile.html

+10
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ <h1>{{user.username}} ({{user.email}})</h1>
7878
</table>
7979
<div class="panel-footer">
8080
<button class="btn btn-primary">Register Notifier</button>
81+
<notifier-pre-test
82+
class="btn btn-info"
83+
href="{% url 'notifier-pre-test' %}"
84+
>
85+
{% trans "Test" %}
86+
</notifier-pre-test>
8187
</div>
8288
</form>
8389
</div>
@@ -88,6 +94,10 @@ <h1>{{user.username}} ({{user.email}})</h1>
8894
</div>
8995
</div>
9096

97+
<div>
98+
<notifier-pre-test-result></notifier-pre-test-result>
99+
</div>
100+
91101
{% if user.is_staff %}
92102
<div class="panel panel-warning">
93103
<div class="panel-heading">Debug</div>

promgen/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
path("notifier/<int:pk>/test", views.NotifierTest.as_view(), name="notifier-test"),
8181
path("notifier/<int:pk>", views.NotifierUpdate.as_view(), name="notifier-edit"),
8282
path("notifier/<int:pk>/toggle", views.NotifierToggle.as_view(), name="notifier-toggle"),
83+
path("notifier/pre-test", views.NotifierPreTest.as_view(), name="notifier-pre-test"),
8384
# Rules
8485
path("rule", views.RulesList.as_view(), name="rules-list"),
8586
path("rule/<int:pk>", views.RuleDetail.as_view(), name="rule-detail"),

promgen/views.py

+22
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,28 @@ def get_success_url(self):
321321
return reverse("profile")
322322

323323

324+
class NotifierPreTest(LoginRequiredMixin, View):
325+
def post(self, request):
326+
data = request.POST.dict()
327+
sender = models.Sender(
328+
sender=data.get("sender"),
329+
value=data.get("value"),
330+
alias=data.get("alias"),
331+
)
332+
333+
try:
334+
sender.test()
335+
except Exception as e:
336+
return JsonResponse({
337+
"result": "error",
338+
"msg": f"Error sending test message with {sender.sender}"
339+
})
340+
else:
341+
return JsonResponse({
342+
"result": "success",
343+
"msg": f"Sent test message with {sender.sender}",
344+
})
345+
324346
class NotifierTest(LoginRequiredMixin, View):
325347
def post(self, request, pk):
326348
sender = get_object_or_404(models.Sender, id=pk)

0 commit comments

Comments
 (0)