Skip to content

@cbsmith /declarative_storage.py
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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.
#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('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.