Implement donations
This commit is contained in:
0
donate/__init__.py
Normal file
0
donate/__init__.py
Normal file
4
donate/admin.py
Normal file
4
donate/admin.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import models
|
||||
from django.contrib import admin
|
||||
|
||||
admin.site.register(models.Donation)
|
4
donate/forms.py
Normal file
4
donate/forms.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from django import forms
|
||||
|
||||
class DonationForm(forms.Form):
|
||||
quantity = forms.DecimalField(min_value=0.01, required=True, label="Donation Quantity", help_text="In USD")
|
7
donate/management/__init__.py
Normal file
7
donate/management/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.db.models.signals import post_syncdb
|
||||
from notification import models as notification
|
||||
|
||||
def create_notice_types(app, created_models, verbosity, **kwargs):
|
||||
notification.create_notice_type("donation_paid", "Donation Recieved", "A donation was made")
|
||||
|
||||
post_syncdb.connect(create_notice_types, sender=notification)
|
22
donate/models.py
Normal file
22
donate/models.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
class Donation(models.Model):
|
||||
class Meta:
|
||||
permissions = (
|
||||
('donation_mail', 'Recieves emails about new donations'),
|
||||
)
|
||||
|
||||
STATUS_PENDING = 0
|
||||
STATUS_PAID = 1
|
||||
STATUS = (
|
||||
(STATUS_PENDING, 'Pending'),
|
||||
(STATUS_PAID, 'Paid'),
|
||||
)
|
||||
created = models.DateTimeField(editable=False, auto_now_add=True)
|
||||
updated = models.DateTimeField(editable=False, auto_now=True)
|
||||
quantity = models.FloatField()
|
||||
transactionId = models.IntegerField(blank=True, null=True)
|
||||
status = models.IntegerField(default=STATUS_PENDING, choices = STATUS)
|
||||
user = models.ForeignKey(User)
|
||||
|
3
donate/signals.py
Normal file
3
donate/signals.py
Normal file
@@ -0,0 +1,3 @@
|
||||
import django.dispatch
|
||||
|
||||
donation_paid = django.dispatch.Signal(providing_args=["donation"])
|
16
donate/tests.py
Normal file
16
donate/tests.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
This file demonstrates writing tests using the unittest module. These will pass
|
||||
when you run "manage.py test".
|
||||
|
||||
Replace this with more appropriate tests for your application.
|
||||
"""
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class SimpleTest(TestCase):
|
||||
def test_basic_addition(self):
|
||||
"""
|
||||
Tests that 1 + 1 always equals 2.
|
||||
"""
|
||||
self.assertEqual(1 + 1, 2)
|
7
donate/urls.py
Normal file
7
donate/urls.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.conf.urls.defaults import patterns, include, url
|
||||
|
||||
urlpatterns = patterns('donate',
|
||||
url(r'^$', 'views.index'),
|
||||
url(r'^dwolla$', 'views.dwollaCallback'),
|
||||
url(r'^thanks$', 'views.thanks'),
|
||||
)
|
61
donate/views.py
Normal file
61
donate/views.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from django.shortcuts import render_to_response
|
||||
from django.conf import settings
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.template import RequestContext
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.db.models import F
|
||||
from notification import models as notification
|
||||
import urllib2
|
||||
import json
|
||||
import forms
|
||||
import models
|
||||
import signals
|
||||
|
||||
def index(request):
|
||||
if request.method == 'POST':
|
||||
form = forms.DonationForm(request.POST)
|
||||
else:
|
||||
form = forms.DonationForm()
|
||||
if form.is_valid():
|
||||
data = {}
|
||||
order = {}
|
||||
item = {}
|
||||
item['Description'] = "Caminus Donation"
|
||||
item['Name'] = "Caminus Donation"
|
||||
item['Price'] = str(form.cleaned_data['quantity'])
|
||||
item['Quantity'] = 1
|
||||
order['OrderItems'] = [item,]
|
||||
order['Test'] = True
|
||||
order['Tax'] = 0.00
|
||||
order['Total'] = str(form.cleaned_data['quantity'])
|
||||
order['DestinationId'] = settings.DWOLLA_API_ID
|
||||
data['PurchaseOrder'] = order
|
||||
data['Key'] = settings.DWOLLA_API_KEY
|
||||
data['Secret'] = settings.DWOLLA_API_SECRET
|
||||
data['Callback'] = 'http://camin.us%s'%reverse('donate.views.dwollaCallback')
|
||||
data['Redirect'] = 'http://camin.us%s'%reverse('donate.views.thanks')
|
||||
donation = models.Donation.objects.create(quantity=form.cleaned_data['quantity'], user=request.user)
|
||||
order['OrderId'] = donation.id
|
||||
req = urllib2.Request("https://www.dwolla.com/payment/request", data=json.dumps(data), headers={'Content-Type': 'application/json'})
|
||||
response = json.load(urllib2.urlopen(req))
|
||||
return HttpResponseRedirect("https://www.dwolla.com/payment/checkout/%s"%(response['CheckoutId']))
|
||||
else:
|
||||
return render_to_response('donate/index.html', {'form': form}, context_instance = RequestContext(request))
|
||||
|
||||
@csrf_exempt
|
||||
def dwollaCallback(request):
|
||||
if request.method =='POST':
|
||||
data = json.loads(request.raw_post_data)
|
||||
donation = models.Donation.objects.get(id=data['OrderId'])
|
||||
donation.status = models.Donation.STATUS_PAID
|
||||
donation.transactionId = data['TransactionId']
|
||||
donation.save()
|
||||
acct = donation.user.minecraftprofile.currencyaccount
|
||||
acct.balance = F('balance')+(donation.quantity*2000)
|
||||
acct.save()
|
||||
notification.send_now([donation.user], "donation_paid", {"donation":donation, "credit":donation.quantity*2000})
|
||||
return HttpResponse(status=204)
|
||||
|
||||
def thanks(request):
|
||||
return render_to_response('donate/thanks.html')
|
@@ -133,7 +133,8 @@ INSTALLED_APPS = (
|
||||
'local',
|
||||
'messages',
|
||||
'notification',
|
||||
'piston'
|
||||
'piston',
|
||||
'donate'
|
||||
# Uncomment the next line to enable admin documentation:
|
||||
# 'django.contrib.admindocs',
|
||||
)
|
||||
|
11
templates/donate/index.html
Normal file
11
templates/donate/index.html
Normal file
@@ -0,0 +1,11 @@
|
||||
{% extends 'base_simple.html'%}
|
||||
|
||||
{% block content %}
|
||||
<p>Donating to caminus helps keep us alive!</p>
|
||||
<p>All donations recieve 2000gr per $1 USD. You must be logged in before donating to recieve this.</p>
|
||||
<form method="POST">
|
||||
{{form.as_p}}
|
||||
{%csrf_token%}
|
||||
<input type="submit"/>
|
||||
</form>
|
||||
{% endblock %}
|
3
templates/notification/donation_paid/full.txt
Normal file
3
templates/notification/donation_paid/full.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
We have recieved a donation of {{donation.quantity}} through Dwolla.
|
||||
|
||||
Your account has been credited {{credit}}gr.
|
3
urls.py
3
urls.py
@@ -19,7 +19,8 @@ urlpatterns = patterns('',
|
||||
url(r'^forums/', include('forums.urls')),
|
||||
url(r'^api/', include('api.urls')),
|
||||
url(r'^notification/', include('notification.urls')),
|
||||
url(r'^i/(?P<code>.+)', 'local.views.claimInvite')
|
||||
url(r'^i/(?P<code>.+)', 'local.views.claimInvite'),
|
||||
url(r'^donate/', include('donate.urls'))
|
||||
)
|
||||
|
||||
urlpatterns += staticfiles_urlpatterns()
|
||||
|
Reference in New Issue
Block a user