Skip to content

Instantly share code, notes, and snippets.

@tehasdf
Created January 24, 2013 19:41
Show Gist options
  • Save tehasdf/4626906 to your computer and use it in GitHub Desktop.
Save tehasdf/4626906 to your computer and use it in GitHub Desktop.
from flask import Flask, Blueprint, current_app, request
from collections import namedtuple
class ConfigBlueprint(Blueprint):
def register(self, app, options, first_registration):
self.options = options
super(ConfigBlueprint, self).register(app, options, first_registration)
mod = ConfigBlueprint('damage', __name__, url_prefix='/damage')
@mod.route('/right')
def example():
return current_app.blueprints[request.blueprint].options["whatever"]
@mod.route('/wrong')
def example2():
return mod.options["whatever"]
app = Flask(__name__)
app.register_blueprint(mod, whatever='right value')
# now we'll reset the mod name to something that is not the blueprint
mod = namedtuple('BlueprintLiar', 'options')({'whatever': 'wrong value'})
assert mod.options['whatever'] != 'right value'
with app.test_client() as c:
assert c.get('/damage/wrong').data == 'wrong value'
assert c.get('/damage/right').data == 'right value'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment