diff options
author | Daniel Schadt <kingdread@gmx.de> | 2022-07-24 22:06:45 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2022-07-24 22:06:45 +0200 |
commit | f1b1c53e1d5d6eabbe044bbefeabc2e171fda0cf (patch) | |
tree | 8158d4337a6fdd8206f84553f15a3aa64d3d5b6a | |
parent | 81aafb1e89ac1c8495d0e8c75cf5ca46c9d0cbe9 (diff) | |
download | fietsboek-f1b1c53e1d5d6eabbe044bbefeabc2e171fda0cf.tar.gz fietsboek-f1b1c53e1d5d6eabbe044bbefeabc2e171fda0cf.tar.bz2 fietsboek-f1b1c53e1d5d6eabbe044bbefeabc2e171fda0cf.zip |
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".
-rw-r--r-- | fietsboek/util.py | 14 |
1 files 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), } |