Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caching on client id for purchases #1551

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions src/purchase/tests/test_send_rsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_list_purchases(self):
)

response = self._post_support_response(
purchaser, post.id, "researchhubpost", tip_amount
purchaser, post.id, "client_id_1", "researchhubpost", tip_amount
)
self.assertContains(response, "id", status_code=201)

Expand All @@ -68,7 +68,7 @@ def test_list_purchases_cannot_list_other_users_purchases(self):
)

response = self._post_support_response(
purchaser, post.id, "researchhubpost", tip_amount
purchaser, post.id, "client_id_2", "researchhubpost", tip_amount
)
self.assertContains(response, "id", status_code=201)

Expand Down Expand Up @@ -125,7 +125,9 @@ def test_support_paper_distribution(self):
amount="10000", user=user, content_type=DISTRIBUTION_CONTENT_TYPE
)

response = self._post_support_response(user, paper.id, "paper", amount)
response = self._post_support_response(
user, paper.id, "client_id_3", "paper", amount
)
self.assertContains(response, "id", status_code=201)
self.assertTrue(Escrow.objects.filter(hold_type=Escrow.AUTHOR_RSC).count() == 1)
author_pot = Escrow.objects.filter(hold_type=Escrow.AUTHOR_RSC).first()
Expand All @@ -146,7 +148,7 @@ def test_support_post_distribution(self):
)

response = self._post_support_response(
user, post.id, "researchhubpost", tip_amount
user, post.id, "client_id_4", "researchhubpost", tip_amount
)
self.assertContains(response, "id", status_code=201)
purchase_id = response.data["id"]
Expand Down Expand Up @@ -193,7 +195,7 @@ def test_support_comment_distribution(self):
)

response = self._post_support_response(
user, comment.id, "rhcommentmodel", tip_amount
user, comment.id, "client_id_5", "rhcommentmodel", tip_amount
)
self.assertContains(response, "id", status_code=201)
purchase_id = response.data["id"]
Expand Down Expand Up @@ -224,7 +226,36 @@ def test_support_comment_distribution(self):
)
self.assertEqual(poster_balance_amount, float(tip_amount))

def _post_support_response(self, user, object_id, content_type, amount=10):
def test_repeat_request_support_comment_distribution(self):
user = create_random_authenticated_user("rep_user")
poster = create_random_authenticated_user("rep_user")
post = create_post(created_by=poster)
comment = create_rh_comment(created_by=poster, post=post)
client_id = "client_id_6"

tip_amount = 100

# give the user 10,000 RSC
DISTRIBUTION_CONTENT_TYPE = ContentType.objects.get(model="distribution")
Balance.objects.create(
amount="10000", user=user, content_type=DISTRIBUTION_CONTENT_TYPE
)

response = self._post_support_response(
user, comment.id, client_id, "rhcommentmodel", tip_amount
)
self.assertContains(response, "id", status_code=201)

# make a second request with the same client_id which should fail.
response_repeat_call = self._post_support_response(
user, comment.id, client_id, "rhcommentmodel", tip_amount
)
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
):
url = "/api/purchase/"
return get_authenticated_post_response(
user,
Expand All @@ -235,5 +266,6 @@ def _post_support_response(self, user, object_id, content_type, amount=10):
"object_id": object_id,
"purchase_method": "OFF_CHAIN",
"purchase_type": "BOOST",
"client_id": client_id,
},
)
14 changes: 12 additions & 2 deletions src/purchase/views/purchase_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def create(self, request):
data = request.data

amount = data["amount"]
client_id = data["client_id"]
purchase_method = data["purchase_method"]
purchase_type = data["purchase_type"]
content_type_str = data["content_type"]
Expand All @@ -77,6 +78,13 @@ def create(self, request):
with transaction.atomic():
user = User.objects.select_for_update().get(id=user.id)

cached_serialized_data = cache.get(f"purchase_client_id_{client_id}")
if cached_serialized_data:
return Response(
cached_serialized_data,
status=201,
)

purchase_data = {
"amount": amount,
"user": user.id,
Expand Down Expand Up @@ -204,8 +212,10 @@ def create(self, request):
)
distributor.distribute()

serializer = self.serializer_class(purchase, context=context)
serializer_data = serializer.data
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(
Expand Down