donate pages and clean up of code

This commit is contained in:
Oliver Marks 2015-12-16 13:42:06 +00:00
parent 445bcd4943
commit 2b98d87928
3 changed files with 157 additions and 0 deletions

41
site/data/donate.py Normal file
View File

@ -0,0 +1,41 @@
import os
from scaffold.core.data.select import select_data
from scaffold.core.data.insert import insert_data
#~ from scaffold.core.data.update import update_data
#~ from scaffold.core.data.delete import delete_data
from scaffold.core.data.sql import query_builder
query_builder.query_path = os.path.abspath('./data/sql/')
class get_pledge(select_data):
debug = True
table = 'pledges'
columns = {'name', 'total'}
required = {'name'}
class get_pledges(select_data):
debug = True
#~ table = 'pledges'
query_file = 'pledge_totals.sql'
#~ required = {'expired'}
columns_where = {'expired'}
grouping = {'name'}
class add_pledge(insert_data):
debug = True
table = 'pledges'
required = {'name'}
columns = {'name'}
class add_payment(insert_data):
debug = True
table = 'pledge_amounts'
required = {'pledge_id', 'reference', 'amount', 'environment'}
columns = {'pledge_id', 'reference', 'amount', 'environment'}

View File

@ -0,0 +1,3 @@
select name, sum(pledge_amounts.amount) as total, target
from pledges
left join pledge_amounts on pledges.id=pledge_amounts.pledge_id

113
site/pages/donate.py Normal file
View File

@ -0,0 +1,113 @@
from flask import Blueprint
from flask import request
from flask import redirect, abort
from scaffold import web
from pages import header, footer
from data import donate
from config.settings import *
donate_pages = Blueprint('donate_pages', __name__, template_folder='templates')
@donate_pages.route("/donate", methods=['GET'])
@donate_pages.route("/donate/", methods=['GET'])
def index():
web.template.create('Maidstone Hackspace')
header('Maidstone Hackspace Donations')
web.page.create('Make a donation')
web.paragraph.create(
"""If you would like to donate to the space please type an amount and use the reference code for what ever your donating for, for example use #lair to donate to getting a space.
We may run pledges in the future for equipment in which case use the reference for the equipment your pledging towards.""")
web.page.section(web.paragraph.render())
for item in donate.get_pledges():
web.paragraph.create(
"""Currently raised £%.2f towards %s target is £%.2f.""" % (
item.get('total', 0) if item.get('total', 0) else 0.0,
item.get('name'),
item.get('target', 0)))
web.page.section(web.paragraph.render())
web.form.create('Donate to Maidstone Hackspace', '/donate/submit')
web.form.append(name='reference', label='Reference', placeholder='#lair', value='#lair', input_type='select')
web.form.append(name='amount', label='Donation Amount', placeholder='50.00', value='50.00')
web.page.append(web.form.render())
web.template.body.append(web.page.render())
return footer()
@donate_pages.route("/donate/populate", methods=['GET'])
def populate_by_name():
pledge = donate.get_pledge({'name': '#lair'}).get()
import gocardless
gocardless.environment = gocardless_environment
gocardless.set_details(**gocardless_credentials)
merchant = gocardless.client.merchant()
for bill in merchant.bills():
environment = int(gocardless_environment=='production')
donate.add_payment().execute({'pledge_id':pledge.get('id','') , 'reference': bill.id, 'amount': bill.amount_minus_fees, 'environment': environment})
return abort()
@donate_pages.route("/donate/submit", methods=['POST'])
def submit_donation():
import gocardless
gocardless.environment = gocardless_environment
print app_domain
gocardless.set_details(**gocardless_credentials)
url = gocardless.client.new_bill_url(
request.form.get('amount'),
name=request.form.get('reference'),
redirect_uri='%s/donate/success' % app_domain)
#~ redirect_uri='%s/donate/success' % gocardless_redirect_uri if gocardless_redirect_uri else app_domain)
return redirect(url)
@donate_pages.route("/donate/success", methods=['GET'])
def success_donation():
# confirm the payment
bill_id = request.args.get('resource_id')
try:
import gocardless
gocardless.environment = gocardless_environment
gocardless.set_details(**gocardless_credentials)
gocardless.client.confirm_resource(request.args)
web.page.create('Thanks for your donation')
web.paragraph.create(
"""Thanks your payment has been recieved.""")
except:
# TODO log what actually has gone wrong
web.page.create('Something went wrong')
web.paragraph.create(
"""We could not confirm the payment something may have gone terribly wrong.""")
if bill_id:
bill = gocardless.client.bill(bill_id)
pledge = donate.get_pledge({'name': bill.name}).get()
#~ print dir(bill)
print bill.amount
print bill.amount_minus_fees
print bill.charge_customer_at
print bill.created_at
print bill.name
print bill.payout
print bill.status
print bill.user
environment = int(gocardless_environment=='production')
donate.add_payment().execute({'pledge_id':pledge.get('name','') , 'reference': bill_id, 'amount': bill.amount_minus_fees, 'environment': environment})
web.template.create('Maidstone Hackspace')
header('Maidstone Hackspace Donations')
web.page.create('Thanks for your donation')
web.paragraph.create(
"""Thanks your payment has been recieved.""")
web.page.section(web.paragraph.render())
web.template.body.append(web.page.render())
return footer()