From 9aeebe47ce1233d724b140f809b7199729b1921b Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Sat, 3 Oct 2020 17:27:48 +0300 Subject: Render display lat/lng --- iss/server.py | 5 +++-- iss/templates/passes.html | 2 +- iss/tests/test_utils.py | 18 +++++++++++++++++- 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 %}
-

Passes for {{ location.lat }}, {{ location.lng }}

+

Passes for {{ location.dlat }}, {{ location.dlng }}

Times localized for [[ tz.name ]] (UTC[[ tz.offset ]])

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", -- cgit v1.3.1