diff --git a/core/signals.py b/core/signals.py index 7425eb2b..aee7be48 100644 --- a/core/signals.py +++ b/core/signals.py @@ -14,7 +14,7 @@ from social_auth.signals import pre_update from social_auth.backends.facebook import FacebookBackend from tastypie.models import create_api_key -from regluit.payment.signals import transaction_charged +from regluit.payment.signals import transaction_charged, pledge_modified, pledge_created import registration.signals import django.dispatch @@ -130,6 +130,7 @@ def notify_successful_campaign(campaign, **kwargs): # successful_campaign -> send notices successful_campaign.connect(notify_successful_campaign) +from regluit.core.tasks import emit_notifications def handle_transaction_charged(sender,transaction=None, **kwargs): if transaction==None: @@ -139,11 +140,37 @@ def handle_transaction_charged(sender,transaction=None, **kwargs): 'transaction':transaction, 'payment_processor':settings.PAYMENT_PROCESSOR }, True) - from regluit.core.tasks import emit_notifications emit_notifications.delay() transaction_charged.connect(handle_transaction_charged) +def handle_pledge_modified(sender, transaction=None, status=None, **kwargs): + if transaction==None or status==None: + return + notification.queue([transaction.user], "pledge_status_change", { + 'site':Site.objects.get_current(), + 'transaction': transaction, + 'payment_processor':settings.PAYMENT_PROCESSOR, + 'status': status + }, True) + emit_notifications.delay() + +pledge_modified.connect(handle_pledge_modified) + +def handle_you_have_pledged(sender, transaction=None, **kwargs): + if transaction==None: + return + notification.queue([transaction.user], "pledge_you_have_pledged", { + 'site':Site.objects.get_current(), + 'transaction': transaction, + 'campaign': transaction.campaign, + 'work': transaction.campaign.work, + 'payment_processor':settings.PAYMENT_PROCESSOR, + }, True) + emit_notifications.delay() + +pledge_created.connect(handle_you_have_pledged) + # The notification templates need some context; I'm making a note of that here # This can be removed as the relevant functions are written # PLEDGE_CHANGE_STATUS: diff --git a/frontend/views.py b/frontend/views.py index f2aa9ee1..e0e31910 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -760,8 +760,10 @@ class PledgeModifyView(FormView): elif status and url is None: # let's use the pledge_complete template for now and maybe look into customizing it. return HttpResponseRedirect("{0}?tid={1}".format(reverse('pledge_complete'), transaction.id)) + from regluit.payment.signals import pledge_modified + pledge_modified.send(sender=self, transaction=transaction, status="increased") else: - return HttpResponse("No modication made") + return HttpResponse("No modification made") class PledgeCompleteView(TemplateView): @@ -850,10 +852,6 @@ class PledgeCompleteView(TemplateView): context["works2"] = works2 context["site"] = Site.objects.get_current() - # generate notices with same context used for user page - notification.queue([transaction.user], "pledge_you_have_pledged", {'transaction': transaction, 'campaign': campaign, 'site': context['site'], 'work': work}, True) - emit_notifications.delay() - return context diff --git a/payment/manager.py b/payment/manager.py index 53f9c388..9f2bb3cc 100644 --- a/payment/manager.py +++ b/payment/manager.py @@ -5,7 +5,7 @@ from django.core.urlresolvers import reverse from django.conf import settings from regluit.payment.parameters import * from regluit.payment.paypal import IPN_SENDER_STATUS_COMPLETED -from regluit.payment.signals import transaction_charged +from regluit.payment.signals import transaction_charged, pledge_modified, pledge_created if settings.PAYMENT_PROCESSOR == 'paypal': from regluit.payment.paypal import Pay, Finish, Preapproval, ProcessIPN, CancelPreapproval, PaymentDetails, PreapprovalDetails, RefundPayment @@ -549,7 +549,7 @@ class PaymentManager( object ): def authorize(self, currency, target, amount, expiry=None, campaign=None, list=None, user=None, return_url=None, nevermind_url=None, anonymous=False, premium=None, - paymentReason="unglue.it Pledge"): + paymentReason="unglue.it Pledge", modification=False): ''' authorize @@ -619,6 +619,17 @@ class PaymentManager( object ): url = p.next_url() logger.info("Authorize Success: " + url) + + # modification and initial pledge use different notification templates -- + # decide which to send + # we fire notifications here because it's the first point at which we are sure + # that the transaction has successfully completed; triggering notifications + # when the transaction is initiated risks sending notifications on transactions + # that for whatever reason fail. will need other housekeeping to handle those. + if modification==True: + pledge_modified.send(sender=self, transaction=transaction, status="increased") + else: + pledge_created.send(sender=self, transaction=transaction) return t, url @@ -681,16 +692,22 @@ class PaymentManager( object ): nevermind_url, transaction.anonymous, premium, - paymentReason) + paymentReason, + True) if t and url: # Need to re-direct to approve the transaction logger.info("New authorization needed, redirection to url %s" % url) - self.cancel_transaction(transaction) + self.cancel_transaction(transaction) + # while it would seem to make sense to send a pledge notification change here + # if we do, we will also send notifications when we initiate but do not + # successfully complete a pledge modification return True, url else: # a problem in authorize logger.info("Error, unable to start a new authorization") + # should we send a pledge_modified signal with state="failed" and a + # corresponding notification to the user? that would go here. return False, None elif amount <= transaction.max_amount: @@ -701,6 +718,7 @@ class PaymentManager( object ): transaction.save() logger.info("Updated amount of transaction to %f" % amount) + pledge_modified.send(sender=self, transaction=transaction, status="decreased") return True, None else: # this shouldn't happen diff --git a/payment/signals.py b/payment/signals.py index 35b6cb64..6487f51f 100644 --- a/payment/signals.py +++ b/payment/signals.py @@ -1,3 +1,5 @@ from django.dispatch import Signal transaction_charged = Signal(providing_args=["transaction"]) +pledge_created = Signal(providing_args=["transaction"]) +pledge_modified = Signal(providing_args=["transaction", "status"]) \ No newline at end of file