stash
This commit is contained in:
parent
29e7c4d283
commit
55577adde8
5 changed files with 104 additions and 7 deletions
|
@ -1,6 +1,15 @@
|
|||
from rest_framework import routers, viewsets, serializers
|
||||
import logging
|
||||
|
||||
from tickets.models import IssueThread, Comment, StateChange
|
||||
from django.urls import path
|
||||
from rest_framework.decorators import api_view, permission_classes, authentication_classes
|
||||
from rest_framework import routers, viewsets, serializers, status
|
||||
from rest_framework.response import Response
|
||||
from asgiref.sync import async_to_sync
|
||||
|
||||
from core.settings import MAIL_DOMAIN
|
||||
from mail.models import Email
|
||||
from mail.protocol import send_smtp, make_reply, collect_references
|
||||
from tickets.models import IssueThread
|
||||
|
||||
|
||||
class IssueSerializer(serializers.ModelSerializer):
|
||||
|
@ -48,7 +57,32 @@ class IssueViewSet(viewsets.ModelViewSet):
|
|||
authentication_classes = []
|
||||
|
||||
|
||||
@api_view(['POST'])
|
||||
@permission_classes([])
|
||||
@authentication_classes([])
|
||||
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)
|
||||
|
||||
|
||||
router = routers.SimpleRouter()
|
||||
router.register(r'tickets', IssueViewSet, basename='issues')
|
||||
|
||||
urlpatterns = router.urls
|
||||
urlpatterns = router.urls + [
|
||||
path('tickets/<int:pk>/reply/', reply, name='reply'),
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue