From b65d79cb633a0d83ef696f5efda70d0e8b2c1e96 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 1 Jul 2024 22:27:19 +0300 Subject: [PATCH] Add filters for space launches --- agenda/thespacedevs.py | 7 +++++-- templates/launches.html | 37 +++++++++++++++++++++++++++++++++++-- web_view.py | 28 ++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/agenda/thespacedevs.py b/agenda/thespacedevs.py index 15ad455..3192448 100644 --- a/agenda/thespacedevs.py +++ b/agenda/thespacedevs.py @@ -136,7 +136,7 @@ def summarize_launch(launch: Launch) -> Summary: "launch_provider": launch_provider, "launch_provider_abbrev": launch_provider_abbrev, "launch_provider_type": get_nested(launch, ["launch_service_provider", "type"]), - "rocket": launch["rocket"]["configuration"]["full_name"], + "rocket": launch["rocket"]["configuration"], "mission": launch.get("mission"), "mission_name": get_nested(launch, ["mission", "name"]), "pad_name": launch["pad"]["name"], @@ -174,7 +174,10 @@ def get_launches( existing.sort(reverse=True) if refresh or not existing or (now - existing[0][0]).seconds > ttl: - return next_launch_api(rocket_dir, limit=limit) + try: + return next_launch_api(rocket_dir, limit=limit) + except Exception: + pass # fallback to cached version f = existing[0][1] diff --git a/templates/launches.html b/templates/launches.html index a8baede..1756424 100644 --- a/templates/launches.html +++ b/templates/launches.html @@ -6,7 +6,40 @@

Space launches

- {% for launch in rockets %} +

Filters

+ +

Mission type: + + {% if request.args.type %}🗙{% endif %} + + {% for t in mission_types | sort %} + {% if t == request.args.type %} + {{ t }} + {% else %} + + {{ t }} + + {% endif %} + {% if not loop.last %} | {% endif %} + {% endfor %} +

+ +

Vehicle: + {% if request.args.rocket %}🗙{% endif %} + + {% for r in rockets | sort %} + {% if r == request.args.rockets %} + {{ r }} + {% else %} + + {{ r }} + + {% endif %} + {% if not loop.last %} | {% endif %} + {% endfor %} +

+ + {% for launch in launches %} {% set highlight =" bg-primary-subtle" if launch.slug in config.FOLLOW_LAUNCHES else "" %} {% set country = get_country(launch.country_code) %}
@@ -24,7 +57,7 @@
{{ country.flag }} - {{ launch.rocket }} + {{ launch.rocket.full_name }} – {{launch.mission.name }} – diff --git a/web_view.py b/web_view.py index 2cd1e80..c3e663a 100755 --- a/web_view.py +++ b/web_view.py @@ -145,10 +145,34 @@ def launch_list() -> str: now = datetime.now() data_dir = app.config["DATA_DIR"] rocket_dir = os.path.join(data_dir, "thespacedevs") - rockets = agenda.thespacedevs.get_launches(rocket_dir, limit=100) + launches = agenda.thespacedevs.get_launches(rocket_dir, limit=100) + + mission_type_filter = flask.request.args.get("type") + rocket_filter = flask.request.args.get("rocket") + + mission_types = { + launch["mission"]["type"] for launch in launches if launch["mission"] + } + + rockets = {launch["rocket"]["full_name"] for launch in launches} + + launches = [ + launch + for launch in launches + if ( + not mission_type_filter + or (launch["mission"] and launch["mission"]["type"] == mission_type_filter) + ) + and (not rocket_filter or launch["rocket"]["full_name"] == rocket_filter) + ] return flask.render_template( - "launches.html", rockets=rockets, now=now, get_country=agenda.get_country + "launches.html", + launches=launches, + rockets=rockets, + now=now, + get_country=agenda.get_country, + mission_types=mission_types, )