Skip to content

Commit

Permalink
set fundraise status to CANCEL* on post creation + clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktytarenko committed Jan 29, 2025
1 parent 08f2c3a commit 714f812
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 24 deletions.
11 changes: 4 additions & 7 deletions src/purchase/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import decimal
import time

from django.contrib.contenttypes.models import ContentType
from rest_framework.response import Response

from purchase.models import Fundraise
from purchase.related_models.constants.currency import USD
from reputation.distributions import create_purchase_distribution
from reputation.distributor import Distributor
Expand Down Expand Up @@ -40,19 +42,14 @@ def store_leftover_paper_support(paper, purchase, leftover_amount):


def create_fundraise_with_escrow(
user, unified_document, goal_amount, goal_currency=USD
user, unified_document, goal_amount, goal_currency=USD, status=Fundraise.OPEN
):
"""
Helper function to create a fundraise with its associated escrow.
Returns (fundraise, error_response) tuple where error_response is None if successful
Note: This function should be called within a transaction.atomic() block
"""
from django.contrib.contenttypes.models import ContentType

from purchase.models import Fundraise
from reputation.models import Escrow

# Validate inputs
if not unified_document.document_type == PREREGISTRATION:
return None, Response(
Expand Down Expand Up @@ -85,7 +82,7 @@ def create_fundraise_with_escrow(
unified_document=unified_document,
goal_amount=goal_amount,
goal_currency=goal_currency,
status=Fundraise.OPEN,
status=status,
)
# Create escrow object
escrow = Escrow.objects.create(
Expand Down
18 changes: 1 addition & 17 deletions src/purchase/views/fundraise_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,7 @@ def create(self, request, *args, **kwargs):
return Response({"message": "User does not exist"}, status=400)

with transaction.atomic():
# Create fundraise object
# fundraise = Fundraise.objects.create(
# created_by=recipient_user,
# unified_document=unified_document,
# goal_amount=goal_amount,
# goal_currency=goal_currency,
# status=Fundraise.OPEN,
# )
# # Create escrow object
# escrow = Escrow.objects.create(
# created_by=recipient_user,
# hold_type=Escrow.FUNDRAISE,
# content_type=ContentType.objects.get_for_model(Fundraise),
# object_id=fundraise.id,
# )
# fundraise.escrow = escrow
# fundraise.save()
# Create a fundraise with its associated escrow
fundraise, error_response = create_fundraise_with_escrow(
user=recipient_user,
unified_document=unified_document,
Expand Down
2 changes: 2 additions & 0 deletions src/researchhub_document/views/researchhub_post_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from note.related_models.note_model import Note
from purchase.models import Balance, Purchase
from purchase.related_models.constants.currency import USD
from purchase.related_models.fundraise_model import Fundraise
from purchase.serializers.fundraise_serializer import DynamicFundraiseSerializer
from purchase.utils import create_fundraise_with_escrow
from researchhub.settings import CROSSREF_DOI_RSC_FEE, TESTING
Expand Down Expand Up @@ -178,6 +179,7 @@ def create_researchhub_post(self, request):
unified_document=unified_document,
goal_amount=fundraise_data.get("goal_amount"),
goal_currency=fundraise_data.get("goal_currency", USD),
status=Fundraise.CLOSED, # TODO we should probably cerate a new status for fundraise (INACTIVE)
)
if error_response:
return error_response

Check warning on line 185 in src/researchhub_document/views/researchhub_post_views.py

View check run for this annotation

Codecov / codecov/patch

src/researchhub_document/views/researchhub_post_views.py#L184-L185

Added lines #L184 - L185 were not covered by tests
Expand Down

0 comments on commit 714f812

Please sign in to comment.