public
Last active

My rather lame approach to setting parameter storage class. It also demonstrates how to write an IPN handler with Flask, as that was my use case where form argument order mattered (most annoying). End result is uglier than just setting the storage class inside your function, but does make it easier to see when that special case is being used.

  • Download Gist
declarative_storage.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#demonstration of hack to declaratively set param_storage_class declaratively using a decorator
#also demonstrates how to write an IPN handler with Flask
 
from flask import Flask, make_response
from itertools import chain
app = Flask(__name__)
 
#Normally this parameter would come from a config
IPN_URLSTRING = 'https://www.sandbox.paypal.com/cgi-bin/webscr'
IPN_VERIFY_EXTRA_PARAMS = (('cmd', '_notify-validate'),)
 
def ordered_storage(f):
import werkzeug.datastructures
import flask
def decorator(*args, **kwargs):
flask.request.parameter_storage_class = werkzeug.datastructures.ImmutableOrderedMultiDict
return f(*args, **kwargs)
return decorator
@app.route('/webhooks/paypal', methods=['POST'])
@ordered_storage
def paypal_webhook():
#probably should have a sanity check here on the size of the form data to guard against DoS attacks
verify_args = chain(request.form.iteritems(), IPN_VERIFY_EXTRA_PARAMS)
verify_string = '&'.join(('%s=%s' % (param, value) for param, value in verify_args))
with closing(urlopen(IPN_URLSTRING, data=verify_string)) as paypal_verify_request:
response_string = paypal_verify_request.read()
if response_string != 'VERIFIED':
raise ValueError('Did not receive expected IPN confirmation from PayPal')
return make_response('')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.