Skip to content

Commit

Permalink
Merge pull request flask-restful#410 from flask-restful/url-for-bluep…
Browse files Browse the repository at this point in the history
…rint

Fix url_for when used with Blueprints
  • Loading branch information
joshfriend committed Mar 4, 2015
2 parents b97b1cf + 72afe1a commit ff6493b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 7 additions & 2 deletions flask_restful/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,13 @@ def wrapper(*args, **kwargs):
return wrapper

def url_for(self, resource, **values):
"""Generates a URL to the given resource."""
return url_for(resource.endpoint, **values)
"""Generates a URL to the given resource.
Works like :func:`flask.url_for`."""
endpoint = resource.endpoint
if self.blueprint:
endpoint = '{0}.{1}'.format(self.blueprint.name, endpoint)
return url_for(endpoint, **values)

def make_response(self, data, *args, **kwargs):
"""Looks up the representation transformer for the requested media
Expand Down
14 changes: 13 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from flask import Flask, redirect, views
from flask import Flask, Blueprint, redirect, views
from flask.signals import got_request_exception, signals_available
try:
from mock import Mock, patch
Expand Down Expand Up @@ -731,6 +731,18 @@ def test_url_for(self):
with app.test_request_context('/foo'):
self.assertEqual(api.url_for(HelloWorld, id=123), '/ids/123')

def test_url_for_with_blueprint(self):
"""Verify that url_for works when an Api object is mounted on a
Blueprint.
"""
api_bp = Blueprint('api', __name__)
app = Flask(__name__)
api = flask_restful.Api(api_bp)
api.add_resource(HelloWorld, '/foo/<string:bar>')
app.register_blueprint(api_bp)
with app.test_request_context('/foo'):
self.assertEqual(api.url_for(HelloWorld, bar='baz'), '/foo/baz')

def test_fr_405(self):
app = Flask(__name__)
api = flask_restful.Api(app)
Expand Down

0 comments on commit ff6493b

Please sign in to comment.