API - allow keeping default tile server for static map - #83

This commit is contained in:
Sam 2021-07-16 19:17:17 +02:00
parent cbcd67b3a1
commit 3c4718cc27
5 changed files with 47 additions and 7 deletions

View File

@ -25,4 +25,5 @@ export REDIS_URL=
# Activities
export TILE_SERVER_URL=
export MAP_ATTRIBUTION=
#export DEFAULT_STATICMAP=False
export WEATHER_API_KEY=

View File

@ -42,6 +42,9 @@ class BaseConfig:
'target="_blank" rel="noopener noreferrer">OpenStreetMap</a>'
' contributors',
),
'DEFAULT_STATICMAP': (
os.environ.get('DEFAULT_STATICMAP', 'False') == 'True'
),
}

View File

@ -75,6 +75,14 @@ def app(monkeypatch: pytest.MonkeyPatch) -> Generator:
monkeypatch.delenv('TILE_SERVER_URL')
if os.getenv('MAP_ATTRIBUTION'):
monkeypatch.delenv('MAP_ATTRIBUTION')
if os.getenv('DEFAULT_STATICMAP'):
monkeypatch.delenv('DEFAULT_STATICMAP')
yield from get_app(with_config=True)
@pytest.fixture
def app_default_static_map(monkeypatch: pytest.MonkeyPatch) -> Generator:
monkeypatch.setenv('DEFAULT_STATICMAP', 'True')
yield from get_app(with_config=True)

View File

@ -360,6 +360,37 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
in call_args[0]
)
def test_it_calls_default_tile_server_for_static_map(
self,
app_default_static_map: Flask,
user_1: User,
sport_1_cycling: Sport,
gpx_file: str,
static_map_get_mock: Mock,
) -> None:
client, auth_token = self.get_test_client_and_auth_token(
app_default_static_map
)
client.post(
'/api/workouts',
data=dict(
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
data='{"sport_id": 1}',
),
headers=dict(
content_type='multipart/form-data',
Authorization=f'Bearer {auth_token}',
),
)
call_args = self.get_args(static_map_get_mock.call_args)
assert (
app_default_static_map.config['TILE_SERVER']['URL']
.replace('{s}.', '')
.replace('/{z}/{x}/{y}.png', '')
not in call_args[0]
)
def test_it_returns_500_if_gpx_file_has_not_tracks(
self,
app: Flask,

View File

@ -265,13 +265,10 @@ def generate_map(map_filepath: str, map_data: List) -> None:
"""
Generate and save map image from map data
"""
m = StaticMap(
400,
225,
10,
url_template=current_app.config['TILE_SERVER']['URL'].replace(
m = StaticMap(400, 225, 10)
if not current_app.config['TILE_SERVER']['DEFAULT_STATICMAP']:
m.url_template = current_app.config['TILE_SERVER']['URL'].replace(
'{s}.', ''
),
)
line = Line(map_data, '#3388FF', 4)
m.add_line(line)