Skip to content

Instantly share code, notes, and snippets.

@jmcarp
Created January 21, 2014 20:36
Show Gist options
  • Save jmcarp/8547858 to your computer and use it in GitHub Desktop.
Save jmcarp/8547858 to your computer and use it in GitHub Desktop.
Copying URL parameters from referrer
@app.before_request
def prepare_private_key():
# Done if private_key in args
key_from_args = request.args.get('private_key')
if key_from_args:
return
# Check referrer for private key
parsed_referrer = urllib.urlparse(request.referrer)
referrer_args = dict(urllib.parse_qsl(parsed_referrer.query))
key_from_referrer = referrer_args.get('private_key')
# Update URL and redirect
if key_from_referrer:
parsed_path = urllib.urlparse(request.path)
path_args = dict(urllib.parse_qsl(parsed_path.query))
path_args['private_key'] = key_from_referrer
new_parsed_path = parsed_path._replace(query=urllib.urlencode(path_args))
new_path = urlparse.urlunparse(new_parsed_path)
return redirect(new_path, code=307)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment