Skip to content

Commit

Permalink
Merge pull request #379 from flask-restful/bp-url-field
Browse files Browse the repository at this point in the history
Fixed fields.Url to allow it to be used with blueprints
  • Loading branch information
joshfriend committed Jan 1, 2015
2 parents 90dd5e7 + b95d3a2 commit bf983cf
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
7 changes: 4 additions & 3 deletions flask_restful/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from urllib.parse import urlparse, urlunparse

from flask_restful import inputs, marshal
from flask import url_for
from flask import url_for, request

__all__ = ["String", "FormattedString", "Url", "DateTime", "Float",
"Integer", "Arbitrary", "Nested", "List", "Raw", "Boolean",
Expand Down Expand Up @@ -277,7 +277,7 @@ class Url(Raw):
"""
A string representation of a Url
"""
def __init__(self, endpoint, absolute=False, scheme=None):
def __init__(self, endpoint=None, absolute=False, scheme=None):
super(Url, self).__init__()
self.endpoint = endpoint
self.absolute = absolute
Expand All @@ -286,7 +286,8 @@ def __init__(self, endpoint, absolute=False, scheme=None):
def output(self, key, obj):
try:
data = to_marshallable_type(obj)
o = urlparse(url_for(self.endpoint, _external=self.absolute, **data))
endpoint = self.endpoint if self.endpoint is not None else request.endpoint
o = urlparse(url_for(endpoint, _external=self.absolute, **data))
if self.absolute:
scheme = self.scheme if self.scheme is not None else o.scheme
return urlunparse((scheme, o.netloc, o.path, "", "", ""))
Expand Down
74 changes: 73 additions & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from flask.ext.restful.utils import OrderedDict
from flask_restful import fields
from datetime import datetime, timedelta, tzinfo
from flask import Flask
from flask import Flask, Blueprint
#noinspection PyUnresolvedReferences
from nose.tools import assert_equals # you need it for tests in form of continuations

Expand Down Expand Up @@ -170,6 +170,78 @@ def test_url_absolute_scheme(self):
with app.test_request_context("/", base_url="http://localhost"):
self.assertEquals("https://localhost/3", field.output("hey", Foo()))

def test_url_without_endpoint_invalid_object(self):
app = Flask(__name__)
app.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
field = fields.Url()

with app.test_request_context("/hey"):
self.assertRaises(MarshallingException, lambda: field.output("hey", None))

def test_url_without_endpoint(self):
app = Flask(__name__)
app.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
field = fields.Url()

with app.test_request_context("/hey"):
self.assertEquals("/3", field.output("hey", Foo()))

def test_url_without_endpoint_absolute(self):
app = Flask(__name__)
app.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
field = fields.Url(absolute=True)

with app.test_request_context("/hey"):
self.assertEquals("http://localhost/3", field.output("hey", Foo()))

def test_url_without_endpoint_absolute_scheme(self):
app = Flask(__name__)
app.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
field = fields.Url(absolute=True, scheme='https')

with app.test_request_context("/hey", base_url="http://localhost"):
self.assertEquals("https://localhost/3", field.output("hey", Foo()))

def test_url_with_blueprint_invalid_object(self):
app = Flask(__name__)
bp = Blueprint("foo", __name__, url_prefix="/foo")
bp.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
app.register_blueprint(bp)
field = fields.Url()

with app.test_request_context("/foo/hey"):
self.assertRaises(MarshallingException, lambda: field.output("hey", None))

def test_url_with_blueprint(self):
app = Flask(__name__)
bp = Blueprint("foo", __name__, url_prefix="/foo")
bp.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
app.register_blueprint(bp)
field = fields.Url()

with app.test_request_context("/foo/hey"):
self.assertEquals("/foo/3", field.output("hey", Foo()))

def test_url_with_blueprint_absolute(self):
app = Flask(__name__)
bp = Blueprint("foo", __name__, url_prefix="/foo")
bp.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
app.register_blueprint(bp)
field = fields.Url(absolute=True)

with app.test_request_context("/foo/hey"):
self.assertEquals("http://localhost/foo/3", field.output("hey", Foo()))

def test_url_with_blueprint_absolute_scheme(self):
app = Flask(__name__)
bp = Blueprint("foo", __name__, url_prefix="/foo")
bp.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
app.register_blueprint(bp)
field = fields.Url(absolute=True, scheme='https')

with app.test_request_context("/foo/hey", base_url="http://localhost"):
self.assertEquals("https://localhost/foo/3", field.output("hey", Foo()))

def test_int(self):
field = fields.Integer()
self.assertEquals(3, field.output("hey", {'hey': 3}))
Expand Down

0 comments on commit bf983cf

Please sign in to comment.