Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Fix external URLs missing HTTP prefixes

Russ Garrettcb38a391994d
diff --git a/web-backend/main.py b/web-backend/main.py
index ec8a3d5..47b04dc 100644
--- a/web-backend/main.py
+++ b/web-backend/main.py
@@ -15,6 +15,7 @@ from template_functions import (
format_length,
format_voltage,
format_percent,
+ format_external_url,
)
from config import database, config
from util import cache_for, country_required
@@ -38,6 +39,7 @@ templates.env.filters["voltage"] = format_voltage
templates.env.filters["percent"] = format_percent
templates.env.filters["country_name"] = country_name
templates.env.globals["osm_link"] = osm_link
+templates.env.filters["external_url"] = format_external_url
class State(TypedDict):
diff --git a/web-backend/template_functions.py b/web-backend/template_functions.py
index 135f191..d3f64d4 100644
--- a/web-backend/template_functions.py
+++ b/web-backend/template_functions.py
@@ -1,4 +1,5 @@
from decimal import Decimal
+from typing import Optional
from jinja2.utils import markupsafe
@@ -54,8 +55,25 @@ def osm_link(osm_id, geom_type):
return url + "/" + str(osm_id)
-THE_COUNTRY_NAMES = {"United States", "United Kingdom", "Netherlands", "Bahamas",
- "Canary Islands", "British Virgin Islands", "Azores", "Cayman Islands"}
+def format_external_url(url: Optional[str]) -> Optional[str]:
+ """Add a http:// prefix to a URL if it doesn't have one."""
+ if url is None:
+ return None
+ if not url.startswith("http"):
+ return "http://" + url
+ return url
+
+
+THE_COUNTRY_NAMES = {
+ "United States",
+ "United Kingdom",
+ "Netherlands",
+ "Bahamas",
+ "Canary Islands",
+ "British Virgin Islands",
+ "Azores",
+ "Cayman Islands",
+}
def country_name(name, cap=False):
diff --git a/web-backend/templates/plant_detail.html b/web-backend/templates/plant_detail.html
index 602ecce..ac466a1 100644
--- a/web-backend/templates/plant_detail.html
+++ b/web-backend/templates/plant_detail.html
@@ -36,7 +36,7 @@
<ul>
<li><a href="{{osm_link(plant.osm_id, plant.geom_type)}}">OpenStreetMap</a></li>
{% if plant.tags.url or plant.tags.website %}
- <li><a href="{{plant.tags.url or plant.tags.website}}">Website</a></li>
+ <li><a href="{{(plant.tags.url or plant.tags.website)|external_url}}">Website</a></li>
{% endif %}
{% if wikidata and wikidata.sitelinks.enwiki %}
<li><a href="{{wikidata.sitelinks.enwiki.url}}">Wikipedia ({{wikidata.sitelinks.enwiki.title}})</a></li>