[WIP] add user notifications and text templates
This commit is contained in:
parent
2c609427ec
commit
b3f107d596
24 changed files with 771 additions and 46 deletions
|
@ -12,25 +12,7 @@ from knox.models import AuthToken
|
|||
from knox.views import LoginView as KnoxLoginView
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
permissions = serializers.SerializerMethodField()
|
||||
groups = serializers.SlugRelatedField(many=True, read_only=True, slug_field='name')
|
||||
|
||||
class Meta:
|
||||
model = ExtendedUser
|
||||
fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups')
|
||||
read_only_fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups')
|
||||
|
||||
def get_permissions(self, obj):
|
||||
return list(set(obj.get_permissions()))
|
||||
|
||||
|
||||
@receiver(post_save, sender=ExtendedUser)
|
||||
def create_auth_token(sender, instance=None, created=False, **kwargs):
|
||||
if created:
|
||||
AuthToken.objects.create(user=instance)
|
||||
from authentication.serializers import UserSerializer, GroupSerializer
|
||||
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
|
@ -38,26 +20,17 @@ class UserViewSet(viewsets.ModelViewSet):
|
|||
serializer_class = UserSerializer
|
||||
|
||||
|
||||
class GroupSerializer(serializers.ModelSerializer):
|
||||
permissions = serializers.SerializerMethodField()
|
||||
members = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Group
|
||||
fields = ('id', 'name', 'permissions', 'members')
|
||||
|
||||
def get_permissions(self, obj):
|
||||
return ["*:" + p.codename for p in obj.permissions.all()]
|
||||
|
||||
def get_members(self, obj):
|
||||
return [u.username for u in obj.user_set.all()]
|
||||
|
||||
|
||||
class GroupViewSet(viewsets.ModelViewSet):
|
||||
queryset = Group.objects.all()
|
||||
serializer_class = GroupSerializer
|
||||
|
||||
|
||||
@receiver(post_save, sender=ExtendedUser)
|
||||
def create_auth_token(sender, instance=None, created=False, **kwargs):
|
||||
if created:
|
||||
AuthToken.objects.create(user=instance)
|
||||
|
||||
|
||||
@api_view(['GET'])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def selfUser(request):
|
||||
|
|
32
core/authentication/serializers.py
Normal file
32
core/authentication/serializers.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
from rest_framework import serializers
|
||||
from django.contrib.auth.models import Group
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
permissions = serializers.SerializerMethodField()
|
||||
groups = serializers.SlugRelatedField(many=True, read_only=True, slug_field='name')
|
||||
|
||||
class Meta:
|
||||
model = ExtendedUser
|
||||
fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups')
|
||||
read_only_fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups')
|
||||
|
||||
def get_permissions(self, obj):
|
||||
return list(set(obj.get_permissions()))
|
||||
|
||||
|
||||
class GroupSerializer(serializers.ModelSerializer):
|
||||
permissions = serializers.SerializerMethodField()
|
||||
members = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Group
|
||||
fields = ('id', 'name', 'permissions', 'members')
|
||||
|
||||
def get_permissions(self, obj):
|
||||
return ["*:" + p.codename for p in obj.permissions.all()]
|
||||
|
||||
def get_members(self, obj):
|
||||
return [u.username for u in obj.user_set.all()]
|
|
@ -32,7 +32,9 @@ SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'django-insecure-tm*$w_14iqbiy-!7(8#
|
|||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = truthy_str(os.getenv('DEBUG_MODE_ACTIVE', 'False'))
|
||||
|
||||
ALLOWED_HOSTS = [os.getenv('HTTP_HOST', 'localhost')]
|
||||
PRIMARY_HOST = os.getenv('HTTP_HOST', 'localhost')
|
||||
|
||||
ALLOWED_HOSTS = [PRIMARY_HOST]
|
||||
|
||||
MAIL_DOMAIN = os.getenv('MAIL_DOMAIN', 'localhost')
|
||||
|
||||
|
@ -45,6 +47,10 @@ SYSTEM3_VERSION = "0.0.0-dev.0"
|
|||
|
||||
ACTIVE_SPAM_TRAINING = truthy_str(os.getenv('ACTIVE_SPAM_TRAINING', 'False'))
|
||||
|
||||
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN', '1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi')
|
||||
|
||||
TELEGRAM_GROUP_CHAT_ID = os.getenv('TELEGRAM_GROUP_CHAT_ID', '-1234567890')
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
|
@ -61,6 +67,7 @@ INSTALLED_APPS = [
|
|||
'drf_yasg',
|
||||
'channels',
|
||||
'authentication',
|
||||
'notifications',
|
||||
'files',
|
||||
'tickets',
|
||||
'inventory',
|
||||
|
|
|
@ -31,6 +31,7 @@ urlpatterns = [
|
|||
path('api/2/', include('mail.api_v2')),
|
||||
path('api/2/', include('notify_sessions.api_v2')),
|
||||
path('api/2/', include('authentication.api_v2')),
|
||||
path('api/2/', include('notifications.api_v2')),
|
||||
path('api/', get_info),
|
||||
path('', include('django_prometheus.urls')),
|
||||
]
|
||||
|
|
|
@ -7,6 +7,7 @@ from channels.db import database_sync_to_async
|
|||
from django.core.files.base import ContentFile
|
||||
|
||||
from mail.models import Email, EventAddress, EmailAttachment
|
||||
from notifications.templates import render_auto_reply
|
||||
from notify_sessions.models import SystemEvent
|
||||
from tickets.models import IssueThread
|
||||
|
||||
|
@ -87,6 +88,22 @@ def make_reply(reply_email, references=None, event=None):
|
|||
return reply
|
||||
|
||||
|
||||
def make_notification(message, to, title): # TODO where should replies to this go
|
||||
from email.message import EmailMessage
|
||||
from core.settings import MAIL_DOMAIN
|
||||
notification = EmailMessage()
|
||||
notification["From"] = "notifications@%s" % MAIL_DOMAIN
|
||||
notification["To"] = to
|
||||
notification["Subject"] = f"[C3LF Notification]%s" % title
|
||||
# notification["Reply-To"] = f"{event}@{MAIL_DOMAIN}"
|
||||
# notification["In-Reply-To"] = email.reference
|
||||
# notification["Message-ID"] = email.id + "@" + MAIL_DOMAIN
|
||||
|
||||
notification.set_content(message)
|
||||
|
||||
return notification
|
||||
|
||||
|
||||
async def send_smtp(message):
|
||||
await aiosmtplib.send(message, hostname="127.0.0.1", port=25, use_tls=False, start_tls=False)
|
||||
|
||||
|
@ -218,16 +235,7 @@ def receive_email(envelope, log=None):
|
|||
references = collect_references(active_issue_thread)
|
||||
if not sender.startswith('noreply'):
|
||||
subject = f"Re: {subject} [#{active_issue_thread.short_uuid()}]"
|
||||
body = '''Your request (#{}) has been received and will be reviewed by our lost&found angels.
|
||||
|
||||
We are reviewing incoming requests during the event and teardown. Immediately after the event, expect a delay as the \
|
||||
workload is high. We will not forget about your request and get back in touch once we have updated information on your \
|
||||
request. Requests for devices, wallets, credit cards or similar items will be handled with priority.
|
||||
|
||||
If you happen to find your lost item or just want to add additional information, please reply to this email. Please \
|
||||
do not create a new request.
|
||||
|
||||
Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid())
|
||||
body = render_auto_reply(active_issue_thread)
|
||||
reply_email = Email.objects.create(
|
||||
sender=recipient, recipient=sender, body=body, subject=subject,
|
||||
in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread)
|
||||
|
@ -270,11 +278,19 @@ class LMTPHandler:
|
|||
'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id,
|
||||
"message": "email received"})
|
||||
log.info(f"Sent message to frontend")
|
||||
|
||||
if new and reply:
|
||||
log.info('Sending message to %s' % reply['To'])
|
||||
await send_smtp(reply)
|
||||
log.info("Sent auto reply")
|
||||
|
||||
if thread:
|
||||
await channel_layer.group_send(
|
||||
'general', {"type": "generic.event", "name": "user_notification", "event_id": systemevent.id,
|
||||
"ticket_id": thread.id, "new": new})
|
||||
else:
|
||||
print("No thread found")
|
||||
|
||||
return '250 Message accepted for delivery'
|
||||
except SpecialMailException as e:
|
||||
import uuid
|
||||
|
|
20
core/mail/tests/v2/test_user_notifications.py
Normal file
20
core/mail/tests/v2/test_user_notifications.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from django.contrib.auth.models import Permission
|
||||
from django.test import TestCase
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
from notifications.models import UserNotificationChannel
|
||||
|
||||
|
||||
class UserNotificationTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
||||
self.user.user_permissions.add(*Permission.objects.all())
|
||||
self.user.save()
|
||||
self.channel = UserNotificationChannel.objects.create(user=self.user, channel_type='telegram',
|
||||
channel_target='123456789',
|
||||
event_filter='*', active=True)
|
||||
|
||||
async def test_telegram_notify(self):
|
||||
pass
|
0
core/notifications/__init__.py
Normal file
0
core/notifications/__init__.py
Normal file
15
core/notifications/admin.py
Normal file
15
core/notifications/admin.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
from django.contrib import admin
|
||||
|
||||
from notifications.models import MessageTemplate, UserNotificationChannel
|
||||
|
||||
|
||||
class MessageTemplateAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
|
||||
|
||||
class UserNotificationChannelAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
|
||||
|
||||
admin.site.register(MessageTemplate, MessageTemplateAdmin)
|
||||
admin.site.register(UserNotificationChannel, UserNotificationChannelAdmin)
|
51
core/notifications/api_v2.py
Normal file
51
core/notifications/api_v2.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
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, UserNotificationChannel
|
||||
from rest_framework import serializers
|
||||
|
||||
from notifications.templates import TEMPLATE_VARS
|
||||
from authentication.serializers import UserSerializer
|
||||
|
||||
|
||||
class MessageTemplateSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = MessageTemplate
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class UserNotificationChannelSerializer(serializers.ModelSerializer):
|
||||
user = UserSerializer()
|
||||
|
||||
class Meta:
|
||||
model = UserNotificationChannel
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class MessageTemplateViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = MessageTemplateSerializer
|
||||
queryset = MessageTemplate.objects.all()
|
||||
|
||||
|
||||
class UserNotificationChannelViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = UserNotificationChannelSerializer
|
||||
queryset = UserNotificationChannel.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)
|
||||
router.register(r'user_notification_channels', UserNotificationChannelViewSet)
|
||||
urlpatterns = ([
|
||||
re_path('message_template_variables', get_template_vars),
|
||||
] + router.urls)
|
16
core/notifications/defaults.py
Normal file
16
core/notifications/defaults.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
auto_reply_body = '''Your request (#{{ ticket_uuid }}) has been received and will be reviewed by our lost&found angels.
|
||||
|
||||
We are reviewing incoming requests during the event and teardown. Immediately after the event, expect a delay as the \
|
||||
workload is high. We will not forget about your request and get back in touch once we have updated information on your \
|
||||
request. Requests for devices, wallets, credit cards or similar items will be handled with priority.
|
||||
|
||||
If you happen to find your lost item or just want to add additional information, please reply to this email. Please \
|
||||
do not create a new request.
|
||||
|
||||
Your c3lf (Cloakroom + Lost&Found) Team'''
|
||||
|
||||
new_issue_notification = '''New issue "{{ ticket_name | limit_length }}" [{{ ticket_uuid }}] created
|
||||
{{ ticket_url }}'''
|
||||
|
||||
reply_issue_notification = '''Reply to issue "{{ ticket_name }}" [{{ ticket_uuid }}] (was {{ previous_state_pretty }})
|
||||
{{ ticket_url }}'''
|
85
core/notifications/dispatch.py
Normal file
85
core/notifications/dispatch.py
Normal file
|
@ -0,0 +1,85 @@
|
|||
import asyncio
|
||||
|
||||
from aiohttp.client import ClientSession
|
||||
from channels.layers import get_channel_layer
|
||||
from channels.db import database_sync_to_async
|
||||
from urllib.parse import quote as urlencode
|
||||
|
||||
from core.settings import TELEGRAM_BOT_TOKEN, TELEGRAM_GROUP_CHAT_ID
|
||||
from mail.protocol import send_smtp, make_notification
|
||||
from notifications.models import UserNotificationChannel
|
||||
from notifications.templates import render_notification_new_ticket_async, render_notification_reply_ticket_async
|
||||
from tickets.models import IssueThread
|
||||
|
||||
|
||||
async def http_get(url):
|
||||
async with ClientSession() as session:
|
||||
async with session.get(url) as response:
|
||||
return await response.text()
|
||||
|
||||
|
||||
async def telegram_notify(message, chat_id):
|
||||
encoded_message = urlencode(message)
|
||||
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage?chat_id={chat_id}&text={encoded_message}"
|
||||
return await http_get(url)
|
||||
|
||||
|
||||
async def email_notify(message, title, email):
|
||||
mail = make_notification(message, email, title)
|
||||
await send_smtp(mail)
|
||||
|
||||
|
||||
class NotificationDispatcher:
|
||||
channel_layer = None
|
||||
room_group_name = "general"
|
||||
|
||||
def __init__(self):
|
||||
self.channel_layer = get_channel_layer('default')
|
||||
if not self.channel_layer:
|
||||
raise Exception("Could not get channel layer")
|
||||
|
||||
@database_sync_to_async
|
||||
def get_notification_targets(self):
|
||||
channels = UserNotificationChannel.objects.filter(active=True)
|
||||
return list(channels)
|
||||
|
||||
@database_sync_to_async
|
||||
def get_ticket(self, ticket_id):
|
||||
return IssueThread.objects.filter(id=ticket_id).select_related('event').first()
|
||||
|
||||
async def run_forever(self):
|
||||
# Infinite loop to continuously listen for messages
|
||||
print("Listening for messages...")
|
||||
channel_name = await self.channel_layer.new_channel()
|
||||
await self.channel_layer.group_add(self.room_group_name, channel_name)
|
||||
print("Channel name:", channel_name)
|
||||
while True:
|
||||
# Blocking receive to get the message from the channel layer
|
||||
message = await self.channel_layer.receive(channel_name)
|
||||
|
||||
if (message and 'type' in message and message['type'] == 'generic.event' and 'name' in message and
|
||||
message['name'] == 'user_notification'):
|
||||
if 'ticket_id' in message and 'event_id' in message and 'new' in message:
|
||||
ticket = await self.get_ticket(message['ticket_id'])
|
||||
await self.dispatch(ticket, message['event_id'], message['new'])
|
||||
else:
|
||||
print("Error: Invalid message format")
|
||||
|
||||
async def dispatch(self, ticket, event_id, new):
|
||||
message = await render_notification_new_ticket_async(
|
||||
ticket) if new else await render_notification_reply_ticket_async(ticket)
|
||||
title = f"[#{ticket.short_uuid()}] {ticket.name}"
|
||||
print("Dispatching message:", message, "with event_id:", event_id)
|
||||
targets = await self.get_notification_targets()
|
||||
jobs = []
|
||||
jobs.append(telegram_notify(message, TELEGRAM_GROUP_CHAT_ID))
|
||||
for target in targets:
|
||||
if target.channel_type == 'telegram':
|
||||
print("Sending telegram notification to:", target.channel_target)
|
||||
jobs.append(telegram_notify(message, target.channel_target))
|
||||
elif target.channel_type == 'email':
|
||||
print("Sending email notification to:", target.channel_target)
|
||||
jobs.append(email_notify(message, title, target.channel_target))
|
||||
else:
|
||||
print("Unknown channel type:", target.channel_type)
|
||||
await asyncio.gather(*jobs)
|
51
core/notifications/migrations/0001_initial.py
Normal file
51
core/notifications/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
# Generated by Django 4.2.7 on 2024-05-03 21:02
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
from notifications.defaults import auto_reply_body, new_issue_notification, reply_issue_notification
|
||||
from notifications.models import MessageTemplate
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
def create_required_templates(apps, schema_editor):
|
||||
MessageTemplate.objects.create(name='auto_reply', message=auto_reply_body, marked_required=True)
|
||||
MessageTemplate.objects.create(name='new_issue_notification', message=new_issue_notification,
|
||||
marked_required=True)
|
||||
MessageTemplate.objects.create(name='reply_issue_notification', message=reply_issue_notification,
|
||||
marked_required=True)
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='MessageTemplate',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=255)),
|
||||
('message', models.TextField()),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('marked_confidential', models.BooleanField(default=False)),
|
||||
('marked_required', models.BooleanField(default=False)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='UserNotificationChannel',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('channel_type',
|
||||
models.CharField(choices=[('telegram', 'telegram'), ('email', 'email')], max_length=255)),
|
||||
('channel_target', models.CharField(max_length=255)),
|
||||
('event_filter', models.CharField(max_length=255)),
|
||||
('active', models.BooleanField(default=True)),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.RunPython(create_required_templates),
|
||||
]
|
0
core/notifications/migrations/__init__.py
Normal file
0
core/notifications/migrations/__init__.py
Normal file
29
core/notifications/models.py
Normal file
29
core/notifications/models.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
from django.db import models
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
|
||||
|
||||
class MessageTemplate(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
message = models.TextField()
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
marked_confidential = models.BooleanField(default=False)
|
||||
marked_required = models.BooleanField(default=False) # may not be deleted
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class UserNotificationChannel(models.Model):
|
||||
user = models.ForeignKey(ExtendedUser, models.CASCADE)
|
||||
channel_type = models.CharField(choices=[('telegram', 'telegram'), ('email', 'email')], max_length=255)
|
||||
channel_target = models.CharField(max_length=255)
|
||||
event_filter = models.CharField(max_length=255)
|
||||
active = models.BooleanField(default=True)
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def validate_constraints(self, exclude=None): # TODO: email -> emailaddress, telegram -> chatid
|
||||
return True
|
||||
|
||||
def __str__(self):
|
||||
return self.user.username + '(' + self.channel_type + ')'
|
69
core/notifications/templates.py
Normal file
69
core/notifications/templates.py
Normal file
|
@ -0,0 +1,69 @@
|
|||
import jinja2
|
||||
from channels.db import database_sync_to_async
|
||||
from core.settings import PRIMARY_HOST
|
||||
|
||||
from notifications.models import MessageTemplate
|
||||
|
||||
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',
|
||||
'web_host'] # TODO customer_name, tracking_code
|
||||
|
||||
|
||||
def limit_length(s, length=50):
|
||||
if len(s) > length:
|
||||
return s[:(length - 3)] + "..."
|
||||
return s
|
||||
|
||||
|
||||
def ticket_url(ticket):
|
||||
eventslug = ticket.event.slug if ticket.event else "37C3" # TODO 37C3 should not be hardcoded
|
||||
return f"https://{PRIMARY_HOST}/{eventslug}/ticket/{ticket.id}/"
|
||||
|
||||
|
||||
def render_template(template, **kwargs):
|
||||
try:
|
||||
environment = jinja2.Environment()
|
||||
environment.filters['limit_length'] = limit_length
|
||||
tmpl = MessageTemplate.objects.get(name=template)
|
||||
template = environment.from_string(tmpl.message)
|
||||
return template.render(**kwargs, web_host=PRIMARY_HOST)
|
||||
except MessageTemplate.DoesNotExist:
|
||||
return None
|
||||
|
||||
|
||||
def get_ticket_vars(ticket):
|
||||
states = list(ticket.state_changes.order_by('-timestamp'))
|
||||
return {
|
||||
'ticket_name': ticket.name,
|
||||
'ticket_uuid': ticket.short_uuid(),
|
||||
'ticket_id': ticket.id,
|
||||
'ticket_url': ticket_url(ticket),
|
||||
'current_state': states[0].state if states else 'none',
|
||||
'previous_state': states[1].state if len(states) > 1 else 'none',
|
||||
'current_state_pretty': states[0].get_state_display() if states else 'none',
|
||||
'previous_state_pretty': states[1].get_state_display() if len(states) > 1 else 'none',
|
||||
'event_slug': ticket.event.slug if ticket.event else "37C3", # TODO 37C3 should not be hardcoded
|
||||
'event_name': ticket.event.name if ticket.event else "37C3",
|
||||
}
|
||||
|
||||
|
||||
def render_auto_reply(ticket):
|
||||
return render_template('auto_reply', **get_ticket_vars(ticket))
|
||||
|
||||
|
||||
def render_notification_new_ticket(ticket):
|
||||
return render_template('new_issue_notification', **get_ticket_vars(ticket))
|
||||
|
||||
|
||||
def render_notification_reply_ticket(ticket):
|
||||
return render_template('reply_issue_notification', **get_ticket_vars(ticket))
|
||||
|
||||
|
||||
async def render_notification_new_ticket_async(ticket):
|
||||
return await database_sync_to_async(render_notification_new_ticket)(ticket)
|
||||
|
||||
|
||||
async def render_notification_reply_ticket_async(ticket):
|
||||
return await database_sync_to_async(render_notification_reply_ticket)(ticket)
|
0
core/notifications/tests/__init__.py
Normal file
0
core/notifications/tests/__init__.py
Normal file
|
@ -12,6 +12,7 @@ django.setup()
|
|||
from helper import init_loop
|
||||
from mail.protocol import LMTPHandler
|
||||
from mail.socket import UnixSocketLMTPController
|
||||
from notifications.dispatch import NotificationDispatcher
|
||||
|
||||
|
||||
class UvicornServer(uvicorn.Server):
|
||||
|
@ -54,6 +55,11 @@ async def lmtp(loop):
|
|||
log.info("LMTP done")
|
||||
|
||||
|
||||
async def notifications(loop):
|
||||
dispatcher = NotificationDispatcher()
|
||||
await dispatcher.run_forever()
|
||||
|
||||
|
||||
def main():
|
||||
import sdnotify
|
||||
import setproctitle
|
||||
|
@ -67,6 +73,7 @@ def main():
|
|||
loop.create_task(web(loop))
|
||||
# loop.create_task(tcp(loop))
|
||||
loop.create_task(lmtp(loop))
|
||||
loop.create_task(notifications(loop))
|
||||
n = sdnotify.SystemdNotifier()
|
||||
n.notify("READY=1")
|
||||
log.info("Server ready")
|
||||
|
|
|
@ -13,3 +13,5 @@ STATIC_ROOT=/var/www/c3lf-sys3/staticfiles
|
|||
ACTIVE_SPAM_TRAINING=True
|
||||
DEBUG_MODE_ACTIVE={{ debug_mode_active }}
|
||||
DJANGO_SECRET_KEY={{ django_secret_key }}
|
||||
TELEGRAM_GROUP_CHAT_ID={{ telegram_group_chat_id }}
|
||||
TELEGRAM_BOT_TOKEN={{ telegram_bot_token }}
|
112
web/src/components/inputs/FormatedText.vue
Normal file
112
web/src/components/inputs/FormatedText.vue
Normal file
|
@ -0,0 +1,112 @@
|
|||
<template>
|
||||
<div contenteditable @input="onchange" ref="text">
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'FormatedText',
|
||||
props: {
|
||||
value: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
format: {
|
||||
type: Function,
|
||||
default: null
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
selection: {start: 0, end: 0, direction: 'forward', type: 'Caret'}
|
||||
};
|
||||
},
|
||||
emits: ['input'],
|
||||
methods: {
|
||||
rawhtml(value) {
|
||||
if (typeof this.format === 'function') {
|
||||
return this.format(value.replace(/ /g, ' '));
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
},
|
||||
onchange(event) {
|
||||
const div = this.$refs.text;
|
||||
const sel = window.getSelection();
|
||||
if (sel.rangeCount > 0) {
|
||||
this.selection.start = this.calculateOffset(div, sel.anchorNode, sel.anchorOffset);
|
||||
this.selection.end = this.calculateOffset(div, sel.focusNode, sel.focusOffset);
|
||||
this.selection.direction = sel.direction;
|
||||
this.selection.type = sel.type;
|
||||
}
|
||||
this.$emit('input', event.target.innerText.replace(/ /g, ' ').replace(/\xA0/g, ' '));
|
||||
},
|
||||
calculateOffset(container, node, offset) {
|
||||
let position = 0;
|
||||
let found = false;
|
||||
const walk = (elem) => {
|
||||
if (elem === node) {
|
||||
found = true;
|
||||
return;
|
||||
}
|
||||
if (elem.nodeType === 3) {
|
||||
position += elem.length;
|
||||
} else {
|
||||
for (let i = 0; i < elem.childNodes.length; i++) {
|
||||
walk(elem.childNodes[i]);
|
||||
if (found) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
walk(container);
|
||||
return position + offset;
|
||||
},
|
||||
findNode(container, offset) {
|
||||
let position = 0;
|
||||
let found = false;
|
||||
let node = null;
|
||||
const walk = (elem) => {
|
||||
if (position + elem.length >= offset) {
|
||||
found = true;
|
||||
node = elem;
|
||||
return;
|
||||
}
|
||||
if (elem.nodeType === 3) {
|
||||
position += elem.length;
|
||||
} else {
|
||||
for (let i = 0; i < elem.childNodes.length; i++) {
|
||||
walk(elem.childNodes[i]);
|
||||
if (found) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
walk(container);
|
||||
return [node, offset - position]
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
value() {
|
||||
if (this.selection) {
|
||||
const div = this.$refs.text;
|
||||
div.innerHTML = this.rawhtml(this.value);
|
||||
|
||||
const range = document.createRange();
|
||||
const sel = window.getSelection();
|
||||
range.setStart(...this.findNode(div, this.selection.start));
|
||||
range.setEnd(...this.findNode(div, this.selection.end));
|
||||
|
||||
sel.removeAllRanges();
|
||||
sel.addRange(range);
|
||||
}
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
const div = this.$refs.text;
|
||||
div.innerHTML = this.rawhtml(this.value);
|
||||
}
|
||||
};
|
||||
</script>
|
|
@ -13,9 +13,11 @@ import Ticket from "@/views/Ticket.vue";
|
|||
import Admin from "@/views/admin/Admin.vue";
|
||||
import Empty from "@/views/Empty.vue";
|
||||
import Events from "@/views/admin/Events.vue";
|
||||
import Settings from "@/views/admin/Settings.vue";
|
||||
import AccessControl from "@/views/admin/AccessControl.vue";
|
||||
import {default as BoxesAdmin} from "@/views/admin/Boxes.vue"
|
||||
import Shipping from "@/views/admin/Shipping.vue";
|
||||
import Notifications from "@/views/admin/Notifications.vue";
|
||||
|
||||
const routes = [
|
||||
{path: '/', redirect: '/37C3/items', meta: {requiresAuth: false}},
|
||||
|
@ -58,6 +60,10 @@ const routes = [
|
|||
path: 'events/', name: 'events', component: Events, meta:
|
||||
{requiresAuth: true, requiresPermission: 'delete_event'}
|
||||
},
|
||||
{
|
||||
path: 'settings/', name: 'settings', component: Settings, meta:
|
||||
{requiresAuth: true, requiresPermission: 'delete_event'}
|
||||
},
|
||||
{
|
||||
path: '', name: 'admin', component: Dashboard, meta:
|
||||
{requiresAuth: true, requiresPermission: 'delete_event'}
|
||||
|
@ -74,6 +80,10 @@ const routes = [
|
|||
path: 'shipping/', name: 'shipping', component: Shipping, meta:
|
||||
{requiresAuth: true, requiresPermission: 'delete_event'}
|
||||
},
|
||||
{
|
||||
path: 'notifications/', name: 'notifications', component: Notifications, meta:
|
||||
{requiresAuth: true, requiresPermission: 'delete_event'}
|
||||
}
|
||||
]
|
||||
},
|
||||
{path: '/user', name: 'user', component: Empty, meta: {requiresAuth: true}},
|
||||
|
|
|
@ -19,7 +19,10 @@ const store = createStore({
|
|||
users: [],
|
||||
groups: [],
|
||||
state_options: [],
|
||||
messageTemplates: [],
|
||||
messageTemplateVariables: [],
|
||||
shippingVouchers: [],
|
||||
userNotificationChannels: [],
|
||||
|
||||
lastEvent: '37C3',
|
||||
lastUsed: {},
|
||||
|
@ -42,7 +45,9 @@ const store = createStore({
|
|||
users: 0,
|
||||
groups: 0,
|
||||
states: 0,
|
||||
messageTemplates: 0,
|
||||
shippingVouchers: 0,
|
||||
userNotificationChannels: 0,
|
||||
},
|
||||
persistent_loaded: false,
|
||||
shared_loaded: false,
|
||||
|
@ -222,10 +227,21 @@ const store = createStore({
|
|||
setThumbnail(state, {url, data}) {
|
||||
state.thumbnailCache[url] = data;
|
||||
},
|
||||
setMessageTemplates(state, templates) {
|
||||
state.messageTemplates = templates;
|
||||
state.fetchedData = {...state.fetchedData, messageTemplates: Date.now()};
|
||||
},
|
||||
setMessageTemplateVariables(state, variables) {
|
||||
state.messageTemplateVariables = variables;
|
||||
},
|
||||
setShippingVouchers(state, codes) {
|
||||
state.shippingVouchers = codes;
|
||||
state.fetchedData = {...state.fetchedData, shippingVouchers: Date.now()};
|
||||
},
|
||||
setUserNotificationChannels(state, channels) {
|
||||
state.userNotificationChannels = channels;
|
||||
state.fetchedData = {...state.fetchedData, userNotificationChannels: Date.now()};
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
async login({commit}, {username, password, remember}) {
|
||||
|
@ -459,6 +475,39 @@ const store = createStore({
|
|||
const {data, success} = await http.patch(`/2/tickets/${id}/`, ticket, state.user.token);
|
||||
commit('updateTicket', data);
|
||||
},
|
||||
async fetchMessageTemplates({commit, state}) {
|
||||
if (!state.user.token) return;
|
||||
if (state.messageTemplates.length > 0) return;
|
||||
const {data, success} = await http.get('/2/message_templates/', state.user.token);
|
||||
if (data && success) {
|
||||
commit('setMessageTemplates', data);
|
||||
}
|
||||
},
|
||||
async updateMessageTemplate({dispatch, state}, template) {
|
||||
const {data, success} = await http.patch(`/2/message_templates/${template.id}/`,
|
||||
{'message': template.message}, state.user.token);
|
||||
if (data && success) {
|
||||
state.fetchedData.messageTemplates = 0;
|
||||
dispatch('fetchMessageTemplates');
|
||||
}
|
||||
},
|
||||
async fetchMessageTemplateVariables({commit, state}) {
|
||||
if (!state.user.token) return;
|
||||
if (state.messageTemplateVariables.length > 0) return;
|
||||
const {data, success} = await http.get('/2/message_template_variables/', state.user.token);
|
||||
if (data && success) {
|
||||
commit('setMessageTemplateVariables', data);
|
||||
}
|
||||
},
|
||||
async createMessageTemplate({dispatch, state}, template_name) {
|
||||
const {data, success} = await http.post('/2/message_templates/', {
|
||||
name: template_name,
|
||||
message: '-'
|
||||
}, state.user.token);
|
||||
if (data && success) {
|
||||
dispatch('fetchMessageTemplates');
|
||||
}
|
||||
},
|
||||
async fetchShippingVouchers({commit, state}) {
|
||||
if (!state.user.token) return;
|
||||
if (state.fetchedData.shippingVouchers > Date.now() - 1000 * 60 * 60 * 24) return;
|
||||
|
@ -485,7 +534,15 @@ const store = createStore({
|
|||
state.fetchedData.tickets = 0;
|
||||
await Promise.all([dispatch('loadTickets'), dispatch('fetchShippingVouchers')]);
|
||||
}
|
||||
}
|
||||
},
|
||||
async fetchUserNotificationChannels({commit, state}) {
|
||||
if (!state.user.token) return;
|
||||
if (state.fetchedData.userNotificationChannels > Date.now() - 1000 * 60 * 60 * 24) return;
|
||||
const {data, success} = await http.get('/2/user_notification_channels/', state.user.token);
|
||||
if (data && success) {
|
||||
commit('setUserNotificationChannels', data);
|
||||
}
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
persistentStatePlugin({ // TODO change remember to some kind of enable field
|
||||
|
@ -513,6 +570,8 @@ const store = createStore({
|
|||
"groups",
|
||||
"loadedBoxes",
|
||||
"loadedItems",
|
||||
"messageTemplates",
|
||||
"messageTemplatesVariables",
|
||||
"shippingVouchers",
|
||||
],
|
||||
watch: [
|
||||
|
@ -524,6 +583,8 @@ const store = createStore({
|
|||
"groups",
|
||||
"loadedBoxes",
|
||||
"loadedItems",
|
||||
"messageTemplates",
|
||||
"messageTemplatesVariables",
|
||||
"shippingVouchers",
|
||||
],
|
||||
mutations: [
|
||||
|
|
|
@ -8,9 +8,15 @@
|
|||
<li class="nav-item">
|
||||
<router-link class="nav-link" :to="{name: 'admin'}" active-class="dummy" exact-active-class="active">Dashboard</router-link>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<router-link class="nav-link" :to="{name: 'settings'}" active-class="active">Settings</router-link>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<router-link class="nav-link" :to="{name: 'events'}" active-class="active">Events</router-link>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<router-link class="nav-link" :to="{name: 'notifications'}" active-class="active">Notifications</router-link>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<router-link class="nav-link" :to="{name: 'shipping'}" active-class="active">Shipping</router-link>
|
||||
</li>
|
||||
|
|
53
web/src/views/admin/Notifications.vue
Normal file
53
web/src/views/admin/Notifications.vue
Normal file
|
@ -0,0 +1,53 @@
|
|||
<template>
|
||||
<div>
|
||||
<Table :items="userNotificationChannels.map(channel => ({...channel, username: channel.user.username || {}}))"
|
||||
:columns="['id', 'username', 'channel_type', 'channel_target', 'event_filter', /*'active', 'created'*/]">
|
||||
<template #actions="{ item }">
|
||||
<div class="btn-group">
|
||||
<button class="btn btn-danger" @click.stop="">
|
||||
<font-awesome-icon icon="trash"/>
|
||||
delete
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
</Table>
|
||||
<div class="card bg-dark">
|
||||
<div class="card-body">
|
||||
<div class="input-group">
|
||||
<select class="form-control">
|
||||
<option value="1">user</option>
|
||||
<option value="2">admin</option>
|
||||
</select>
|
||||
<select class="form-control">
|
||||
<option value="email">Email</option>
|
||||
<option value="telegram">Telegram</option>
|
||||
</select>
|
||||
<input type="text" class="form-control" placeholder="channel_target">
|
||||
<input type="text" class="form-control" value="*">
|
||||
<div class="input-group-append">
|
||||
<button class="btn btn-primary">Add</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {mapActions, mapState} from 'vuex';
|
||||
import Table from '@/components/Table';
|
||||
|
||||
export default {
|
||||
name: 'Notifications',
|
||||
components: {Table},
|
||||
computed: mapState(['userNotificationChannels']),
|
||||
methods: mapActions(['fetchUserNotificationChannels']),
|
||||
mounted() {
|
||||
this.fetchUserNotificationChannels();
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
109
web/src/views/admin/Settings.vue
Normal file
109
web/src/views/admin/Settings.vue
Normal file
|
@ -0,0 +1,109 @@
|
|||
<template>
|
||||
<h3 class="text-center">Available Message Template Variables</h3>
|
||||
<p>
|
||||
<span v-for="(variable, key) in messageTemplateVariables" :key="key" class="badge badge-primary"
|
||||
style="margin: 5px;">
|
||||
{{ variable }}
|
||||
</span>
|
||||
</p>
|
||||
<h3 class="text-center">Message Templates</h3>
|
||||
<div v-for="template in messageTemplatesIntermediate" :key="template.id" class="card bg-dark"
|
||||
style="margin-bottom: 10px;">
|
||||
<div class="card-header">{{ template.name }}</div>
|
||||
<FormatedText :value="template.message" :format="formatText" class="card-body"
|
||||
@input="changeMessageTemplate(template.id, $event)"/>
|
||||
<div class="card-body">
|
||||
<button class="btn btn-primary" @click="resetMessageTemplate(template.id)"
|
||||
:disabled="messageTemplates.find(t => t.id === template.id).message === template.message">Reset
|
||||
</button>
|
||||
<button class="btn btn-success" @click="saveMessageTemplate(template.id)"
|
||||
:disabled="messageTemplates.find(t => t.id === template.id).message === template.message">Save
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card bg-dark">
|
||||
<div class="card-body">
|
||||
<div class="input-group">
|
||||
<input type="text" class="form-control" v-model="newTemplateName" placeholder="New Template Name">
|
||||
<button class="btn btn-success input-group-btn" @click="createMessageTemplateAndReset()"
|
||||
ref="createButton">Create
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {mapActions, mapState} from 'vuex';
|
||||
import Table from '@/components/Table';
|
||||
import FormatedText from "@/components/inputs/FormatedText.vue";
|
||||
|
||||
export default {
|
||||
name: 'Settings',
|
||||
components: {FormatedText, Table},
|
||||
data() {
|
||||
return {
|
||||
messageTemplatesIntermediate: [],
|
||||
newTemplateName: '',
|
||||
};
|
||||
},
|
||||
computed: mapState(['messageTemplates', 'messageTemplateVariables']),
|
||||
methods: {
|
||||
...mapActions(['fetchMessageTemplates', 'fetchMessageTemplateVariables', 'updateMessageTemplate', 'createMessageTemplate']),
|
||||
formatText(value) {
|
||||
return value.replace(/{{(.+?)}}/g, (match, key) => {
|
||||
return `<span class="text-primary">{{${key}}}</span>`;
|
||||
}).replace(/\n/g, '<br>').replace(/\t/g, ' ');
|
||||
},
|
||||
changeMessageTemplate(id, message) {
|
||||
console.log(id, message);
|
||||
this.messageTemplatesIntermediate.forEach(template => {
|
||||
if (template.id === id) {
|
||||
template.message = message;
|
||||
}
|
||||
});
|
||||
},
|
||||
saveMessageTemplate(id) {
|
||||
this.updateMessageTemplate(this.messageTemplatesIntermediate.find(template => template.id === id));
|
||||
},
|
||||
resetMessageTemplate(id) {
|
||||
this.messageTemplatesIntermediate.find(template => template.id === id).message =
|
||||
this.messageTemplates.find(template => template.id === id).message;
|
||||
},
|
||||
async createMessageTemplateAndReset() {
|
||||
this.$refs.createButton.disabled = true;
|
||||
await this.createMessageTemplate(this.newTemplateName);
|
||||
this.newTemplateName = '';
|
||||
this.$refs.createButton.disabled = false;
|
||||
},
|
||||
},
|
||||
mounted() {
|
||||
this.fetchMessageTemplates().then(() => {
|
||||
this.messageTemplatesIntermediate = JSON.parse(JSON.stringify(this.messageTemplates));
|
||||
});
|
||||
this.fetchMessageTemplateVariables();
|
||||
},
|
||||
watch: {
|
||||
messageTemplates() {
|
||||
for (const template of this.messageTemplates) {
|
||||
if (!this.messageTemplatesIntermediate.find(t => t.id === template.id)) {
|
||||
this.messageTemplatesIntermediate.push(JSON.parse(JSON.stringify(template)));
|
||||
}
|
||||
}
|
||||
for (const template of this.messageTemplatesIntermediate) {
|
||||
if (!this.messageTemplates.find(t => t.id === template.id)) {
|
||||
this.messageTemplatesIntermediate = this.messageTemplatesIntermediate.filter(t => t.id !== template.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
pre {
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
color: inherit;
|
||||
}
|
||||
</style>
|
Loading…
Reference in a new issue