From 0112cfc64e9b8c55a65d79e5b12d7a9a43a899e1 Mon Sep 17 00:00:00 2001 From: koutst Date: Tue, 30 Apr 2024 14:43:43 +0000 Subject: [PATCH] Cache response, return success on repeat request --- src/purchase/tests/test_send_rsc.py | 5 +++-- src/purchase/views/purchase_view.py | 14 ++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/purchase/tests/test_send_rsc.py b/src/purchase/tests/test_send_rsc.py index e9d73e280..c510f7d82 100644 --- a/src/purchase/tests/test_send_rsc.py +++ b/src/purchase/tests/test_send_rsc.py @@ -247,10 +247,11 @@ def test_repeat_request_support_comment_distribution(self): self.assertContains(response, "id", status_code=201) # make a second request with the same client_id which should fail. - response = self._post_support_response( + response_repeat_call = self._post_support_response( user, comment.id, client_id, "rhcommentmodel", tip_amount ) - self.assertEqual(response.status_code, 400) + self.assertEqual(response.data, response_repeat_call.data) + self.assertEqual(response_repeat_call.status_code, 201) def _post_support_response( self, user, object_id, client_id, content_type, amount=10 diff --git a/src/purchase/views/purchase_view.py b/src/purchase/views/purchase_view.py index c79879f06..d5aee293d 100644 --- a/src/purchase/views/purchase_view.py +++ b/src/purchase/views/purchase_view.py @@ -78,17 +78,13 @@ def create(self, request): with transaction.atomic(): user = User.objects.select_for_update().get(id=user.id) - cached_client_id = cache.get(f"purchase_client_id_{client_id}") - if cached_client_id: + cached_serialized_data = cache.get(f"purchase_client_id_{client_id}") + if cached_serialized_data: return Response( - { - "detail": "This purchase has already been processed", - }, - status=400, + cached_serialized_data, + status=201, ) - cache.set(f"purchase_client_id_{client_id}", True, timeout=60) - purchase_data = { "amount": amount, "user": user.id, @@ -219,6 +215,8 @@ def create(self, request): serializer = self.serializer_class(purchase, context=context) serializer_data = serializer.data + cache.set(f"purchase_client_id_{client_id}", serializer_data, timeout=3600) + if recipient and user: self.send_purchase_notification( purchase, unified_doc, recipient, notification_type