Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add last image check field to ingredient model
Roland Geider5e69ca904b16
diff --git a/wger/exercises/management/commands/read-exercise-cleanup.py b/wger/exercises/management/commands/read-exercise-cleanup.pyindex 768a30b8d..e88128b56 100644--- a/wger/exercises/management/commands/read-exercise-cleanup.py+++ b/wger/exercises/management/commands/read-exercise-cleanup.py@@ -35,8 +35,7 @@ from wger.exercises.models import (ExerciseVideo,Variation,)-from wger.utils.constants import CC_BY_SA_4_ID-+from wger.utils.constants import CC_BY_SA_4_LICENSE_IDUUID_NEW = 'NEW'VIDEO_AUTHOR = 'Goulart'@@ -93,7 +92,7 @@ class Command(BaseCommand):name = f'{language}:{column}'assert name in file_reader.fieldnames, f'{name} not in {file_reader.fieldnames}'- default_license = License.objects.get(pk=CC_BY_SA_4_ID)+ default_license = License.objects.get(pk=CC_BY_SA_4_LICENSE_ID)## Process the exercisesdiff --git a/wger/exercises/tests/test_exercise_base.py b/wger/exercises/tests/test_exercise_base.pyindex 0fd6d9e5b..64f9caf4d 100644--- a/wger/exercises/tests/test_exercise_base.py+++ b/wger/exercises/tests/test_exercise_base.py@@ -26,7 +26,7 @@ from wger.exercises.models import (Exercise,ExerciseBase,)-from wger.utils.constants import CC_BY_SA_4_ID+from wger.utils.constants import CC_BY_SA_4_LICENSE_IDclass ExerciseBaseTestCase(WgerTestCase):@@ -153,4 +153,4 @@ class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):self.assertEqual(response.status_code, status.HTTP_201_CREATED)exercise = ExerciseBase.objects.get(pk=self.pk)- self.assertEqual(exercise.license_id, CC_BY_SA_4_ID)+ self.assertEqual(exercise.license_id, CC_BY_SA_4_LICENSE_ID)diff --git a/wger/exercises/tests/test_exercise_translation.py b/wger/exercises/tests/test_exercise_translation.pyindex 7e413d12a..3c3b4aaf1 100644--- a/wger/exercises/tests/test_exercise_translation.py+++ b/wger/exercises/tests/test_exercise_translation.py@@ -38,7 +38,7 @@ from wger.exercises.models import (Muscle,)from wger.utils.cache import cache_mapper-from wger.utils.constants import CC_BY_SA_4_ID+from wger.utils.constants import CC_BY_SA_4_LICENSE_IDclass ExerciseRepresentationTestCase(WgerTestCase):@@ -370,7 +370,7 @@ class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):self.assertEqual(response.status_code, status.HTTP_201_CREATED)exercise = Exercise.objects.get(pk=self.pk)- self.assertEqual(exercise.license_id, CC_BY_SA_4_ID)+ self.assertEqual(exercise.license_id, CC_BY_SA_4_LICENSE_ID)def test_patch_clean_html(self):"""diff --git a/wger/nutrition/migrations/0025_add_last_image_check.py b/wger/nutrition/migrations/0025_add_last_image_check.pynew file mode 100644index 000000000..07336ec01--- /dev/null+++ b/wger/nutrition/migrations/0025_add_last_image_check.py@@ -0,0 +1,22 @@+# Generated by Django 4.2.16 on 2025-02-23 14:17++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ('nutrition', '0024_remove_ingredient_status'),+ ]++ operations = [+ migrations.AddField(+ model_name='ingredient',+ name='last_image_check',+ field=models.DateTimeField(+ blank=True,+ default=None,+ null=True,+ editable=False,+ ),+ ),+ ]diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.pyindex fbc325104..7755ba20f 100644--- a/wger/nutrition/models/ingredient.py+++ b/wger/nutrition/models/ingredient.py@@ -59,7 +59,6 @@ from wger.utils.language import load_languagefrom wger.utils.models import AbstractLicenseModelfrom wger.utils.requests import wger_user_agent-logger = logging.getLogger(__name__)@@ -97,6 +96,21 @@ class Ingredient(AbstractLicenseModel, models.Model):)"""Last update time"""+ last_image_check = models.DateTimeField(+ blank=True,+ editable=False,+ default=None,+ null=True,+ )+ """+ Last time we checked for an image.++ This is used to prevent trying to fetch images over and over again from an+ ingredient that does not have any.++ In the future, this field can be used to renew existing images.+ """+uuid = models.UUIDField(default=uuid.uuid4,unique=True,diff --git a/wger/nutrition/sync.py b/wger/nutrition/sync.pyindex aff35e2be..36aecfb76 100644--- a/wger/nutrition/sync.py+++ b/wger/nutrition/sync.py@@ -23,6 +23,7 @@ from typing import (# Djangofrom django.conf import settingsfrom django.db import IntegrityError+from django.utils import timezone# Third Partyimport requests@@ -55,7 +56,6 @@ from wger.utils.requests import ()from wger.utils.url import make_uri-logger = logging.getLogger(__name__)@@ -78,6 +78,15 @@ def fetch_ingredient_image(pk: int):if not ingredient.source_url:return+ if (+ ingredient.last_image_check+ and (+ ingredient.last_image_check + settings.WGER_SETTINGS['INGREDIENT_IMAGE_CHECK_INTERVAL']+ )+ > timezone.now()+ ):+ return+if settings.TESTING:return@@ -181,6 +190,8 @@ def fetch_image_from_off(ingredient: Ingredient):except IntegrityError:logger.info('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.pyindex 535154a94..b55db0f51 100644--- a/wger/nutrition/tests/test_tasks.py+++ b/wger/nutrition/tests/test_tasks.py@@ -12,14 +12,17 @@## You should have received a copy of the GNU Affero General Public License# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.-# Standard Library# Standard Library+import datetimefrom unittest.mock import (ANY,patch,)+# Django+from django.utils import timezone+# wgerfrom wger.core.tests.base_testcase import WgerTestCasefrom wger.nutrition.models import Ingredient@@ -111,7 +114,7 @@ class FetchIngredientImageTestCase(WgerTestCase):ingredient.source_name = 'blabla'ingredient.save()- with self.settings(WGER_SETTINGS={'DOWNLOAD_INGREDIENTS_FROM': True}):+ with self.settings(WGER_SETTINGS={'DOWNLOAD_INGREDIENTS_FROM': 'FOO'}):result = fetch_ingredient_image(1)mock_logger.assert_not_called()mock_request.assert_not_called()@@ -133,6 +136,54 @@ class FetchIngredientImageTestCase(WgerTestCase):mock_request.assert_not_called()self.assertEqual(result, None)+ @patch('requests.get', return_value=MockOffResponse())+ @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):+ """+ Test that no images are fetched if we already checked recently+ """+ ingredient = Ingredient.objects.get(pk=1)+ ingredient.last_image_check = timezone.now() - datetime.timedelta(days=1)+ ingredient.save()++ with self.settings(+ TESTING=False,+ WGER_SETTINGS={+ 'DOWNLOAD_INGREDIENTS_FROM': DOWNLOAD_INGREDIENT_OFF,+ 'INGREDIENT_IMAGE_CHECK_INTERVAL': datetime.timedelta(days=5),+ }+ ):+ result = fetch_ingredient_image(1)+ mock_from_json.assert_not_called()+ mock_logger.assert_not_called()+ mock_request.assert_not_called()+ self.assertEqual(result, None)++ @patch('requests.get', return_value=MockOffResponse())+ @patch('wger.nutrition.models.Image.from_json')+ @patch.object(logger, 'info')+ def test_last_old_image_date(self, mock_logger, mock_from_json, mock_request):+ """+ Test that images are fetched if we checked a long time ago+ """+ ingredient = Ingredient.objects.get(pk=1)+ ingredient.last_image_check = timezone.now() - datetime.timedelta(days=200)+ ingredient.save()++ with self.settings(+ TESTING=False,+ WGER_SETTINGS={+ 'DOWNLOAD_INGREDIENTS_FROM': DOWNLOAD_INGREDIENT_OFF,+ 'INGREDIENT_IMAGE_CHECK_INTERVAL': datetime.timedelta(days=5),+ }+ ):+ result = fetch_ingredient_image(1)+ mock_from_json.assert_called()+ mock_logger.assert_any_call('Fetching image for ingredient 1')+ mock_request.assert_called()+ self.assertEqual(result, None)+@patch('requests.get', return_value=MockOffResponse())@patch('wger.nutrition.models.Image.from_json')@patch.object(logger, 'info')diff --git a/wger/settings_global.py b/wger/settings_global.pyindex de808105b..3f60eb849 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -1,5 +1,3 @@-# -*- coding: utf-8 -*--# This file is part of wger Workout Manager.## wger Workout Manager is free software: you can redistribute it and/or modify@@ -15,6 +13,7 @@# You should have received a copy of the GNU Affero General Public License# Standard Library+import datetimeimport osimport reimport sys@@ -24,7 +23,6 @@ from datetime import timedeltafrom wger import get_versionfrom wger.utils.constants import DOWNLOAD_INGREDIENT_WGER-"""This file contains the global settings that don't usually need to be changed.For a full list of options, visit:@@ -538,10 +536,11 @@ WGER_SETTINGS = {'ALLOW_GUEST_USERS': True,'ALLOW_REGISTRATION': True,'ALLOW_UPLOAD_VIDEOS': False,- 'DOWNLOAD_INGREDIENTS_FROM': DOWNLOAD_INGREDIENT_WGER,'EMAIL_FROM': 'wger Workout Manager <[email protected]>','EXERCISE_CACHE_TTL': 3600,+ 'DOWNLOAD_INGREDIENTS_FROM': DOWNLOAD_INGREDIENT_WGER,'INGREDIENT_CACHE_TTL': 604800, # one week+ 'INGREDIENT_IMAGE_CHECK_INTERVAL': datetime.timedelta(weeks=12),'MIN_ACCOUNT_AGE_TO_TRUST': 21,'SYNC_EXERCISES_CELERY': False,'SYNC_EXERCISE_IMAGES_CELERY': False,diff --git a/wger/utils/constants.py b/wger/utils/constants.pyindex 3615b4d8a..476c98279 100644--- a/wger/utils/constants.py+++ b/wger/utils/constants.py@@ -15,7 +15,6 @@# Standard Libraryfrom decimal import Decimal-# NavigationWORKOUT_TAB = 'workout'EXERCISE_TAB = 'exercises'@@ -49,7 +48,7 @@ PAGINATION_MAX_TOTAL_PAGES = 10PAGINATION_PAGES_AROUND_CURRENT = 5# Important license IDs-CC_BY_SA_4_ID = 2+CC_BY_SA_4_LICENSE_ID = 2CC_BY_SA_3_LICENSE_ID = 1CC_0_LICENSE_ID = 3ODBL_LICENSE_ID = 5diff --git a/wger/utils/models.py b/wger/utils/models.pyindex 79d453884..483338763 100644--- a/wger/utils/models.py+++ b/wger/utils/models.py@@ -18,8 +18,7 @@ from django.utils.translation import gettext_lazy as _# wgerfrom wger.core.models import License-from wger.utils.constants import CC_BY_SA_4_ID-+from wger.utils.constants import CC_BY_SA_4_LICENSE_ID"""Abstract model classes@@ -43,7 +42,7 @@ class AbstractLicenseModel(models.Model):license = models.ForeignKey(License,verbose_name=_('License'),- default=CC_BY_SA_4_ID,+ default=CC_BY_SA_4_LICENSE_ID,on_delete=models.CASCADE,)