Skip to content

Commit b7e55b6

Browse files
committed
Remove dedicated activity feed serializer
1 parent e44b24c commit b7e55b6

File tree

3 files changed

+3
-287
lines changed

3 files changed

+3
-287
lines changed

src/feed/serializers.py

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -966,61 +966,6 @@ def _get_grant_image(grant):
966966
return None
967967

968968

969-
class ActivityFeedEntrySerializer(FeedEntrySerializer):
970-
"""
971-
Serializer for activity feed entries that includes fundraise contributions.
972-
"""
973-
974-
contributions = serializers.SerializerMethodField()
975-
976-
class Meta:
977-
model = FeedEntry
978-
fields = FeedEntrySerializer.Meta.fields + [
979-
"contributions",
980-
]
981-
982-
def get_contributions(self, obj):
983-
"""
984-
Return fundraise contributors for entries whose unified document has a
985-
fundraise.
986-
"""
987-
if not obj.unified_document:
988-
return None
989-
990-
fundraises = getattr(obj.unified_document, "prefetched_fundraises", None)
991-
if fundraises is None:
992-
fundraises = list(obj.unified_document.fundraises.all())
993-
994-
if not fundraises:
995-
return None
996-
997-
fundraise = fundraises[0]
998-
aggregated = fundraise.get_contributors_summary()
999-
1000-
result = []
1001-
for entry in aggregated.top:
1002-
serializer = SimpleUserSerializer(entry.user)
1003-
user_result = serializer.data
1004-
user_result["total_contribution"] = {
1005-
"rsc": entry.total_rsc,
1006-
"usd": entry.total_usd,
1007-
}
1008-
user_result["contributions"] = [
1009-
{
1010-
"amount": contribution.amount,
1011-
"currency": contribution.currency,
1012-
"date": contribution.date,
1013-
}
1014-
for contribution in entry.contributions
1015-
]
1016-
result.append(user_result)
1017-
1018-
return {
1019-
"total": aggregated.total,
1020-
"top": result,
1021-
}
1022-
1023-
1024969
class GrantFeedEntrySerializer(FeedEntrySerializer):
1025970
"""Serializer for grant feed entries"""
1026971

src/feed/tests/test_serializers.py

Lines changed: 1 addition & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from feed.models import FeedEntry
1010
from feed.serializers import (
11-
ActivityFeedEntrySerializer,
11+
1212
CommentSerializer,
1313
ContentObjectSerializer,
1414
FeedEntrySerializer,
@@ -2447,204 +2447,6 @@ def test_fundraise_without_application_has_no_associated_grants(
24472447
self.assertEqual(serializer.data["associated_grants"], [])
24482448

24492449

2450-
class ActivityFeedEntrySerializerTests(AWSMockTestCase):
2451-
"""
2452-
Test cases for the ActivityFeedEntrySerializer.
2453-
"""
2454-
2455-
def setUp(self):
2456-
super().setUp()
2457-
self.user = create_random_default_user("activity_feed_user")
2458-
2459-
def _create_feed_entry(self, unified_doc, post):
2460-
return FeedEntry.objects.create(
2461-
content_type=ContentType.objects.get_for_model(ResearchhubPost),
2462-
object_id=post.id,
2463-
user=self.user,
2464-
action="PUBLISH",
2465-
action_date=post.created_date,
2466-
unified_document=unified_doc,
2467-
)
2468-
2469-
def _create_fundraise_post(self):
2470-
unified_doc = ResearchhubUnifiedDocument.objects.create(
2471-
document_type=document_type.PREREGISTRATION,
2472-
)
2473-
post = ResearchhubPost.objects.create(
2474-
title="Prereg Post",
2475-
created_by=self.user,
2476-
document_type=document_type.PREREGISTRATION,
2477-
renderable_text="A preregistration post",
2478-
unified_document=unified_doc,
2479-
)
2480-
return unified_doc, post
2481-
2482-
def _create_purchase(self, fundraise, user, amount):
2483-
ct = ContentType.objects.get_for_model(Fundraise)
2484-
return Purchase.objects.create(
2485-
user=user,
2486-
content_type=ct,
2487-
object_id=fundraise.id,
2488-
purchase_type=Purchase.FUNDRAISE_CONTRIBUTION,
2489-
purchase_method=Purchase.OFF_CHAIN,
2490-
amount=str(amount),
2491-
)
2492-
2493-
def _create_usd_contribution(self, fundraise, user, amount_cents):
2494-
return UsdFundraiseContribution.objects.create(
2495-
user=user,
2496-
fundraise=fundraise,
2497-
amount_cents=amount_cents,
2498-
fee_cents=int(amount_cents * 0.09),
2499-
origin_fund_id="test-origin",
2500-
destination_org_id="test-destination",
2501-
)
2502-
2503-
def test_contributions_none_without_fundraise(self):
2504-
"""Feed entry on a regular post should have contributions=None."""
2505-
# Arrange
2506-
unified_doc = ResearchhubUnifiedDocument.objects.create(
2507-
document_type=document_type.DISCUSSION,
2508-
)
2509-
post = ResearchhubPost.objects.create(
2510-
title="Discussion Post",
2511-
created_by=self.user,
2512-
document_type=document_type.DISCUSSION,
2513-
renderable_text="Just a discussion",
2514-
unified_document=unified_doc,
2515-
)
2516-
feed_entry = self._create_feed_entry(unified_doc, post)
2517-
2518-
# Act
2519-
serializer = ActivityFeedEntrySerializer(feed_entry)
2520-
2521-
# Assert
2522-
self.assertIsNone(serializer.data["contributions"])
2523-
2524-
@patch(
2525-
"purchase.related_models.rsc_exchange_rate_model.RscExchangeRate.get_latest_exchange_rate"
2526-
)
2527-
def test_contributions_empty_with_fundraise_no_purchases(self, mock_usd_to_rsc):
2528-
"""Fundraise with no purchases should return total=0, top=[]."""
2529-
# Arrange
2530-
mock_usd_to_rsc.return_value = 1.0
2531-
2532-
unified_doc, post = self._create_fundraise_post()
2533-
Fundraise.objects.create(
2534-
unified_document=unified_doc,
2535-
created_by=self.user,
2536-
goal_amount=Decimal("100.00"),
2537-
goal_currency=USD,
2538-
status=Fundraise.OPEN,
2539-
)
2540-
feed_entry = self._create_feed_entry(unified_doc, post)
2541-
2542-
# Act
2543-
serializer = ActivityFeedEntrySerializer(feed_entry)
2544-
contributions = serializer.data["contributions"]
2545-
2546-
# Assert
2547-
self.assertEqual(contributions["total"], 0)
2548-
self.assertEqual(contributions["top"], [])
2549-
2550-
@patch(
2551-
"purchase.related_models.rsc_exchange_rate_model.RscExchangeRate.get_latest_exchange_rate"
2552-
)
2553-
def test_contributions_single_contributor(self, mock_usd_to_rsc):
2554-
"""Single purchase should produce one contributor entry."""
2555-
# Arrange
2556-
mock_usd_to_rsc.return_value = 1.0
2557-
2558-
unified_doc, post = self._create_fundraise_post()
2559-
fundraise = Fundraise.objects.create(
2560-
unified_document=unified_doc,
2561-
created_by=self.user,
2562-
goal_amount=Decimal("500.00"),
2563-
goal_currency=USD,
2564-
status=Fundraise.OPEN,
2565-
)
2566-
self._create_purchase(fundraise, self.user, 50.0)
2567-
feed_entry = self._create_feed_entry(unified_doc, post)
2568-
2569-
# Act
2570-
serializer = ActivityFeedEntrySerializer(feed_entry)
2571-
contributions = serializer.data["contributions"]
2572-
2573-
# Assert
2574-
self.assertEqual(contributions["total"], 1)
2575-
self.assertEqual(len(contributions["top"]), 1)
2576-
top_entry = contributions["top"][0]
2577-
self.assertEqual(top_entry["total_contribution"]["rsc"], 50.0)
2578-
self.assertEqual(top_entry["total_contribution"]["usd"], 0)
2579-
self.assertEqual(len(top_entry["contributions"]), 1)
2580-
self.assertEqual(top_entry["contributions"][0]["amount"], 50.0)
2581-
self.assertEqual(top_entry["contributions"][0]["currency"], "RSC")
2582-
2583-
@patch(
2584-
"purchase.related_models.rsc_exchange_rate_model.RscExchangeRate.get_latest_exchange_rate"
2585-
)
2586-
def test_contributions_multiple_purchases_aggregated(self, mock_usd_to_rsc):
2587-
"""Multiple purchases by the same user should be aggregated."""
2588-
# Arrange
2589-
mock_usd_to_rsc.return_value = 1.0
2590-
2591-
unified_doc, post = self._create_fundraise_post()
2592-
fundraise = Fundraise.objects.create(
2593-
unified_document=unified_doc,
2594-
created_by=self.user,
2595-
goal_amount=Decimal("1000.00"),
2596-
goal_currency=USD,
2597-
status=Fundraise.OPEN,
2598-
)
2599-
self._create_purchase(fundraise, self.user, 30.0)
2600-
self._create_purchase(fundraise, self.user, 70.0)
2601-
feed_entry = self._create_feed_entry(unified_doc, post)
2602-
2603-
# Act
2604-
serializer = ActivityFeedEntrySerializer(feed_entry)
2605-
contributions = serializer.data["contributions"]
2606-
2607-
# Assert
2608-
self.assertEqual(contributions["total"], 1)
2609-
top_entry = contributions["top"][0]
2610-
self.assertEqual(top_entry["total_contribution"]["rsc"], 100.0)
2611-
self.assertEqual(top_entry["total_contribution"]["usd"], 0)
2612-
self.assertEqual(len(top_entry["contributions"]), 2)
2613-
2614-
@patch(
2615-
"purchase.related_models.rsc_exchange_rate_model.RscExchangeRate.get_latest_exchange_rate"
2616-
)
2617-
def test_contributions_include_usd_contributions(self, mock_usd_to_rsc):
2618-
"""RSC and USD contributions from the same user should both appear."""
2619-
# Arrange
2620-
mock_usd_to_rsc.return_value = 1.0
2621-
2622-
unified_doc, post = self._create_fundraise_post()
2623-
fundraise = Fundraise.objects.create(
2624-
unified_document=unified_doc,
2625-
created_by=self.user,
2626-
goal_amount=Decimal("500.00"),
2627-
goal_currency=USD,
2628-
status=Fundraise.OPEN,
2629-
)
2630-
contributor = create_random_default_user("mixed_contributor")
2631-
self._create_purchase(fundraise, contributor, 10.0)
2632-
self._create_usd_contribution(fundraise, contributor, 2500)
2633-
feed_entry = self._create_feed_entry(unified_doc, post)
2634-
2635-
# Act
2636-
serializer = ActivityFeedEntrySerializer(feed_entry)
2637-
contributions = serializer.data["contributions"]
2638-
top_entry = contributions["top"][0]
2639-
2640-
# Assert
2641-
self.assertEqual(top_entry["total_contribution"]["rsc"], 10.0)
2642-
self.assertEqual(top_entry["total_contribution"]["usd"], 25.0)
2643-
self.assertEqual(len(top_entry["contributions"]), 2)
2644-
currencies = {c["currency"] for c in top_entry["contributions"]}
2645-
self.assertEqual(currencies, {"RSC", "USD"})
2646-
2647-
26482450
class FundraiseContributionContentSerializerTests(AWSMockTestCase):
26492451
"""
26502452
Test cases for the FundraiseContributionContentSerializer.

src/feed/views/activity_feed_view.py

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
from django.contrib.contenttypes.models import ContentType
2-
from django.db.models import Prefetch
32
from rest_framework.viewsets import ModelViewSet
43

54
from feed.models import FeedEntry
6-
from feed.serializers import ActivityFeedEntrySerializer
5+
from feed.serializers import FeedEntrySerializer
76
from feed.views.common import FeedPagination
87
from feed.views.feed_view_mixin import FeedViewMixin
9-
from purchase.models import Fundraise, UsdFundraiseContribution
108
from purchase.related_models.grant_application_model import GrantApplication
119
from purchase.related_models.grant_model import Grant
12-
from purchase.related_models.purchase_model import Purchase
1310
from researchhub_comment.constants.rh_comment_thread_types import PEER_REVIEW
1411
from researchhub_comment.related_models.rh_comment_model import RhCommentModel
1512

@@ -31,7 +28,7 @@ class ActivityFeedViewSet(FeedViewMixin, ModelViewSet):
3128
returns only comments across all grant-related documents.
3229
"""
3330

34-
serializer_class = ActivityFeedEntrySerializer
31+
serializer_class = FeedEntrySerializer
3532
permission_classes = []
3633
pagination_class = FeedPagination
3734
http_method_names = ["get", "head", "options"]
@@ -58,34 +55,6 @@ def get_queryset(self):
5855
"user__author_profile",
5956
"user__userverification",
6057
)
61-
.prefetch_related(
62-
Prefetch(
63-
"unified_document__fundraises",
64-
queryset=Fundraise.objects.prefetch_related(
65-
Prefetch(
66-
"purchases",
67-
queryset=Purchase.objects.select_related(
68-
"user",
69-
"user__author_profile",
70-
"user__userverification",
71-
).order_by("-created_date"),
72-
to_attr="prefetched_purchases",
73-
),
74-
Prefetch(
75-
"usd_contributions",
76-
queryset=UsdFundraiseContribution.objects.select_related(
77-
"user",
78-
"user__author_profile",
79-
"user__userverification",
80-
)
81-
.filter(is_refunded=False)
82-
.order_by("-created_date"),
83-
to_attr="prefetched_usd_contributions",
84-
),
85-
),
86-
to_attr="prefetched_fundraises",
87-
),
88-
)
8958
.order_by("-action_date")
9059
)
9160

0 commit comments

Comments
 (0)