Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow testing notifiers before registering them #419

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions promgen/static/js/promgen.vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ['[[', ']]'],
Expand Down Expand Up @@ -263,3 +273,46 @@ const ExporterTest = Vue.component('exporter-test', {
}
}
})

const NotifierPreTestResult = Vue.component("notifier-pre-test-result", {
data: () => ({
state: notifierPreTestResultStore.state,
}),
template: `
<bootstrap-panel
:class="{
'panel-info': state.result === 'success',
'panel-danger': state.result === 'error',
}"
heading="Notifier test result"
v-if="state.show"
>
{{ state.msg }}
</bootstrap-panel>`,
});

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: `
<button @click.prevent="onTestSubmit">
<slot />
</button>`,
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));
},
},
});
10 changes: 10 additions & 0 deletions promgen/templates/promgen/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ <h1>{{user.username}} ({{user.email}})</h1>
</table>
<div class="panel-footer">
<button class="btn btn-primary">Register Notifier</button>
<notifier-pre-test
class="btn btn-info"
href="{% url 'notifier-pre-test' %}"
>
{% trans "Test" %}
</notifier-pre-test>
</div>
</form>
</div>
Expand All @@ -88,6 +94,10 @@ <h1>{{user.username}} ({{user.email}})</h1>
</div>
</div>

<div>
<notifier-pre-test-result></notifier-pre-test-result>
</div>

{% if user.is_staff %}
<div class="panel panel-warning">
<div class="panel-heading">Debug</div>
Expand Down
1 change: 1 addition & 0 deletions promgen/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
path("notifier/<int:pk>/test", views.NotifierTest.as_view(), name="notifier-test"),
path("notifier/<int:pk>", views.NotifierUpdate.as_view(), name="notifier-edit"),
path("notifier/<int:pk>/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/<int:pk>", views.RuleDetail.as_view(), name="rule-detail"),
Expand Down
22 changes: 22 additions & 0 deletions promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down