From f1b1c53e1d5d6eabbe044bbefeabc2e171fda0cf Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 24 Jul 2022 22:06:45 +0200 Subject: don't crash tour_metadata with missing information Some tracks (especially the "premade" ones) lack the time and speed information. We just assume some random values in those cases, which is better than crashing the site. In the future, this is gonna be important once we implement "template tracks". --- fietsboek/util.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/fietsboek/util.py b/fietsboek/util.py index bab2074..aff41fe 100644 --- a/fietsboek/util.py +++ b/fietsboek/util.py @@ -27,6 +27,10 @@ ALLOWED_TAGS = (bleach.sanitizer.ALLOWED_TAGS + ALLOWED_ATTRIBUTES = dict(bleach.sanitizer.ALLOWED_ATTRIBUTES) ALLOWED_ATTRIBUTES['img'] = ['alt', 'src'] +# Arbitrarily chosen, just make sure they are representable +DEFAULT_START_TIME = datetime.datetime(1977, 5, 25, 8, 0) +DEFAULT_END_TIME = datetime.datetime(1985, 7, 3, 8, 0) + _filename_ascii_strip_re = re.compile(r"[^A-Za-z0-9_.-]") _windows_device_files = ( @@ -165,6 +169,10 @@ def tour_metadata(gpx_data): uphill, downhill = gpx.get_uphill_downhill() moving_data = gpx.get_moving_data() time_bounds = gpx.get_time_bounds() + try: + avg_speed = moving_data.moving_distance / moving_data.moving_time + except ZeroDivisionError: + avg_speed = 0.0 return { 'length': gpx.length_3d(), 'uphill': uphill, @@ -172,9 +180,9 @@ def tour_metadata(gpx_data): 'moving_time': moving_data.moving_time, 'stopped_time': moving_data.stopped_time, 'max_speed': moving_data.max_speed, - 'avg_speed': moving_data.moving_distance / moving_data.moving_time, - 'start_time': time_bounds.start_time.astimezone(timezone), - 'end_time': time_bounds.end_time.astimezone(timezone), + 'avg_speed': avg_speed, + 'start_time': (time_bounds.start_time or DEFAULT_START_TIME).astimezone(timezone), + 'end_time': (time_bounds.end_time or DEFAULT_END_TIME).astimezone(timezone), } -- cgit v1.2.3