Skip to content

Commit

Permalink
Merge pull request #2122 from ResearchHub/feat/add-fundraise-to-note-…
Browse files Browse the repository at this point in the history
…serializer

Add fundraise and document_type to note serializer
  • Loading branch information
yattias authored Feb 25, 2025
2 parents 6a9a452 + 8cbdc63 commit 1a9f470
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 5 deletions.
60 changes: 57 additions & 3 deletions src/note/serializers/note_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ def get_post(self, note):
"name",
]
},
"doc_dps_get_unified_document": {"_include_fields": ["fundraise"]},
"doc_duds_get_fundraise": {
"_include_fields": [
"id",
"status",
"goal_amount",
"goal_currency",
"start_date",
"end_date",
"amount_raised",
"contributors",
]
},
}
serializer = DynamicPostSerializer(
note.post,
Expand All @@ -115,6 +128,8 @@ def get_post(self, note):
"hubs",
"id",
"slug",
"document_type",
"unified_document",
],
)
return serializer.data
Expand Down Expand Up @@ -199,10 +214,49 @@ def get_organization(self, note):
def get_post(self, note):
from researchhub_document.serializers import DynamicPostSerializer

context = self.context
_context_fields = context.get("nte_dns_get_post", {})
if not hasattr(note, "post"):
return None

context = {
"doc_dps_get_authors": {
"_include_fields": [
"id",
"first_name",
"last_name",
"user",
]
},
"doc_dps_get_hubs": {
"_include_fields": [
"id",
"name",
]
},
"doc_dps_get_unified_document": {"_include_fields": ["fundraise"]},
"doc_duds_get_fundraise": {
"_include_fields": [
"id",
"status",
"goal_amount",
"goal_currency",
"start_date",
"end_date",
"amount_raised",
]
},
}
serializer = DynamicPostSerializer(
note.post, context=context, **_context_fields
note.post,
context=context,
_include_fields=[
"authors",
"doi",
"hubs",
"id",
"slug",
"document_type",
"unified_document",
],
)
return serializer.data

Expand Down
101 changes: 101 additions & 0 deletions src/note/tests/test_note_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework.test import APITestCase

from note.models import Note
from purchase.related_models.rsc_exchange_rate_model import RscExchangeRate
from researchhub_access_group.models import Permission
from researchhub_document.models import ResearchhubUnifiedDocument
from user.models import Organization
Expand All @@ -27,6 +28,9 @@ def setUp(self):
response = self.client.post("/api/organization/", {"name": "some org"})
self.org = response.data

# Create exchange rate
RscExchangeRate.objects.create(rate=4.99014625)

def test_user_can_list_created_notes(self):
# Arrange
response = self.client.post(
Expand Down Expand Up @@ -949,3 +953,100 @@ def test_note_content_json_priority_over_src(self):
note = response.data
self.assertEqual(note["latest_version"]["json"], test_json)
self.assertIsNone(note["latest_version"]["src"])

def test_note_without_post(self):
# Create a note without an associated post
response = self.client.post(
"/api/note/",
{
"grouping": "WORKSPACE",
"organization_slug": self.org["slug"],
"title": "Note without post",
},
)
self.assertEqual(response.status_code, 200)
note = response.data

# Verify that post is None
self.assertIsNone(note["post"])

def test_note_with_post(self):
# Create a note first
response = self.client.post(
"/api/note/",
{
"grouping": "WORKSPACE",
"organization_slug": self.org["slug"],
"title": "Note with post",
},
)
self.assertEqual(response.status_code, 200)
note = response.data

# Create a post associated with the note
post_response = self.client.post(
"/api/researchhubpost/",
{
"document_type": "DISCUSSION",
"created_by": self.user.id,
"full_src": "Test post content",
"is_public": True,
"note_id": note["id"],
"renderable_text": "Test post content that is sufficiently long for validation",
"title": "Test post title that is sufficiently long",
"hubs": [],
},
)
self.assertEqual(post_response.status_code, 200)

# Re-fetch the note to verify post data
response = self.client.get(f"/api/note/{note['id']}/")
self.assertEqual(response.status_code, 200)
note = response.data

# Verify post data is present and correctly structured
self.assertIsNotNone(note["post"])
self.assertIn("authors", note["post"])
self.assertIn("hubs", note["post"])
self.assertIn("unified_document", note["post"])

def test_note_with_preregistration_post_fundraise(self):
# Create a note first
response = self.client.post(
"/api/note/",
{
"grouping": "WORKSPACE",
"organization_slug": self.org["slug"],
"title": "Note with preregistration post",
},
)
self.assertEqual(response.status_code, 200)
note = response.data

# Create a preregistration post with fundraise
post_response = self.client.post(
"/api/researchhubpost/",
{
"document_type": "PREREGISTRATION",
"created_by": self.user.id,
"full_src": "Test post content",
"is_public": True,
"note_id": note["id"],
"renderable_text": "Test post content that is sufficiently long for validation",
"title": "Test post title that is sufficiently long",
"hubs": [],
"fundraise_goal_amount": 1000,
},
)
self.assertEqual(post_response.status_code, 200)

# Re-fetch the note to verify post data
response = self.client.get(f"/api/note/{note['id']}/")
self.assertEqual(response.status_code, 200)
note = response.data

# Verify fundraise data is present
self.assertIsNotNone(note["post"]["unified_document"]["fundraise"])
self.assertEqual(
note["post"]["unified_document"]["fundraise"]["goal_amount"]["usd"], 1000.0
)
4 changes: 2 additions & 2 deletions src/researchhub_document/views/researchhub_post_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ def update_existing_researchhub_posts(self, request):

created_by = request.user
created_by_author = created_by.author_profile
hubs = data.pop("hubs", None)
renderable_text = data.pop("renderable_text", "")
hubs = data.get("hubs", None)
renderable_text = data.get("renderable_text", "")
title = data.get("title", "")
assign_doi = data.get("assign_doi", False)
doi = DOI() if assign_doi else None
Expand Down

0 comments on commit 1a9f470

Please sign in to comment.