diff options
| author | Yuval Adam <_@yuv.al> | 2020-10-03 17:27:48 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2020-10-03 17:27:48 +0300 |
| commit | 9aeebe47ce1233d724b140f809b7199729b1921b (patch) | |
| tree | b295921cb09c0e82039384519ae4ae2b66c5fec7 | |
| parent | 4df4ad3bd903ed13d233b34528521a0f25089406 (diff) | |
Render display lat/lng
| -rw-r--r-- | iss/server.py | 5 | ||||
| -rw-r--r-- | iss/templates/passes.html | 2 | ||||
| -rw-r--r-- | iss/tests/test_utils.py | 18 | ||||
| -rw-r--r-- | iss/utils.py | 6 |
4 files changed, 27 insertions, 4 deletions
diff --git a/iss/server.py b/iss/server.py index 73e007d..e766967 100644 --- a/iss/server.py +++ b/iss/server.py @@ -7,6 +7,7 @@ from typing import Optional from .geo import get_location from .predictions import Predictions +from .utils import display_lat_lng app = FastAPI() @@ -19,7 +20,6 @@ templates = Jinja2Templates(directory="iss/templates") async def home(request: Request, cf_connecting_ip: Optional[str] = Header(None)): client_ip = cf_connecting_ip or request.client.host location = get_location(client_ip) - # preds = Predictions(lat, lng, tz=tz, altitude=0, days=5).get_grouped_predictions() return templates.TemplateResponse( "index.html", {"request": request, "location": location} ) @@ -28,12 +28,13 @@ async def home(request: Request, cf_connecting_ip: Optional[str] = Header(None)) @app.get("/passes/{lat}/{lng}") async def passes(request: Request, lat: float, lng: float): preds = Predictions(lat, lng, altitude=0, days=5).get_predictions() + dlat, dlng = display_lat_lng(lat, lng) return templates.TemplateResponse( "passes.html", { "request": request, "predictions_json": json.dumps(preds), - "location": {"lat": lat, "lng": lng}, + "location": {"lat": lat, "lng": lng, "dlat": dlat, "dlng": dlng}, }, ) diff --git a/iss/templates/passes.html b/iss/templates/passes.html index cb740e6..4d27cfc 100644 --- a/iss/templates/passes.html +++ b/iss/templates/passes.html @@ -6,7 +6,7 @@ {% block content %} <div id="passes"> - <p class="f3">Passes for {{ location.lat }}, {{ location.lng }}</p> + <p class="f3">Passes for {{ location.dlat }}, {{ location.dlng }}</p> <p class="f4">Times localized for <span class="light-gray">[[ tz.name ]]</span> (UTC[[ tz.offset ]])</p> <div> <input type="checkbox" id="degrees" v-model="display.degrees"> diff --git a/iss/tests/test_utils.py b/iss/tests/test_utils.py index 17dadf8..f51c10e 100644 --- a/iss/tests/test_utils.py +++ b/iss/tests/test_utils.py @@ -1,6 +1,11 @@ import pytest -from ..utils import deg_to_cardinal, seconds_to_minutes, normalize_lat_lng +from ..utils import ( + deg_to_cardinal, + seconds_to_minutes, + normalize_lat_lng, + display_lat_lng, +) def test_seconds_to_minutes(): @@ -39,6 +44,17 @@ def test_fail_normalize_lat_lng(): normalize_lat_lng(lat, lng) +def test_display_lat_lng(): + cases = [ + (12.345, 67.890, "12.345°N", "67.89°E"), + (12.345, -67.890, "12.345°N", "67.89°W"), + (-12.345, -67.890, "12.345°S", "67.89°W"), + (-12.345, 67.890, "12.345°S", "67.89°E"), + ] + for lat, lng, dlat, dlng in cases: + assert display_lat_lng(lat, lng) == (dlat, dlng) + + def test_deg_to_cardinal(): cases = [ (0, "N"), diff --git a/iss/utils.py b/iss/utils.py index 656d4e3..23667cc 100644 --- a/iss/utils.py +++ b/iss/utils.py @@ -15,6 +15,12 @@ def normalize_lat_lng(lat, lng): return nlat, nlng +def display_lat_lng(lat, lng): + dlat = str(abs(lat)) + "°" + ("N" if lat > 0 else "S") + dlng = str(abs(lng)) + "°" + ("E" if lng > 0 else "W") + return dlat, dlng + + def deg_to_cardinal(deg): cardinals = [ "N", |
