strip static url trailing slash at assignment

This commit is contained in:
David Lord 2019-05-25 11:14:49 -07:00
parent a12bf290da
commit ed9ab2d3b6
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
5 changed files with 21 additions and 19 deletions

View file

@ -38,6 +38,8 @@ Unreleased
dependency to >= 0.15. :issue:`3022`
- Support ``static_url_path`` that ends with a forward slash.
:issue:`3134`
- Support empty ``static_folder`` without requiring setting an empty
``static_url_path`` as well. :pr:`3124`
- :meth:`jsonify` supports :class:`dataclasses.dataclass` objects.
:pr:`3195`
- Allow customizing the :attr:`Flask.url_map_class` used for routing.

View file

@ -408,11 +408,8 @@ class Flask(_PackageBoundObject):
self, import_name, template_folder=template_folder, root_path=root_path
)
if static_url_path is not None:
self.static_url_path = static_url_path
if static_folder is not None:
self.static_folder = static_folder
self.static_url_path = static_url_path
self.static_folder = static_folder
if instance_path is None:
instance_path = self.auto_find_instance_path()
@ -594,7 +591,7 @@ class Flask(_PackageBoundObject):
bool(static_host) == host_matching
), "Invalid static_host/host_matching combination"
self.add_url_rule(
self.static_url_path.rstrip("/") + "/<path:filename>",
self.static_url_path + "/<path:filename>",
endpoint="static",
host=static_host,
view_func=self.send_static_file,

View file

@ -208,7 +208,7 @@ class Blueprint(_PackageBoundObject):
if self.has_static_folder:
state.add_url_rule(
self.static_url_path.rstrip("/") + "/<path:filename>",
self.static_url_path + "/<path:filename>",
view_func=self.send_static_file,
endpoint="static",
)

View file

@ -965,24 +965,27 @@ class _PackageBoundObject(object):
)
del _get_static_folder, _set_static_folder
def _get_static_url_path(self):
@property
def static_url_path(self):
"""The URL prefix that the static route will be accessible from.
If it was not configured during init, it is derived from
:attr:`static_folder`.
"""
if self._static_url_path is not None:
return self._static_url_path
if self.static_folder is not None:
basename = os.path.basename(self.static_folder)
return "/" + basename if basename else ""
return ("/" + basename).rstrip("/")
@static_url_path.setter
def static_url_path(self, value):
if value is not None:
value = value.rstrip("/")
def _set_static_url_path(self, value):
self._static_url_path = value
static_url_path = property(
_get_static_url_path,
_set_static_url_path,
doc="The URL prefix that the static route will be registered for.",
)
del _get_static_url_path, _set_static_url_path
@property
def has_static_folder(self):
"""This is ``True`` if the package bound object's container has a

View file

@ -1427,14 +1427,14 @@ def test_static_url_path_with_ending_slash():
assert flask.url_for("static", filename="index.html") == "/foo/index.html"
def test_static_url_null_path(app):
def test_static_url_empty_path(app):
app = flask.Flask(__name__, static_folder='', static_url_path='')
rv = app.test_client().open('/static/index.html', method='GET')
assert rv.status_code == 200
rv.close()
def test_static_url_null_path_defaulting(app):
def test_static_url_empty_path_default(app):
app = flask.Flask(__name__, static_folder='')
rv = app.test_client().open('/static/index.html', method='GET')
assert rv.status_code == 200