Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Set the "last_image_check" when downloading images for ingredients

Roland Geiderdc3f57493c15
diff --git a/wger/exercises/tests/test_exercise_translation.py b/wger/exercises/tests/test_exercise_translation.py
index e7c70ab07..a8ae8c1bc 100644
--- a/wger/exercises/tests/test_exercise_translation.py
+++ b/wger/exercises/tests/test_exercise_translation.py
@@ -102,7 +102,7 @@ class ExercisesTestCase(WgerTestCase):
translation.exercise.muscles_secondary.add(Muscle.objects.get(pk=2))
translation.save()
- translation = Translation.objects.get(pk=2)
+ translation.refresh_from_db()
self.assertEqual(len(translation.history.all()), 1)
diff --git a/wger/nutrition/sync.py b/wger/nutrition/sync.py
index 4a581e60a..a30038d18 100644
--- a/wger/nutrition/sync.py
+++ b/wger/nutrition/sync.py
@@ -136,11 +136,19 @@ def fetch_image_from_wger_instance(ingredient):
def fetch_image_from_off(ingredient: Ingredient):
"""
- See
- - https://openfoodfacts.github.io/openfoodfacts-server/api/how-to-download-images/
- - https://openfoodfacts.github.io/openfoodfacts-server/api/ref-v2/
+ Tries to download an image from Open Food Facts for the given ingredient.
+
+ See https://github.com/openfoodfacts/openfoodfacts-python
"""
+
logger.info(f'Trying to fetch image from OFF for "{ingredient.name}" ({ingredient.uuid})')
+
+ # We always update the last check time, no matter if we found an image or there were
+ # errors in the response (keys missing, etc) since in any case we don't want to retry
+ # too often.
+ ingredient.last_image_check = timezone.now()
+ ingredient.save()
+
off_api = API(
user_agent=wger_user_agent(),
country=Country.world,
@@ -158,7 +166,7 @@ def fetch_image_from_off(ingredient: Ingredient):
if not image_url:
logger.info('Product data has no "image_front_url" key')
return
- image_data = product_data['images']
+ image_data = product_data.get('images')
if not image_data:
logger.info('Product data has no "images" key')
return
@@ -201,8 +209,6 @@ def fetch_image_from_off(ingredient: Ingredient):
except IntegrityError:
logger.debug('Ingredient has already an image, skipping...')
return
- ingredient.last_image_check = timezone.now()
- ingredient.save()
logger.info('Image successfully saved')
diff --git a/wger/nutrition/tests/test_tasks.py b/wger/nutrition/tests/test_tasks.py
index e72dd9e78..7534cebf6 100644
--- a/wger/nutrition/tests/test_tasks.py
+++ b/wger/nutrition/tests/test_tasks.py
@@ -15,7 +15,11 @@
# Standard Library
import datetime
-from unittest.mock import patch
+from unittest.mock import (
+ ANY,
+ MagicMock,
+ patch,
+)
# Django
from django.utils import timezone
@@ -44,6 +48,15 @@ mock_off_response = {
},
}
+mock_off_response_no_front_url = {
+ 'images': {
+ 'front_en': {
+ 'imgid': '12345',
+ },
+ '12345': {'uploader': 'Mr Foobar'},
+ },
+}
+
class MockOffImageResponse:
image_bytes = [0, 1, 2, 3]
@@ -89,9 +102,16 @@ class FetchIngredientImageTestCase(WgerTestCase):
Test fetching an ingredient image
"""
- @patch('openfoodfacts.api.ProductResource.get')
- @patch.object(logger, 'info')
- def test_source(self, mock_logger, mock_request):
+ @patch(
+ 'openfoodfacts.api.ProductResource.get',
+ autospec=True,
+ )
+ @patch.object(
+ logger,
+ 'info',
+ autospec=True,
+ )
+ def test_source(self, mock_logger: MagicMock, mock_request: MagicMock):
"""
Test that sources other than OFF are ignored
"""
@@ -103,11 +123,11 @@ class FetchIngredientImageTestCase(WgerTestCase):
result = fetch_ingredient_image(1)
mock_logger.assert_not_called()
mock_request.assert_not_called()
- self.assertEqual(result, None)
+ self.assertIsNone(result)
- @patch('openfoodfacts.api.ProductResource.get')
- @patch.object(logger, 'info')
- def test_download_off_setting(self, mock_logger, mock_request):
+ @patch('openfoodfacts.api.ProductResource.get', autospec=True)
+ @patch.object(logger, 'info', autospec=True)
+ def test_download_off_setting(self, mock_logger: MagicMock, mock_request: MagicMock):
"""
Test that no images are fetched if the appropriate setting is not set
"""
@@ -121,15 +141,22 @@ class FetchIngredientImageTestCase(WgerTestCase):
mock_request.assert_not_called()
self.assertEqual(result, None)
- @patch('openfoodfacts.api.ProductResource.get')
- @patch('wger.nutrition.models.Image.from_json')
- @patch.object(logger, 'info')
- def test_last_new_image_date(self, mock_logger, mock_from_json, mock_request):
+ @patch('openfoodfacts.api.ProductResource.get', autospec=True)
+ @patch('wger.nutrition.models.Image.from_json', autospec=True)
+ @patch.object(logger, 'info', autospec=True)
+ def test_last_new_image_date(
+ self,
+ mock_logger: MagicMock,
+ mock_from_json: MagicMock,
+ mock_request: MagicMock,
+ ):
"""
Test that no images are fetched if we already checked recently
"""
+ last_checked = timezone.now() - datetime.timedelta(days=1)
+
ingredient = Ingredient.objects.get(pk=1)
- ingredient.last_image_check = timezone.now() - datetime.timedelta(days=1)
+ ingredient.last_image_check = last_checked
ingredient.save()
with self.settings(
@@ -139,22 +166,27 @@ class FetchIngredientImageTestCase(WgerTestCase):
'INGREDIENT_IMAGE_CHECK_INTERVAL': datetime.timedelta(days=5),
},
):
+ # Act
result = fetch_ingredient_image(1)
+ ingredient.refresh_from_db()
+
+ # Assert
mock_from_json.assert_not_called()
mock_logger.assert_not_called()
mock_request.assert_not_called()
- self.assertEqual(result, None)
+ self.assertIsNone(result)
+ self.assertEqual(ingredient.last_image_check , last_checked)
- @patch('wger.nutrition.sync.download_image', return_value=MockOffImageResponse)
- @patch('openfoodfacts.api.ProductResource.get', return_value=mock_off_response)
- @patch('wger.nutrition.models.Image.from_json')
- @patch.object(logger, 'info')
- def test_last_old_image_date(
+ @patch('wger.nutrition.sync.download_image', return_value=MockOffImageResponse, autospec=True)
+ @patch('openfoodfacts.api.ProductResource.get', return_value=mock_off_response, autospec=True)
+ @patch('wger.nutrition.models.Image.from_json', autospec=True)
+ @patch.object(logger, 'info', autospec=True)
+ def test_old_last_image_check_date(
self,
- mock_logger,
- mock_from_json,
- mock_request,
- mock_download_image,
+ mock_logger: MagicMock,
+ mock_from_json: MagicMock,
+ mock_request: MagicMock,
+ mock_download_image: MagicMock,
):
"""
Test that images are fetched if we checked a long time ago
@@ -170,7 +202,11 @@ class FetchIngredientImageTestCase(WgerTestCase):
'INGREDIENT_IMAGE_CHECK_INTERVAL': datetime.timedelta(days=5),
},
):
+ # Act
result = fetch_ingredient_image(1)
+ ingredient.refresh_from_db()
+
+ # Assert
mock_from_json.assert_called()
mock_download_image.assert_called_with(
'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',
@@ -178,20 +214,68 @@ class FetchIngredientImageTestCase(WgerTestCase):
)
mock_logger.assert_any_call('Fetching image for ingredient 1')
mock_request.assert_called_with(
- '1234567890987654321', fields=['images', 'image_front_url']
+ ANY,
+ '1234567890987654321',
+ fields=['images', 'image_front_url'],
+ )
+
+ self.assertIsNone(result)
+ self.assertAlmostEqual(
+ ingredient.last_image_check, timezone.now(), delta=datetime.timedelta(seconds=1)
)
- self.assertEqual(result, None)
- @patch('wger.nutrition.sync.download_image', return_value=MockOffImageResponse)
- @patch('openfoodfacts.api.ProductResource.get', return_value=mock_off_response)
- @patch('wger.nutrition.models.Image.from_json')
- @patch.object(logger, 'info')
+ @patch('wger.nutrition.sync.download_image', autospec=True, return_value=MockOffImageResponse)
+ @patch(
+ 'openfoodfacts.api.ProductResource.get',
+ autospec=True,
+ return_value=mock_off_response_no_front_url,
+ )
+ @patch('wger.nutrition.models.Image.from_json', autospec=True)
+ @patch.object(logger, 'info', autospec=True)
+ def test_update_handle_no_front_url_key(
+ self,
+ mock_logger: MagicMock,
+ mock_from_json: MagicMock,
+ mock_request: MagicMock,
+ mock_download_image: MagicMock,
+ ):
+ """
+ Properly handles case where OFF response does not contain the 'image_front_url' key
+ """
+ ingredient = Ingredient.objects.get(pk=1)
+ ingredient.last_image_check = None
+ ingredient.save()
+
+ with self.settings(
+ TESTING=False,
+ WGER_SETTINGS={'DOWNLOAD_INGREDIENTS_FROM': DOWNLOAD_INGREDIENT_OFF},
+ ):
+ # Act
+ result = fetch_ingredient_image(1)
+ ingredient.refresh_from_db()
+
+ # Assert
+ mock_logger.assert_called()
+ mock_from_json.assert_not_called()
+ mock_download_image.assert_not_called()
+ mock_request.assert_called()
+
+ self.assertIsNone(result)
+ self.assertAlmostEqual(
+ ingredient.last_image_check, timezone.now(), delta=datetime.timedelta(seconds=1)
+ )
+
+
+ @patch('wger.nutrition.sync.download_image', return_value=MockOffImageResponse, autospec=True)
+ @patch('openfoodfacts.api.ProductResource.get', return_value=mock_off_response, autospec=True)
+ @patch('wger.nutrition.models.Image.from_json', autospec=True)
+ @patch.object(logger, 'info', autospec=True)
def test_download_ingredient_off(
self,
- mock_logger,
- mock_from_json,
- mock_request,
- mock_download_image,
+ mock_logger: MagicMock,
+ mock_from_json: MagicMock,
+ mock_request: MagicMock,
+ mock_download_image: MagicMock,
):
"""
Test that the image is correctly downloaded
@@ -205,10 +289,13 @@ class FetchIngredientImageTestCase(WgerTestCase):
WGER_SETTINGS={'DOWNLOAD_INGREDIENTS_FROM': DOWNLOAD_INGREDIENT_OFF},
TESTING=False,
):
+ # Act
result = fetch_ingredient_image(1)
+ # Assert
# print(mock_request.mock_calls)
mock_request.assert_called_with(
+ ANY,
'1234567890987654321',
fields=['images', 'image_front_url'],
)
@@ -224,12 +311,17 @@ class FetchIngredientImageTestCase(WgerTestCase):
mock_logger.assert_any_call('Image successfully saved')
mock_from_json.assert_called()
- self.assertEqual(result, None)
+ self.assertIsNone(result)
- @patch('requests.get', return_value=MockWgerApiResponse())
- @patch('wger.nutrition.models.Image.from_json')
- @patch.object(logger, 'info')
- def test_download_ingredient_wger(self, mock_logger, mock_from_json, mock_request):
+ @patch('requests.get', return_value=MockWgerApiResponse(), autospec=True)
+ @patch('wger.nutrition.models.Image.from_json', autospec=True)
+ @patch.object(logger, 'info', autospec=True)
+ def test_download_ingredient_wger(
+ self,
+ mock_logger: MagicMock,
+ mock_from_json: MagicMock,
+ mock_request: MagicMock,
+ ):
"""
Test that the image is correctly downloaded
@@ -241,14 +333,15 @@ class FetchIngredientImageTestCase(WgerTestCase):
with self.settings(
WGER_SETTINGS={'DOWNLOAD_INGREDIENTS_FROM': DOWNLOAD_INGREDIENT_WGER}, TESTING=False
):
+ # Act
result = fetch_ingredient_image(1)
+ # Assert
mock_logger.assert_any_call('Fetching image for ingredient 1')
mock_logger.assert_any_call(
'Trying to fetch image from WGER for Test ingredient 1 (UUID: '
'7908c204-907f-4b1e-ad4e-f482e9769ade)'
)
-
mock_request.assert_any_call(
'https://wger.de/api/v2/ingredient-image/?ingredient__uuid=7908c204-907f-4b1e-ad4e-f482e9769ade',
headers=wger_headers(),
@@ -258,5 +351,4 @@ class FetchIngredientImageTestCase(WgerTestCase):
headers=wger_headers(),
)
mock_from_json.assert_called()
-
- self.assertEqual(result, None)
+ self.assertIsNone(result)