Skip to content

Commit

Permalink
✨ send events when publishing message draft via api
Browse files Browse the repository at this point in the history
  • Loading branch information
krmax44 committed Dec 16, 2024
1 parent d8fa438 commit b8cf3c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
20 changes: 16 additions & 4 deletions froide/foirequest/api_views/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
from rest_framework.response import Response
from rest_framework.viewsets import mixins

from froide.foirequest.models.message import FoiMessageDraft

from ..auth import (
get_read_foimessage_queryset,
)
from ..models import FoiMessage
from ..models import FoiMessage, FoiMessageDraft, FoiRequest
from ..permissions import (
OnlyEditableWhenDraftPermission,
WriteFoiRequestPermission,
Expand Down Expand Up @@ -71,12 +69,26 @@ def get_queryset(self):
).order_by()
return self.optimize_query(qs)

@action(detail=True, methods=["post"])
@action(detail=True, methods=["get", "post"])
def publish(self, request, pk=None):
message = self.get_object()
message.is_draft = False

if not message.is_response:
message.sender_user = request.user

message.save()

if message.is_response:
FoiRequest.message_received.send(
sender=message.request, message=message, user=request.user
)
else:
FoiRequest.message_sent.send(
sender=message.request, message=message, uer=request.user
)

# return it as a regular message
serializer = FoiMessageSerializer(
message, context=self.get_serializer_context()
)
Expand Down
6 changes: 6 additions & 0 deletions froide/foirequest/tests/test_api_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from froide.foirequest.models.event import FoiEvent
from froide.foirequest.tests import factories


Expand Down Expand Up @@ -89,6 +90,11 @@ def test_message_draft(client: Client, user):
assert response.status_code == 200
assert "/draft/" not in response.json()["resource_uri"]

# ensure event was created
assert FoiEvent.objects.get(
message=response.json()["id"], event_name="message_received", user=user
)

# can't delete anymore
resource_uri = reverse("api:message-detail", kwargs={"pk": message_id})
response = client.delete(resource_uri)
Expand Down

0 comments on commit b8cf3c9

Please sign in to comment.