Files @ 2d078b0004fb
Branch filter:

Location: website/www/conservancy/apps/news/templatetags/fill_url.py

bsturmfels
Apply `futurize --stage-2` Python 2/3 compatibility transformations.

These changes specifically require the use of the "future" library.
from future import standard_library
standard_library.install_aliases()
from builtins import zip
import urllib.parse

from django import template

register = template.Library()

@register.filter(name='fill_url')
def fill_url(given_url, base_url):
    """"Fill out" missing pieces of one URL from another.

    This function parses the given URL, and if it's missing any pieces
    (scheme, netloc, etc.), it fills those in from the base URL.
    Typical usage is "/URL/path"|fill_url:"https://hostname/"
    to generate "https://hostname/URL/path".
    """
    given_parts = urllib.parse.urlsplit(given_url)
    base_parts = urllib.parse.urlsplit(base_url)
    return urllib.parse.urlunsplit(
        given_part or base_part for given_part, base_part in zip(given_parts, base_parts)
    )