changed PaymentManager.cancel to PaymentManager.cancel_transaction and added PaymentManager.cancel_campaign to parallel other methods in PaymentManager

morphing the campaign_admin view towards being able to push all the campaigns through the various states
pull/1/head
Raymond Yee 2011-12-21 17:32:08 -05:00
parent 1872615f2a
commit 17d4acceab
4 changed files with 101 additions and 24 deletions

View File

@ -12,6 +12,10 @@
<h2>Campaign Admin</h2>
{% if command_status %}
<p style="font-weight:bold; color:red">{{command_status}}</p>
{% endif %}
<form method="post" action="#">
{% csrf_token %}
{{ form.as_p }}
@ -36,6 +40,14 @@
<li>{{campaign.id}} | {{campaign.name}} </li>
{% endfor %}
</ul>
<form method="post" action="#">
{% csrf_token %}
<input type="submit" name="execute_campaigns" value="Execute Payment to Gluejar" id="submit">
</form>
<form method="post" action="#">
{% csrf_token %}
<input type="submit" name="cancel_campaigns" value="Cancel Active Transactions" id="submit">
</form>
{% else %}
<p>No campaigns with active transactions</p>
{% endif %}
@ -48,6 +60,10 @@
<li>{{campaign.id}} | {{campaign.name}} </li>
{% endfor %}
</ul>
<form method="post" action="#">
{% csrf_token %}
<input type="submit" name="finish_campaigns" value="Pay Rights Holders" id="submit">
</form>
{% else %}
<p>No campaigns with incomplete transactions</p>
{% endif %}
@ -65,4 +81,16 @@
{% endif %}
<!-- canceled transactions -->
<p style="font-weight:bold">Campaigns with canceled transactions</p>
{% if campaigns_with_canceled_transactions %}
<ul>
{% for campaign in campaigns_with_canceled_transactions %}
<li>{{campaign.id}} | {{campaign.name}} </li>
{% endfor %}
</ul>
{% else %}
<p>No campaigns with canceled transactions</p>
{% endif %}
{% endblock %}

View File

@ -39,7 +39,7 @@ from regluit.frontend.forms import RightsHolderForm, UserClaimForm, LibraryThin
from regluit.frontend.forms import ManageCampaignForm, DonateForm, CampaignAdminForm
from regluit.payment.manager import PaymentManager
from regluit.payment.parameters import TARGET_TYPE_CAMPAIGN, TARGET_TYPE_DONATION
from regluit.payment.paypal import Preapproval, IPN_PAY_STATUS_ACTIVE, IPN_PAY_STATUS_INCOMPLETE, IPN_PAY_STATUS_COMPLETED
from regluit.payment.paypal import Preapproval, IPN_PAY_STATUS_ACTIVE, IPN_PAY_STATUS_INCOMPLETE, IPN_PAY_STATUS_COMPLETED, IPN_PAY_STATUS_CANCELED
from regluit.core import goodreads
from tastypie.models import ApiKey
from regluit.payment.models import Transaction
@ -425,39 +425,70 @@ def campaign_admin(request):
# first task: run PaymentManager.checkStatus() to update Campaign statuses
# does it return data to display?
def campaigns_types():
# pull out Campaigns with Transactions that are ACTIVE -- and hence can be executed
# Campaign.objects.filter(transaction__status='ACTIVE')
campaigns_with_active_transactions = models.Campaign.objects.filter(transaction__status=IPN_PAY_STATUS_ACTIVE)
# pull out Campaigns with Transactions that are INCOMPLETE
campaigns_with_incomplete_transactions = models.Campaign.objects.filter(transaction__status=IPN_PAY_STATUS_INCOMPLETE)
# show all Campaigns with Transactions that are COMPLETED
campaigns_with_completed_transactions = models.Campaign.objects.filter(transaction__status=IPN_PAY_STATUS_COMPLETED)
# show Campaigns with Transactions that are CANCELED
campaigns_with_canceled_transactions = models.Campaign.objects.filter(transaction__status=IPN_PAY_STATUS_CANCELED)
return (campaigns_with_active_transactions, campaigns_with_incomplete_transactions, campaigns_with_completed_transactions,
campaigns_with_canceled_transactions)
form = CampaignAdminForm()
pm = PaymentManager()
check_status_results = None
command_status = None
if request.method == 'GET':
check_status_results = None
pass
elif request.method == 'POST':
try:
pm = PaymentManager()
check_status_results = pm.checkStatus()
except Exception, e:
check_status_results = e
if 'campaign_checkstatus' in request.POST.keys():
# campaign_checkstatus
try:
check_status_results = pm.checkStatus()
command_status = _("PaymentDetails and PreapprovalDetails have been applied")
except Exception, e:
check_status_results = e
elif 'execute_campaigns' in request.POST.keys():
campaigns_with_active_transactions = models.Campaign.objects.filter(transaction__status=IPN_PAY_STATUS_ACTIVE)
results = [pm.execute_campaign(c) for c in campaigns_with_active_transactions]
command_status = str(results)
elif 'finish_campaigns' in request.POST.keys():
campaigns_with_incomplete_transactions = models.Campaign.objects.filter(transaction__status=IPN_PAY_STATUS_INCOMPLETE)
results = [pm.finish_campaign(c) for c in campaigns_with_incomplete_transactions]
command_status = str(results)
pass
elif 'cancel_campaigns' in request.POST.keys():
campaigns_with_active_transactions = models.Campaign.objects.filter(transaction__status=IPN_PAY_STATUS_ACTIVE)
results = [pm.cancel_campaign(c) for c in campaigns_with_active_transactions]
command_status = str(results)
# second task: pull out Campaigns with Transactions that are ACTIVE -- and hence can be executed
# Campaign.objects.filter(transaction__status='ACTIVE')
campaigns_with_active_transactions = models.Campaign.objects.filter(transaction__status=IPN_PAY_STATUS_ACTIVE)
# third task: pull out Campaigns with Transactions that are INCOMPLETE
campaigns_with_incomplete_transactions = models.Campaign.objects.filter(transaction__status=IPN_PAY_STATUS_INCOMPLETE)
# 4th task: show all Campaigns with Transactions that are COMPLETED
campaigns_with_completed_transactions = models.Campaign.objects.filter(transaction__status=IPN_PAY_STATUS_COMPLETED)
(campaigns_with_active_transactions, campaigns_with_incomplete_transactions, campaigns_with_completed_transactions,
campaigns_with_canceled_transactions) = campaigns_types()
context.update({
'form': form,
'check_status_results':check_status_results,
'campaigns_with_active_transactions': campaigns_with_active_transactions,
'campaigns_with_incomplete_transactions': campaigns_with_incomplete_transactions,
'campaigns_with_completed_transactions': campaigns_with_completed_transactions
'campaigns_with_completed_transactions': campaigns_with_completed_transactions,
'campaigns_with_canceled_transactions': campaigns_with_canceled_transactions,
'command_status': command_status
})
return render(request, "campaign_admin.html", context)
def supporter(request, supporter_username, template_name):

View File

@ -360,6 +360,24 @@ class PaymentManager( object ):
return transactions
def cancel_campaign(self, campaign):
'''
cancel_campaign
attempts to cancel active preapprovals related to the campaign
return value: returns a list of transactions with the status of each receiver/transaction updated
'''
transactions = Transaction.objects.filter(campaign=campaign, status=IPN_PAY_STATUS_ACTIVE)
for t in transactions:
result = self.cancel_transaction(t)
return transactions
def finish_transaction(self, transaction):
'''
@ -462,7 +480,7 @@ class PaymentManager( object ):
logger.info("execute_transaction Error: " + p.error_string())
return False
def cancel(self, transaction):
def cancel_transaction(self, transaction):
'''
cancel

View File

@ -130,7 +130,7 @@ def testCancel(request):
t = Transaction.objects.get(id=int(request.GET['transaction']))
p = PaymentManager()
if p.cancel(t):
if p.cancel_transaction(t):
return HttpResponse("Success")
else:
message = "Error: " + t.error