Self hosted FLOSS fitness/workout, nutrition and weight tracker
Fix bug with the image download from wger
Roland Geiderecdafbfccdf1
diff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.pyindex cfc2b62db..6faaaea2a 100644--- a/wger/exercises/tests/test_sync.py+++ b/wger/exercises/tests/test_sync.py@@ -42,6 +42,7 @@ class MockLanguageResponse:self.status_code = 200self.content = b'1234'+ # yapf: disable@staticmethoddef json():return {@@ -53,25 +54,30 @@ class MockLanguageResponse:"id": 1,"short_name": "de","full_name": "Daitsch"- }, {+ },+ {"id": 2,"short_name": "en","full_name": "English"- }, {+ },+ {"id": 3,"short_name": "fr","full_name": "Français"- }, {+ },+ {"id": 4,"short_name": "es","full_name": "Español"- }, {+ },+ {"id": 19,"short_name": "eo","full_name": "Esperanto"}]}+ # yapf: enableclass MockCategoryResponse:@@ -80,6 +86,7 @@ class MockCategoryResponse:self.status_code = 200self.content = b'1234'+ # yapf: disable@staticmethoddef json():return {@@ -90,24 +97,30 @@ class MockCategoryResponse:{"id": 1,"name": "A cooler, swaggier category"- }, {+ },+ {"id": 2,"name": "Another category"- }, {+ },+ {"id": 3,"name": "Yet another category"- }, {+ },+ {"id": 4,"name": "Calves"- }, {+ },+ {"id": 5,"name": "Cardio"- }, {+ },+ {"id": 16,"name": "Chest"}]}+ # yapf: enableclass MockMuscleResponse:@@ -116,6 +129,7 @@ class MockMuscleResponse:self.status_code = 200self.content = b''+ # yapf: disable@staticmethoddef json():return {@@ -157,6 +171,7 @@ class MockMuscleResponse:},]}+ # yapf: enableclass MockEquipmentResponse:@@ -165,6 +180,7 @@ class MockEquipmentResponse:self.status_code = 200self.content = b''+ # yapf: disable@staticmethoddef json():return {@@ -190,6 +206,7 @@ class MockEquipmentResponse:},]}+ # yapf: enableclass MockDeletionLogResponse:@@ -198,6 +215,7 @@ class MockDeletionLogResponse:self.status_code = 200self.content = b''+ # yapf: disable@staticmethoddef json():return {@@ -249,6 +267,7 @@ class MockDeletionLogResponse:},]}+ # yapf: enableclass MockExerciseResponse:@@ -257,6 +276,7 @@ class MockExerciseResponse:self.status_code = 200self.content = b''+ # yapf: disable@staticmethoddef json():return {@@ -454,6 +474,7 @@ class MockExerciseResponse:},]}+ # yapf: enableclass TestSyncMethods(WgerTestCase):diff --git a/wger/nutrition/api/endpoints.py b/wger/nutrition/api/endpoints.pyindex 292cddcee..6f225e199 100644--- a/wger/nutrition/api/endpoints.py+++ b/wger/nutrition/api/endpoints.py@@ -1 +1 @@-IMAGE_ENDPOINT = "{0}/api/v2/ingredient-image/"+IMAGE_ENDPOINT = "ingredient-image"diff --git a/wger/nutrition/sync.py b/wger/nutrition/sync.pyindex 97e22584d..25a916c38 100644--- a/wger/nutrition/sync.py+++ b/wger/nutrition/sync.py@@ -36,6 +36,7 @@ from wger.utils.constants import (DOWNLOAD_INGREDIENT_WGER,)from wger.utils.requests import wger_headers+from wger.utils.url import make_urilogger = logging.getLogger(__name__)@@ -67,17 +68,21 @@ def fetch_ingredient_image(pk: int):def fetch_image_from_wger_instance(ingredient):- url = f"{IMAGE_ENDPOINT.format(settings.WGER_SETTINGS['WGER_INSTANCE'])}{ingredient.pk}/"+ url = make_uri(IMAGE_ENDPOINT, query={'uuid': ingredient.uuid})logger.info(f'Trying to fetch image from WGER for {ingredient.name} (UUID: {ingredient.uuid})')result = requests.get(url, headers=wger_headers()).json()- image_uuid = result['uuid']+ if result['count'] == 0:+ logger.info('No ingredient matches UUID in the remote server')++ image_data = result['results'][0]+ image_uuid = image_data['uuid']try:Image.objects.get(uuid=image_uuid)logger.info('image already present locally, skipping...')returnexcept Image.DoesNotExist:- retrieved_image = requests.get(result['image'], headers=wger_headers())- Image.from_json(ingredient, retrieved_image, result)+ retrieved_image = requests.get(image_data['image'], headers=wger_headers())+ Image.from_json(ingredient, retrieved_image, image_data)def fetch_image_from_off(ingredient):@@ -133,11 +138,12 @@ def download_ingredient_images(remote_url=settings.WGER_SETTINGS['WGER_INSTANCE'],style_fn=lambda x: x,):- headers = wger_headers()- # Get all imagespage = 1all_images_processed = False- result = requests.get(IMAGE_ENDPOINT.format(remote_url), headers=headers).json()+ headers = wger_headers()+ url = make_uri(IMAGE_ENDPOINT, server_url=remote_url)+ result = requests.get(url, headers=headers).json()+print_fn('*** Processing images ***')while not all_images_processed:print_fn('')diff --git a/wger/nutrition/tests/test_tasks.py b/wger/nutrition/tests/test_tasks.pyindex 136bd2408..357f1b780 100644--- a/wger/nutrition/tests/test_tasks.py+++ b/wger/nutrition/tests/test_tasks.py@@ -42,7 +42,7 @@ class MockOffResponse:return {"product": {'image_front_url':- 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',+ 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg','images': {'front_en': {'imgid': '12345',@@ -69,7 +69,7 @@ class MockWgerApiResponse:"ingredient_id": "12345","ingredient_uuid": "e9baa8bd-84fc-4756-8d90-5b9739b06cf8","image": "http://localhost:8000/media/ingredients/e9baa8bd-84fc-4756-8d90-5b9739b06cf8"- "/188324b5-587f-42d7-9abc-d2ca64c73d45.jpg",+ "/188324b5-587f-42d7-9abc-d2ca64c73d45.jpg","last_update": "2023-03-15T23:20:10.969369+01:00","size": 20179,"source_url": "",diff --git a/wger/utils/tests/test_url.py b/wger/utils/tests/test_url.pynew file mode 100644index 000000000..f78ac8ba4--- /dev/null+++ b/wger/utils/tests/test_url.py@@ -0,0 +1,66 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# Standard Library+import unittest++# wger+from wger.utils.url import make_uri+++class TestMakeUri(unittest.TestCase):++ def test_make_uri(self):+ # Test default server_url+ self.assertEqual(+ make_uri("test"),+ "https://wger.de/api/v2/test/",+ )++ # Test custom server_url+ self.assertEqual(+ make_uri("test", server_url="https://api.example.com"),+ "https://api.example.com/api/v2/test/"+ )++ # Test with id+ self.assertEqual(+ make_uri("test", id=123),+ "https://wger.de/api/v2/test/123/",+ )++ # Test with object_method+ self.assertEqual(+ make_uri("test", object_method="create"),+ "https://wger.de/api/v2/test/create/",+ )++ # Test with query parameters+ self.assertEqual(+ make_uri("endpoint", query={+ "key1": "value1",+ "key2": "value2"+ }),+ "https://wger.de/api/v2/endpoint/?key1=value1&key2=value2",+ )++ # Test with all parameters+ self.assertEqual(+ make_uri("test", id=123, object_method="create", query={"key1": "value1"}),+ "https://wger.de/api/v2/test/123/create/?key1=value1",+ )+++if __name__ == "__main__":+ unittest.main()diff --git a/wger/utils/url.py b/wger/utils/url.pynew file mode 100644index 000000000..8c37a3b24--- /dev/null+++ b/wger/utils/url.py@@ -0,0 +1,57 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# Standard Library+from typing import (+ Dict,+ Optional,+)+from urllib.parse import (+ urlparse,+ urlunparse,+)++# Django+from django.conf import settings+++def make_uri(+ path: str,+ id: Optional[int] = None,+ object_method: Optional[str] = None,+ query: Optional[Dict[str, any]] = None,+ server_url: str = settings.WGER_SETTINGS['WGER_INSTANCE']+):+ uri_server = urlparse(server_url)+ query = query or {}+ path_list = [uri_server.path, 'api', 'v2', path]++ if id is not None:+ path_list.append(str(id))++ if object_method is not None:+ path_list.append(object_method)++ uri = urlunparse(+ (+ uri_server.scheme,+ uri_server.netloc,+ '/'.join(path_list) + '/',+ '',+ '&'.join([f"{key}={value}" for key, value in query.items()]),+ '',+ )+ )++ return uri