Skip to content

Commit

Permalink
using urlparse for the urlclean filter
Browse files Browse the repository at this point in the history
  • Loading branch information
iambibhas committed Aug 6, 2020
1 parent 6a1e0d6 commit 73f9bf4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
17 changes: 8 additions & 9 deletions baseframe/filters.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from six.moves.urllib.parse import urlparse, urlunparse
import six

from datetime import datetime, timedelta
Expand Down Expand Up @@ -288,12 +289,10 @@ def timestamp_filter(value):

@baseframe.app_template_filter('cleanurl')
def cleanurl_filter(url):
if url.startswith('http://'):
url = url[7:]
elif url.startswith('https://'):
url = url[8:]
if url.endswith('/') and url.count('/') == 1:
# Remove trailing slash if applied to end of domain name
# but leave it in if it's a path
url = url[:-1]
return url
parsed_url = urlparse(url)
unparsed_url = urlunparse(('', parsed_url.netloc, parsed_url.path, '', '', ''))
if unparsed_url.startswith('//'):
unparsed_url = unparsed_url.lstrip('//')
if unparsed_url.endswith('/'):
unparsed_url = unparsed_url.rstrip('/')
return unparsed_url
20 changes: 20 additions & 0 deletions tests/test_filters.py
Expand Up @@ -363,3 +363,23 @@ def test_none_if_empty(self):
assert none_if_empty_func([]) is None
assert none_if_empty_func(False) is None
assert none_if_empty_func(0) is None

def test_cleanurl(self):
assert (
filters.cleanurl_filter("https://example.com/some/path/?query=value")
== "example.com/some/path"
)
assert (
filters.cleanurl_filter("example.com/some/path/?query=value")
== "example.com/some/path"
)
assert (
filters.cleanurl_filter("example.com/some/path/") == "example.com/some/path"
)
assert (
filters.cleanurl_filter("example.com/some/path") == "example.com/some/path"
)
assert filters.cleanurl_filter("example.com/") == "example.com"
assert filters.cleanurl_filter("//example.com/") == "example.com"
assert filters.cleanurl_filter("//test/") == "test"
assert filters.cleanurl_filter("foobar") == "foobar"

0 comments on commit 73f9bf4

Please sign in to comment.