From 0359ec1b44d09defd397e943c0b6d4a394ec6be8 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 17 Jun 2021 14:10:09 -0700 Subject: [PATCH] Much improved display of derived data on /location/x, refs #650 --- vaccinate/core/models.py | 37 +++++++++++++++----------- vaccinate/core/views.py | 7 +++-- vaccinate/templates/location.html | 44 ++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 21 deletions(-) diff --git a/vaccinate/core/models.py b/vaccinate/core/models.py index 43403f7..ac76a16 100644 --- a/vaccinate/core/models.py +++ b/vaccinate/core/models.py @@ -553,21 +553,9 @@ def derive_availability_and_inventory( source_location_to_use_for_vaccines_offered = None if source_location_to_use_for_vaccines_offered: - inventory = source_location_to_use_for_vaccines_offered.import_json[ - "inventory" - ] - inventory_mapping = { - "moderna": "Moderna", - "pfizer_biontech": "Pfizer", - "johnson_johnson_janssen": "Johnson & Johnson", - "oxford_astrazeneca": "Astrazeneca", - } - in_stock = [ - stock["vaccine"] - for stock in inventory - if stock.get("supply_level") != "out_of_stock" - ] - vaccines_offered = list(sorted([inventory_mapping[v] for v in in_stock])) + vaccines_offered = ( + source_location_to_use_for_vaccines_offered.vaccines_offered + ) vaccines_offered_provenance_source_location = ( source_location_to_use_for_vaccines_offered ) @@ -1613,6 +1601,25 @@ def pydantic_convert(cls, id: str) -> SourceLocation: raise ValueError("SourceLocation '{}' does not exist".format(id)) return obj + @property + def vaccines_offered(self): + try: + inventory = self.import_json["inventory"] + except KeyError: + return None + inventory_mapping = { + "moderna": "Moderna", + "pfizer_biontech": "Pfizer", + "johnson_johnson_janssen": "Johnson & Johnson", + "oxford_astrazeneca": "Astrazeneca", + } + in_stock = [ + stock["vaccine"] + for stock in inventory + if stock.get("supply_level") != "out_of_stock" + ] + return list(sorted([inventory_mapping[v] for v in in_stock])) + class Meta: db_table = "source_location" indexes = [models.Index(fields=["matched_location"])] diff --git a/vaccinate/core/views.py b/vaccinate/core/views.py index 5fff6a2..a92f019 100644 --- a/vaccinate/core/views.py +++ b/vaccinate/core/views.py @@ -153,16 +153,19 @@ def location(request, public_id): ) vaccines_offered_timeline.sort(key=lambda r: r["when"], reverse=True) + derived = location.derive_availability_and_inventory() + return render( request, "location.html", { "location": location, - "derived_availability_and_inventory": json.dumps( - location.derive_availability_and_inventory()._asdict(), + "derived_raw": json.dumps( + derived._asdict(), indent=4, default=lambda v: v.isoformat() if hasattr(v, "isoformat") else repr(v), ), + "derived": derived, "availability_timeline": availability_timeline, "vaccines_offered_timeline": vaccines_offered_timeline, "source_locations": location.matched_source_locations.order_by( diff --git a/vaccinate/templates/location.html b/vaccinate/templates/location.html index 0692e55..74cc7bf 100644 --- a/vaccinate/templates/location.html +++ b/vaccinate/templates/location.html @@ -7,14 +7,50 @@

{{ location }}

{{ location.public_id }} - {{ location.full_address }}

Find another location

+

Derived availability and inventory

We have logic that derives vaccine inventory and availability based on a combination of recently filed reports and recently scraped source locations. The location.derived_availability_and_inventory() method for this location currently returns:

-
{{ derived_availability_and_inventory }}
+

Vaccines offered: {{ derived.vaccines_offered|default:"-" }}

+

According to + {% if derived.vaccines_offered_provenance_report %}report: {{ derived.vaccines_offered_provenance_report }} + {% else %} + source location: {{ derived.vaccines_offered_provenance_source_location }} + {% endif %} + on {{ derived.vaccines_offered_last_updated_at }} + {% if derived.vaccines_offered_provenance_report and derived.most_recent_source_location_on_vaccines_offered %} +

Most recent source location with an opinion was {{ derived.most_recent_source_location_on_vaccines_offered }} on {{ derived.most_recent_source_location_on_vaccines_offered.last_imported_at }} which said {{ derived.most_recent_source_location_on_vaccines_offered.vaccines_offered }}
+ {% endif %} + {% if derived.vaccines_offered_provenance_source_location and derived.most_recent_report_on_vaccines_offered %} +
Most recent report with an opinion was {{ derived.most_recent_report_on_vaccines_offered }} on {{ derived.most_recent_report_on_vaccines_offered.created_at }} which said {{ derived.most_recent_report_on_vaccines_offered.vaccines_offered }}
+ {% endif %} +

+ +

+ Accepts appointments:{% if derived.accepts_appointments %}True{% else %} {{ derived.accepts_appointments }}{% endif %}, + Accepts walk-ins:{% if derived.accepts_walkins %}True{% else %} {{ derived.accepts_walkins }}{% endif %}

+

According to + {% if derived.appointments_walkins_provenance_report %}report: {{ derived.appointments_walkins_provenance_report }} [{{ derived.appointments_walkins_provenance_report.availability }}] + {% else %} + source location: {{ derived.appointments_walkins_provenance_source_location }} which said {{ derived.appointments_walkins_provenance_source_location.import_json.availability }} + {% endif %} + on {{ derived.appointments_walkins_last_updated_at }} + {% if derived.appointments_walkins_provenance_report and derived.most_recent_source_location_on_availability %} +

Most recent source location with an opinion was {{ derived.most_recent_source_location_on_availability }} on {{ derived.most_recent_source_location_on_availability.last_imported_at }} which said {{ derived.most_recent_source_location_on_availability.import_json.availability }}
+ {% endif %} + {% if derived.vaccines_offered_provenance_source_location and derived.most_recent_report_on_availability %} +
Most recent report with an opinion was {{ derived.most_recent_report_on_availability }} on {{ derived.most_recent_report_on_availability.created_at }} which said {{ derived.most_recent_report_on_availability.availability }}
+ {% endif %} +

+ +
Raw derived data +
{{ derived_raw }}
+
+ {% if availability_timeline %} -

Availability:

+

Availability timeline:

{% for title, content in api_previews %}

{{ title }}

{{ content }}