url_for Builderror in flask extension with pluggable views - python

I am building a simple extension for flask and have a problem with the url_for function not being able to build the urls within the extension.
Can somebody help me figure out what I am missing here?
I simplified the code to demonstrate the issue (all of the url_for calls raise a werkzeug BuildError exception):
import flask
import flask.views
import logging
logging.basicConfig(level=logging.DEBUG)
class MyFlaskExt(object):
def __init__(self, app=None):
if app is not None:
self.init_app(app)
def init_app(self, app):
self.blueprint = flask.Blueprint('myext', __name__, static_folder='static', template_folder='templates')
self.blueprint.add_url_rule('/', view_func=RootView.as_view('root'), endpoint='root')
self.blueprint.add_url_rule('/var/<somevar>', view_func=VarView.as_view('var'), endpoint='var')
self.blueprint.add_url_rule('/novar', view_func=NoVarView.as_view('novar'), endpoint='novar')
app.register_blueprint(self.blueprint)
class RootView(flask.views.View):
def dispatch_request(self):
logging.debug(flask.url_for('novar'))
logging.debug(flask.url_for('novar', _external=True))
return flask.redirect(flask.url_for('var', somevar='test'))
class VarView(flask.views.View):
def dispatch_request(self, somevar):
return "SUCCESS! ({})".format(somevar)
class NoVarView(flask.views.View):
def dispatch_request(self):
return "SUCCESS!"
if __name__ == '__main__':
app = flask.Flask(__name__)
app.debug = True
my_ext = MyFlaskExt()
my_ext.init_app(app)
logging.debug(app.url_map)
app.run()

Blueprint endpoints are registered under the name of the Blueprint. If I remember correctly you will either need to prepend your url_for calls with "." (if they will all be operating under the same blueprint) or use the full name:
def dispatch_request(self):
logging.debug(flask.url_for('.novar'))
logging.debug(flask.url_for('.novar', _external=True))
return flask.redirect(flask.url_for('.var', somevar='test'))

Related

How to have separate routers module in Flask?

How can I decouple this class? I would like to put the paths in another file, is it possible to move the routes in another file?
#api.route('/home', '/api/email')
class Server(Resource):
def create_server(app, oauth=None):
if not oauth:
oauth = default_provider(app)
app = prepare_app(app)
#app.before_request
def load_current_user():
user = User.query.get(1)
g.user = user
#app.route('/home')
def home():
return 'home'
#app.route('/oauth/authorize', methods=['GET', 'POST'])
#oauth.authorize_handler
def authorize(*args, **kwargs):
return True
Those
#app.route('/home') # and
#app.route('/oauth/authorize', methods=['GET', 'POST'])
have to be in another file.
My attempt was this, I tried to create a file for routers:
class Router():
def __init__(self, app, oauth):
self.app = app
self.oauth = oauth
#app.route('/home')
def home():
return 'home'
I'm getting this error:
NameError: name 'app' is not defined
Well, I see a package you can use for flask projects called Flask-Via [pypi], inspired by the Django URL configuration system and designed to add similar functionality to Flask applications that have grown beyond a simple single file application. The following example is given from the docs of this project:
from flask import Flask
from flask.ext.via import Via
from flask.ext.via.routers.default import Functional
app = Flask(__name__)
def foo(bar=None):
return 'Foo View!'
routes = [
Functional('/foo', foo),
Functional('/foo/<bar>', foo, endpoint='foo2'),
]
via = Via()
via.init_app(app, route_module='flask_via.examples.basic')
if __name__ == "__main__":
app.run(debug=True)
I think this is exactly what you want :) and you can move it to another python module for example called routers.py.

Unable to find template with BluePrints Flask

I am trying to take a modular approach to designing my Flask App.
So each module as multiple routes. See below
Templates folder
Contents of my home_routes.py
from flask import Blueprint, request, session, abort
from flask import render_template
def home_blueprints(app):
home_routes = Blueprint("home_routes", __name__, template_folder='../../templates', static_folder='../../static')
#home_routes.route('/home/Links')
def links():
return render_template('links.html')
#home_routes.route('/home/Confirmation')
def confirmation():
return render_template('Confirmation.html')
#home_routes.route('/')
def main():
return render_template('main.html')
#home_routes.route('/home/Contactus', methods=['GET', 'POST'])
def contact():
if request.method == "POST":
token = session.pop('_csrf_token', None)
if not token or token != request.form.get('_csrf_token'):
abort(403)
return render_template('GoogleForm.html')
app.register_blueprint(home_routes)
Contents of believe_routes.py
from flask import Blueprint
from flask import render_template
def believe_blueprints(app):
believe_routes = Blueprint("believe_routes", __name__, template_folder='../../templates', static_folder='../../static')
#believe_routes.route('/home/Believe/Truth_About_God')
def truth_about_God():
return render_template('The_Truth_Concerning_God.html')
#believe_routes.route('/home/Believe/Truth_We_Believe')
def the_truth_we_beleive():
return render_template('The_Truth_We_Believe.html')
#believe_routes.route('/home/Believe/Truth_About_Christ')
def truth_about_christ():
return render_template('The_Truth_About_Christ.html')
#believe_routes.route('/home/Believe/Truth_About_Church')
def truth_about_church():
return render_template('The_Truth_About_Church.html')
#believe_routes.route('/home/Believe/Truth_About_Salvation')
def truth_about_salvation():
return render_template('The_Truth_About_Salvation.html')
#believe_routes.route('/Believe')
def truth_we_believe():
return render_template('Truth_We_Believe.html')
app.register_blueprint(believe_routes)
Contents of init.py
from home_routes import home_blueprints
from believe_routes import believe_blueprints
def configure_blueprints(app):
home_blueprints(app)
believe_blueprints(app)
Only the home_routes work. The URLs in the believe_routes do not work. I get a 404
INFO 2017-05-26 18:01:44,325 module.py:813] default: "GET /home/Believe/Truth_About_Christ HTTP/1.1" 404 233
I am calling configure_blueprints(app) from create_app which is then called from main.py.
Any thoughts please.
the 404 is not for the template reason, maybe you can try this code to view whether blueprint is registed to flask instance, and you can put your output back for more details...
#!/usr/bin/env python
# encoding: utf-8
from flask import Flask
from home_routes import home_blueprints
from believe_routes import believe_blueprints
app = Flask('demo')
def configure_blueprints(app):
home_blueprints(app)
believe_blueprints(app)
configure_blueprints(app)
print(app.url_map)
i hope this would help you ..

404 Response when running FlaskClient test method

I'm baffled by this. I'm using an application factory in a Flask application and under the test configuration my routes always return 404s.
However when I use Flask-Script and load the app from the interpreter everything works as expected, the response comes back as 200.
Navigating to the URL with the browser works fine
app/__init__.py
def create_app():
app = Flask(__name__)
return app
sever1.py
from flask import Flask
from flask_script import Manager
from app import create_app
app = create_app()
app_context = app.app_context()
app_context.push()
manager = Manager(app)
#app.route('/')
def index():
return '<h1>Hello World!</h1>'
#app.route('/user/<name>')
def user(name):
return '<h1>Hello, %s!</h1>' % name
#manager.command
def test():
"""Run the unit tests"""
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
if __name__ == '__main__':
manager.run()
tests/test.py
#imports committed
def setUp(self):
self.app = create_app('testing')
self.app_context = self.app.app_context()
self.app_context.push()
self.client = self.app.test_client()
def test_app_exists(self):
response = self.client.get('/', follow_redirects=True)
print(response) #404 :(
self.assertTrue("Hello World!" in response.get_data()) #this is just an example of how it fails
You're not using the factory pattern correctly. You should use blueprints to collect routes and register them with the app in the factory. (Or use app.add_url_rule in the factory.) Nothing outside the factory should affect the app.
Right now you create an instance of the app and then use that instance to register routes. Then you create a different instance in your tests, which doesn't have the routes registered. Since that instance doesn't have any registered routes, it returns 404 for requests to those urls.
Instead, register your routes with a blueprint, then register the blueprint with the app in the factory. Use the factory to create an app during tests. Pass the factory to the Flask-Script manager. You should not need to push the app context manually.
from flask import Flask, Blueprint
from flask_script import Manager
from unittest import TestCase
bp = Blueprint('myapp', __name__)
#bp.route('/')
def index():
return 'Hello, World!'
def create_app(config='dev'):
app = Flask(__name__)
# config goes here
app.register_blueprint(bp)
return app
class SomeTest(TestCase):
def setUp(self):
self.app = create_app(config='test')
self.client = self.app.test_client()
def test_index(self):
rv = self.client.get('/')
self.assertEqual(rv.data, b'Hello, World!')
manager = Manager(create_app)
manager.add_option('-c', '--config', dest='config', required=False)
if __name__ == '__main__':
manager.run()

flask-restful having a get/<id> and post with json in the same class

The get method on user works if the # api.add_resource(User, '/user/')
line is uncommented, and the other api.add_resource is.
The inverse of that is true to make the post method work.
How can I get both of these paths to work?
from flask import Flask, request
from flask.ext.restful import reqparse, abort, Api, Resource
import os
# set the project root directory as the static folder, you can set others.
app = Flask(__name__)
api = Api(app)
class User(Resource):
def get(self, userid):
print type(userid)
if(userid == '1'):
return {'id':1, 'name':'foo'}
else:
abort(404, message="user not found")
def post(self):
# should just return the json that was posted to it
return request.get_json(force=True)
api.add_resource(User, '/user/')
# api.add_resource(User, '/user/<string:userid>')
if __name__ == "__main__":
app.run(debug=True)
Flask-Restful supports registering multiple URLs for a single resource. Simply provide both URLs when you register the User resource:
api.add_resource(User, '/user/', '/user/<userid>')

Flask, Blueprint, current_app

I am trying to add a function in the Jinja environment from a blueprint (a function that I will use into a template).
Main.py
app = Flask(__name__)
app.register_blueprint(heysyni)
MyBluePrint.py
heysyni = Blueprint('heysyni', __name__)
#heysyni.route('/heysyni'):
return render_template('heysyni.html', heysini=res_heysini)
Now in MyBluePrint.py, I would like to add something like :
def role_function():
return 'admin'
app.jinja_env.globals.update(role_function=role_function)
I will then be able to use this function in my template. I cannot figure out how I can access the application since
app = current_app._get_current_object()
returns the error:
working outside of request context
How can I implement such a pattern ?
The message error was actually pretty clear :
working outside of request context
In my blueprint, I was trying to get my application outside the 'request' function :
heysyni = Blueprint('heysyni', __name__)
app = current_app._get_current_object()
print(app)
#heysyni.route('/heysyni/')
def aheysyni():
return 'hello'
I simply had to move the current_app statement into the function. Finally it works that way :
Main.py
from flask import Flask
from Ablueprint import heysyni
app = Flask(__name__)
app.register_blueprint(heysyni)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
Ablueprint.py
from flask import Blueprint, current_app
heysyni = Blueprint('heysyni', __name__)
#heysyni.route('/heysyni/')
def aheysyni():
# Got my app here
app = current_app._get_current_object()
return 'hello'

Categories

Resources