c3lf-system-3/core/tickets/api_v2.py

126 lines
4.7 KiB
Python
Raw Normal View History

2023-12-06 23:58:04 +00:00
import logging
2023-11-23 22:17:20 +00:00
2023-12-20 16:13:07 +00:00
from django.urls import re_path
2023-12-13 07:09:29 +00:00
from django.contrib.auth.decorators import permission_required
2023-12-06 23:58:04 +00:00
from rest_framework import routers, viewsets, serializers, status
2023-12-13 07:09:29 +00:00
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
2023-12-06 23:58:04 +00:00
from rest_framework.response import Response
from asgiref.sync import async_to_sync
2023-12-18 00:06:16 +00:00
from channels.layers import get_channel_layer
2023-12-06 23:58:04 +00:00
from core.settings import MAIL_DOMAIN
from mail.models import Email
from mail.protocol import send_smtp, make_reply, collect_references
2023-12-18 00:06:16 +00:00
from notify_sessions.models import SystemEvent
2023-12-06 23:58:04 +00:00
from tickets.models import IssueThread
2023-11-23 22:17:20 +00:00
class IssueSerializer(serializers.ModelSerializer):
2023-12-06 03:34:22 +00:00
timeline = serializers.SerializerMethodField()
2023-11-23 22:17:20 +00:00
class Meta:
model = IssueThread
2023-12-06 03:51:47 +00:00
fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity')
read_only_fields = ('id', 'timeline', 'last_activity')
2023-12-06 03:34:22 +00:00
@staticmethod
def get_timeline(obj):
timeline = []
for comment in obj.comments.all():
timeline.append({
'type': 'comment',
'id': comment.id,
'timestamp': comment.timestamp,
'comment': comment.comment,
})
for state_change in obj.state_changes.all():
timeline.append({
'type': 'state',
'id': state_change.id,
'timestamp': state_change.timestamp,
'state': state_change.state,
})
for email in obj.emails.all():
timeline.append({
'type': 'mail',
'id': email.id,
'timestamp': email.timestamp,
'sender': email.sender,
'recipient': email.recipient,
'subject': email.subject,
'body': email.body,
})
return sorted(timeline, key=lambda x: x['timestamp'])
2023-11-23 22:17:20 +00:00
class IssueViewSet(viewsets.ModelViewSet):
serializer_class = IssueSerializer
queryset = IssueThread.objects.all()
2023-12-06 23:58:04 +00:00
@api_view(['POST'])
2023-12-13 07:09:29 +00:00
@permission_classes([IsAuthenticated])
@permission_required('tickets.add_issuethread', raise_exception=True)
2023-12-06 23:58:04 +00:00
def reply(request, pk):
issue = IssueThread.objects.get(pk=pk)
# email = issue.reply(request.data['body']) # TODO evaluate if this is a useful abstraction
references = collect_references(issue)
most_recent = Email.objects.filter(issue_thread=issue, recipient__endswith='@' + MAIL_DOMAIN).order_by(
'-timestamp').first()
mail = Email.objects.create(
issue_thread=issue,
sender=most_recent.recipient,
recipient=most_recent.sender,
subject=f'Re: {most_recent.subject}',
body=request.data['message'],
in_reply_to=most_recent.reference,
)
log = logging.getLogger('mail.log')
async_to_sync(send_smtp)(make_reply(mail, references), log)
return Response({'status': 'ok'}, status=status.HTTP_201_CREATED)
2023-12-18 00:06:16 +00:00
@api_view(['POST'])
@permission_classes([IsAuthenticated])
@permission_required('tickets.add_issuethread_manual', raise_exception=True)
def manual_ticket(request):
2023-12-20 16:13:07 +00:00
if 'name' not in request.data:
return Response({'status': 'error', 'message': 'missing name'}, status=status.HTTP_400_BAD_REQUEST)
if 'sender' not in request.data:
return Response({'status': 'error', 'message': 'missing sender'}, status=status.HTTP_400_BAD_REQUEST)
if 'recipient' not in request.data:
return Response({'status': 'error', 'message': 'missing recipient'}, status=status.HTTP_400_BAD_REQUEST)
if 'body' not in request.data:
return Response({'status': 'error', 'message': 'missing body'}, status=status.HTTP_400_BAD_REQUEST)
2023-12-18 00:06:16 +00:00
issue = IssueThread.objects.create(
name=request.data['name'],
manually_created=True,
)
email = Email.objects.create(
issue_thread=issue,
sender=request.data['sender'],
recipient=request.data['recipient'],
subject=request.data['name'],
body=request.data['body'],
)
systemevent = SystemEvent.objects.create(type='email received', reference=email.id)
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)(
'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id,
"message": "email received"}
)
2023-12-20 16:13:07 +00:00
return Response(IssueSerializer(issue).data, status=status.HTTP_201_CREATED)
2023-12-18 00:06:16 +00:00
2023-11-23 22:17:20 +00:00
router = routers.SimpleRouter()
router.register(r'tickets', IssueViewSet, basename='issues')
2023-12-20 16:13:07 +00:00
urlpatterns = ([
re_path(r'^tickets/(?P<pk>\d+)/reply/$', reply, name='reply'),
re_path(r'^tickets/manual/$', manual_ticket, name='manual_ticket'),
] + router.urls)