diff --git a/core/notifications/api_v2.py b/core/notifications/api_v2.py index c06673b..f50a453 100644 --- a/core/notifications/api_v2.py +++ b/core/notifications/api_v2.py @@ -1,7 +1,15 @@ +from django.contrib.auth.decorators import permission_required from rest_framework import routers, viewsets +from django.urls import re_path +from rest_framework.decorators import api_view, permission_classes +from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response + from notifications.models import MessageTemplate from rest_framework import serializers +from notifications.templates import TEMPLATE_VARS + class MessageTemplateSerializer(serializers.ModelSerializer): class Meta: @@ -14,7 +22,16 @@ class MessageTemplateViewSet(viewsets.ModelViewSet): queryset = MessageTemplate.objects.all() +@api_view(['GET']) +@permission_classes([IsAuthenticated]) +@permission_required('tickets.add_issuethread_manual', raise_exception=True) # TDOO: change this permission +def get_template_vars(self): + return Response(TEMPLATE_VARS, status=200) + + router = routers.SimpleRouter() router.register(r'message_templates', MessageTemplateViewSet) -urlpatterns = ([] + router.urls) +urlpatterns = ([ + re_path('message_template_variables', get_template_vars), + ] + router.urls) diff --git a/core/notifications/models.py b/core/notifications/models.py index af33126..caeab7a 100644 --- a/core/notifications/models.py +++ b/core/notifications/models.py @@ -25,4 +25,4 @@ class UserNotificationChannel(models.Model): return True def __str__(self): - return self.user.username + ' - ' + self.channel_type + ' -> ' + self.channel_target + return self.user.username + '(' + self.channel_type + ')' diff --git a/core/notifications/templates.py b/core/notifications/templates.py index e8e66b3..af77193 100644 --- a/core/notifications/templates.py +++ b/core/notifications/templates.py @@ -4,7 +4,7 @@ from core.settings import PRIMARY_HOST from notifications.models import MessageTemplate -TEMLATE_VARS = ['ticket_name', 'ticket_uuid', 'ticket_id', 'ticket_url', +TEMPLATE_VARS = ['ticket_name', 'ticket_uuid', 'ticket_id', 'ticket_url', 'current_state', 'previous_state', 'current_state_pretty', 'previous_state_pretty', 'event_slug', 'event_name', 'username', 'user_nick', diff --git a/web/src/store.js b/web/src/store.js index 6449abe..14f44cd 100644 --- a/web/src/store.js +++ b/web/src/store.js @@ -19,6 +19,8 @@ const store = createStore({ users: [], groups: [], state_options: [], + messageTemplates: [], + messageTemplatesVariables: [], lastEvent: '37C3', lastUsed: {}, @@ -201,6 +203,12 @@ const store = createStore({ setThumbnail(state, {url, data}) { state.thumbnailCache[url] = data; }, + setMessageTemplates(state, templates) { + state.messageTemplates = templates; + }, + setMessageTemplateVariables(state, variables) { + state.messageTemplatesVariables = variables; + }, }, actions: { async login({commit}, {username, password, remember}) { @@ -415,7 +423,19 @@ const store = createStore({ async updateTicketPartial({commit, state}, {id, ...ticket}) { const {data, success} = await http.patch(`/2/tickets/${id}/`, ticket, state.user.token); commit('updateTicket', data); - } + }, + async fetchMessageTemplates({commit, state}) { + const {data, success} = await http.get('/2/message_templates/', state.user.token); + if (data && success) { + commit('setMessageTemplates', data); + } + }, + async fetchMessageTemplateVariables({commit, state}) { + const {data, success} = await http.get('/2/message_template_variables/', state.user.token); + if (data && success) { + commit('setMessageTemplateVariables', data); + } + }, }, plugins: [ persistentStatePlugin({ // TODO change remember to some kind of enable field @@ -443,6 +463,8 @@ const store = createStore({ "groups", "loadedBoxes", "loadedItems", + "messageTemplates", + "messageTemplatesVariables", ], watch: [ "test", @@ -453,6 +475,8 @@ const store = createStore({ "groups", "loadedBoxes", "loadedItems", + "messageTemplates", + "messageTemplatesVariables", ], mutations: [ //"replaceTickets", diff --git a/web/src/views/admin/Settings.vue b/web/src/views/admin/Settings.vue index af9ac39..c945dd1 100644 --- a/web/src/views/admin/Settings.vue +++ b/web/src/views/admin/Settings.vue @@ -1,5 +1,16 @@