From e25b38e9fd5dd1b0f2633a8214a89f9101e99393 Mon Sep 17 00:00:00 2001 From: tsauerwein Date: Wed, 8 Feb 2017 16:57:09 +0100 Subject: [PATCH] Handle route/outings versions without activity --- c2corg_api/tests/views/test_outing.py | 10 ++++++++++ c2corg_api/tests/views/test_route.py | 10 ++++++++++ c2corg_api/views/document_schemas.py | 13 +++++++++++++ 3 files changed, 33 insertions(+) diff --git a/c2corg_api/tests/views/test_outing.py b/c2corg_api/tests/views/test_outing.py index 3e4d3901f..5295d4eda 100644 --- a/c2corg_api/tests/views/test_outing.py +++ b/c2corg_api/tests/views/test_outing.py @@ -171,6 +171,16 @@ def test_get_edit(self): def test_get_version(self): self.get_version(self.outing, self.outing_version) + def test_get_version_without_activity(self): + """ Tests that old outings versions without activity include the fields + of all activities. + """ + self.outing_version.document_archive.activities = [] + self.session.flush() + body = self.get_version(self.outing, self.outing_version) + locale = body['document']['locales'][0] + self.assertIn('title', locale) + def test_get_lang(self): self.get_lang(self.outing) diff --git a/c2corg_api/tests/views/test_route.py b/c2corg_api/tests/views/test_route.py index 1abb9b6ae..da2b9eba3 100644 --- a/c2corg_api/tests/views/test_route.py +++ b/c2corg_api/tests/views/test_route.py @@ -150,6 +150,16 @@ def test_get(self): def test_get_version(self): self.get_version(self.route, self.route_version) + def test_get_version_without_activity(self): + """ Tests that old route versions without activity include the fields + of all activities. + """ + self.route_version.document_archive.activities = [] + self.session.flush() + body = self.get_version(self.route, self.route_version) + locale = body['document']['locales'][0] + self.assertIn('title', locale) + def test_get_lang(self): body = self.get_lang(self.route) diff --git a/c2corg_api/views/document_schemas.py b/c2corg_api/views/document_schemas.py index 0d5be7fa2..bdaaf8990 100644 --- a/c2corg_api/views/document_schemas.py +++ b/c2corg_api/views/document_schemas.py @@ -15,6 +15,7 @@ schema_listing_user_profile from c2corg_api.models.waypoint import WAYPOINT_TYPE, schema_waypoint, Waypoint from c2corg_api.views import set_author +from c2corg_common import attributes from c2corg_common.fields_outing import fields_outing from c2corg_common.fields_xreport import fields_xreport from c2corg_common.fields_route import fields_route @@ -149,6 +150,12 @@ def adapt_outing_schema_for_activities(activities, field_list_type): """Get the schema for a set of activities. `field_list_type` should be either "fields" or "listing". """ + if not activities: + # `activities` is a required field, so it should not be empty. + # but old versions might have no activities, so we include the fields + # for all activities in that case. + activities = attributes.activities + fields = get_all_fields(fields_outing, activities, field_list_type) return restrict_schema(schema_outing, fields) @@ -178,6 +185,12 @@ def adapt_route_schema_for_activities(activities, field_list_type): """Get the schema for a set of activities. `field_list_type` should be either "fields" or "listing". """ + if not activities: + # `activities` is a required field, so it should not be empty. + # but old versions might have no activities, so we include the fields + # for all activities in that case. + activities = [a for a in attributes.activities if a != 'paragliding'] + fields = get_all_fields(fields_route, activities, field_list_type) return restrict_schema(schema_route, fields)