Self hosted FLOSS fitness/workout, nutrition and weight tracker
Refactor options used for downloading product images
Roland Geiderebb43c5b1164
diff --git a/extras/docker/development/settings.py b/extras/docker/development/settings.pyindex e2d1408bf..b5507bc48 100644--- a/extras/docker/development/settings.py+++ b/extras/docker/development/settings.py@@ -98,6 +98,7 @@ WGER_SETTINGS["ALLOW_UPLOAD_VIDEOS"] = env.bool("ALLOW_UPLOAD_VIDEOS", True)WGER_SETTINGS["MIN_ACCOUNT_AGE_TO_TRUST"] = env.int("MIN_ACCOUNT_AGE_TO_TRUST", 21) # in daysWGER_SETTINGS["EXERCISE_CACHE_TTL"] = env.int("EXERCISE_CACHE_TTL", 3600)WGER_SETTINGS["USE_CELERY"] = env.bool("USE_CELERY", False)+WGER_SETTINGS["DOWNLOAD_INGREDIENT_IMAGES"] = env.str("DOWNLOAD_INGREDIENT_IMAGES", "WGER")# Cachediff --git a/wger/core/apps.py b/wger/core/apps.pyindex 17fae27ef..233a390b7 100644--- a/wger/core/apps.py+++ b/wger/core/apps.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@@ -24,3 +22,4 @@ class CoreConfig(AppConfig):def ready(self):import wger.core.signals+ import wger.core.checksdiff --git a/wger/core/checks.py b/wger/core/checks.pynew file mode 100644index 000000000..b00466445--- /dev/null+++ b/wger/core/checks.py@@ -0,0 +1,39 @@+# Django+from django.conf import settings+from django.core.checks import (+ Error,+ Warning,+ register,+)++# wger+from wger.utils.constants import DOWNLOAD_INGREDIENT_OPTIONS+++@register()+def settings_check(app_configs, **kwargs):+ errors = []++ # Upstream wger instance should be configured+ if not settings.WGER_SETTINGS.get('WGER_INSTANCE'):+ errors.append(+ Warning(+ 'wger instance not set',+ hint='No wger instance configured, sync commands will not work',+ obj=settings,+ id='wger.W001',+ )+ )++ # Only one setting should be set+ if settings.WGER_SETTINGS['DOWNLOAD_INGREDIENT_IMAGES'] not in DOWNLOAD_INGREDIENT_OPTIONS:+ errors.append(+ Error(+ 'Ingredient images configuration error',+ hint=f'Origin for ingredient images misconfigured. Valid options are '+ f'{DOWNLOAD_INGREDIENT_OPTIONS}',+ obj=settings,+ id='wger.E001',+ )+ )+ return errorsdiff --git a/wger/core/tests/base_testcase.py b/wger/core/tests/base_testcase.pyindex 871280f53..7facae03b 100644--- a/wger/core/tests/base_testcase.py+++ b/wger/core/tests/base_testcase.py@@ -159,7 +159,7 @@ class BaseTestCase():# https://django-axes.readthedocs.io/en/latest/3_usage.html#authenticating-userssettings.AXES_ENABLED = False- settings.WGER_SETTINGS['DOWNLOAD_FROM_OFF'] = False+ settings.WGER_SETTINGS['DOWNLOAD_INGREDIENT_IMAGES'] = Falsesettings.WGER_SETTINGS['USE_CELERY'] = Falsedef tearDown(self):diff --git a/wger/nutrition/management/commands/download-ingredient-images.py b/wger/nutrition/management/commands/download-ingredient-images.pyindex 8f1f950b7..3b4aebc04 100644--- a/wger/nutrition/management/commands/download-ingredient-images.py+++ b/wger/nutrition/management/commands/download-ingredient-images.py@@ -35,6 +35,7 @@ from wger.nutrition.models import (Image,Ingredient,)+from wger.utils.requests import wger_headersIMAGE_API = "{0}/api/v2/ingredient-image/"@@ -62,9 +63,9 @@ class Command(BaseCommand):'--remote-url',action='store',dest='remote_url',- default='https://wger.de',- help='Remote URL to fetch the exercises from (default: '- 'https://wger.de)'+ default=settings.WGER_SETTINGS['WGER_INSTANCE'],+ help=f'Remote URL to fetch the ingredients from (default: WGER_SETTINGS'+ f'["WGER_INSTANCE"] - {settings.WGER_SETTINGS["WGER_INSTANCE"]})')def handle(self, **options):@@ -79,7 +80,7 @@ class Command(BaseCommand):except ValidationError:raise CommandError('Please enter a valid URL')- headers = {'User-agent': f'wger/{get_version()} https://github.com/wger-project'}+ headers = wger_headers()# Get all imagespage = 1@@ -109,7 +110,7 @@ class Command(BaseCommand):except Image.DoesNotExist:self.stdout.write(' Image not found in local DB, creating now...')retrieved_image = requests.get(image_data['image'], headers=headers)- Image.from_json(ingredient, retrieved_image, image_data, headers)+ Image.from_json(ingredient, retrieved_image, image_data)self.stdout.write(self.style.SUCCESS(' successfully saved'))diff --git a/wger/nutrition/management/commands/download-off-ingredient-images.py b/wger/nutrition/management/commands/download-off-ingredient-images.pyindex d519d1a45..bdf0c4f30 100644--- a/wger/nutrition/management/commands/download-off-ingredient-images.py+++ b/wger/nutrition/management/commands/download-off-ingredient-images.py@@ -21,7 +21,7 @@ from django.core.management.base import BaseCommand# wgerfrom wger.nutrition.models import MealItem-from wger.nutrition.tasks import fetch_ingredient_image+from wger.nutrition.tasks import fetch_ingredient_image_tasklogger = logging.getLogger(__name__)@@ -35,15 +35,17 @@ class Command(BaseCommand):currently used ingredients"""- help = 'Download images of all Open Food Facts ingredients that are used in a nutrition plan'+ help = 'Download images of all Open Food Facts ingredients that are used in a nutritional plan'def handle(self, **options):if not settings.WGER_SETTINGS['USE_CELERY']:- logger.info('Celery deactivated, skipping task')+ self.stdout.write('Celery deactivated. Exiting...')return# Make sure off downloads are enabled- settings.WGER_SETTINGS['DOWNLOAD_FROM_OFF'] = True+ if not settings.WGER_SETTINGS['DOWNLOAD_INGREDIENT_IMAGES']:+ self.stdout.write('DOWNLOAD_INGREDIENT_IMAGES not set. Exiting...')+ return# Since each MealItem is linked to a NutritionPlan via a Meal we can skip accessing# NutritionPlan and Meal itself and fetch all MealItems directly instead.@@ -52,7 +54,7 @@ class Command(BaseCommand):download_counter = 0for meal_item in meal_items:if meal_item.ingredient:- fetch_ingredient_image.delay(meal_item.ingredient.pk)+ fetch_ingredient_image_task.delay(meal_item.ingredient.pk)download_counter += 1meal_item_counter += 1diff --git a/wger/nutrition/models/image.py b/wger/nutrition/models/image.pyindex 1d916bf15..473acbcc6 100644--- a/wger/nutrition/models/image.py+++ b/wger/nutrition/models/image.py@@ -77,12 +77,9 @@ class Image(AbstractLicenseModel, models.Model, BaseImage):connect_to,retrieved_image,json_data: dict,- headers,generate_uuid: bool = False,):- image: cls = super().from_json(- connect_to, retrieved_image, json_data, headers, generate_uuid- )+ image: cls = super().from_json(connect_to, retrieved_image, json_data, generate_uuid)image.ingredient = connect_toimage.license = License.objects.get(pk=1)diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.pyindex 6c0b6b126..15576449b 100644--- a/wger/nutrition/models/ingredient.py+++ b/wger/nutrition/models/ingredient.py@@ -41,7 +41,7 @@ from django.utils.translation import gettext_lazy as _# wgerfrom wger.core.models import Language-from wger.nutrition.tasks import fetch_ingredient_image+from wger.nutrition.tasks import fetch_ingredient_image_taskfrom wger.utils.cache import cache_mapperfrom wger.utils.constants import TWOPLACESfrom wger.utils.managers import SubmissionManager@@ -449,8 +449,8 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):returnif not settings.WGER_SETTINGS['USE_CELERY']:- logger.info('Celery deactivated, skipping task')+ logger.info('Celery deactivated, skipping retrieving ingredient image')return# Let celery fetch the image- fetch_ingredient_image.delay(self.pk)+ fetch_ingredient_image_task.delay(self.pk)diff --git a/wger/nutrition/tasks.py b/wger/nutrition/tasks.pyindex e51fa98a7..cc7491a49 100644--- a/wger/nutrition/tasks.py+++ b/wger/nutrition/tasks.py@@ -28,27 +28,31 @@ from celery import shared_task# wgerfrom wger.nutrition.models import Imagefrom wger.nutrition.models.sources import Source+from wger.utils.constants import (+ DOWNLOAD_INGREDIENT_OFF,+ DOWNLOAD_INGREDIENT_WGER,+)from wger.utils.requests import wger_headers+logger = logging.getLogger(__name__)@shared_task()-def fetch_ingredient_image(pk: int):+def fetch_ingredient_image_task(pk: int):"""Fetches the ingredient image from Open Food Facts servers if it is not available locallyReturns the image if it is already present in the DB"""- fetch_ingredient_image_function(pk)+ fetch_ingredient_image(pk)-def fetch_ingredient_image_function(pk: int):+def fetch_ingredient_image(pk: int):# wgerfrom wger.nutrition.models import Ingredientingredient = Ingredient.objects.get(pk=pk)-logger.info(f'Fetching image for ingredient {pk}')if hasattr(ingredient, 'image'):@@ -60,11 +64,29 @@ def fetch_ingredient_image_function(pk: int):if not ingredient.source_url:return- if not settings.WGER_SETTINGS['DOWNLOAD_FROM_OFF']:+ if settings.TESTING:return- if settings.TESTING:+ if settings.WGER_SETTINGS['DOWNLOAD_INGREDIENT_IMAGES'] == DOWNLOAD_INGREDIENT_OFF:+ fetch_image_from_off(ingredient)+ elif settings.WGER_SETTINGS['DOWNLOAD_INGREDIENT_IMAGES'] == DOWNLOAD_INGREDIENT_WGER:+ fetch_image_from_wger_instance(ingredient)+++def fetch_image_from_wger_instance(ingredient):+ url = f"{settings.WGER_SETTINGS['WGER_INSTANCE']}/api/v2/ingredient-image/{ingredient.pk}"+ result = requests.get(url, headers=wger_headers()).json()+ image_uuid = result['uuid']+ try:+ Image.objects.get(uuid=image_uuid)+ logger.info('image already present locally, skipping...')return+ except Image.DoesNotExist:+ retrieved_image = requests.get(result['image'], headers=wger_headers())+ Image.from_json(ingredient, retrieved_image, result)+++def fetch_image_from_off(ingredient):# Everything looks fine, go aheadlogger.info(f'Trying to fetch image from OFF for {ingredient.name} (UUID: {ingredient.uuid})')@@ -103,7 +125,7 @@ def fetch_ingredient_image_function(pk: int):'size': len(response.content)}try:- Image.from_json(ingredient, response, image_data, headers, generate_uuid=True)+ Image.from_json(ingredient, response, image_data, generate_uuid=True)# Due to a race condition (e.g. when adding tasks over the search), we might# try to save an image to an ingredient that already has one. In that case,# just ignore the errordiff --git a/wger/nutrition/tests/test_tasks.py b/wger/nutrition/tests/test_tasks.pyindex 0968b1f97..3151ba22e 100644--- a/wger/nutrition/tests/test_tasks.py+++ b/wger/nutrition/tests/test_tasks.py@@ -24,15 +24,41 @@ from unittest.mock import (from wger.core.tests.base_testcase import WgerTestCasefrom wger.nutrition.models import Ingredientfrom wger.nutrition.tasks import (- fetch_ingredient_image_function,+ fetch_ingredient_image,logger,)+from wger.utils.constants import DOWNLOAD_INGREDIENT_OFFfrom wger.utils.requests import wger_headers+loggerMock = MagicMock()-class MockResponse:+class MockOffResponse:++ def __init__(self):+ self.status_code = 200+ self.content = b'2000'++ @staticmethod+ def json():+ return {+ "product": {+ 'image_front_url':+ 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',+ 'images': {+ 'front_en': {+ 'imgid': '12345',+ },+ '12345': {+ 'uploader': 'Mr Foobar'+ }+ }+ },+ }+++class MockWgerApiResponse:def __init__(self):self.status_code = 200@@ -69,7 +95,7 @@ class FetchIngredientImageTestCase(WgerTestCase):ingredient.source_name = 'blabla'ingredient.save()- result = fetch_ingredient_image_function(1)+ result = fetch_ingredient_image(1)self.assertEqual(result, None)def test_download_off_setting(self):@@ -80,11 +106,11 @@ class FetchIngredientImageTestCase(WgerTestCase):ingredient.source_name = 'blabla'ingredient.save()- with self.settings(WGER_SETTINGS={'DOWNLOAD_FROM_OFF': False}):- result = fetch_ingredient_image_function(1)+ with self.settings(WGER_SETTINGS={'DOWNLOAD_INGREDIENT_IMAGES': False}):+ result = fetch_ingredient_image(1)self.assertEqual(result, None)- @patch('requests.get', return_value=MockResponse())+ @patch('requests.get', return_value=MockOffResponse())@patch.object(logger, 'info')def test_download_ingredient(self, mock_logger, mock_request):"""@@ -95,8 +121,11 @@ class FetchIngredientImageTestCase(WgerTestCase):object could probably be done better"""- with self.settings(WGER_SETTINGS={'DOWNLOAD_FROM_OFF': True}, TESTING=False):- result = fetch_ingredient_image_function(1)+ with self.settings(+ WGER_SETTINGS={'DOWNLOAD_INGREDIENT_IMAGES': DOWNLOAD_INGREDIENT_OFF},+ TESTING=False+ ):+ result = fetch_ingredient_image(1)log1 = mock_logger.mock_calls[0]log2 = mock_logger.mock_calls[1]diff --git a/wger/settings_global.py b/wger/settings_global.pyindex 19ca7dd37..453005c39 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -21,6 +21,8 @@ import reimport sysfrom datetime import timedelta+# wger+from wger.utils.constants import DOWNLOAD_INGREDIENT_WGER"""This file contains the global settings that don't usually need to be changed.@@ -484,8 +486,7 @@ WGER_SETTINGS = {'ALLOW_GUEST_USERS': True,'ALLOW_REGISTRATION': True,'ALLOW_UPLOAD_VIDEOS': False,- 'DOWNLOAD_INGREDIENT_IMAGES_FROM_OFF': False,- 'DOWNLOAD_INGREDIENT_IMAGES_FROM_WGER': True,+ 'DOWNLOAD_INGREDIENT_IMAGES': DOWNLOAD_INGREDIENT_WGER,'EMAIL_FROM': 'wger Workout Manager <[email protected]>','EXERCISE_CACHE_TTL': 3600,'MIN_ACCOUNT_AGE_TO_TRUST': 21,diff --git a/wger/utils/constants.py b/wger/utils/constants.pyindex 3c7e9cd64..1ac1e4fdb 100644--- a/wger/utils/constants.py+++ b/wger/utils/constants.py@@ -59,3 +59,8 @@ DEFAULT_LICENSE_ID = 2# Default languageENGLISH_SHORT_NAME = 'en'++# Possible values for ingredient image download+DOWNLOAD_INGREDIENT_WGER = 'WGER'+DOWNLOAD_INGREDIENT_OFF = 'OFF'+DOWNLOAD_INGREDIENT_OPTIONS = (DOWNLOAD_INGREDIENT_WGER, DOWNLOAD_INGREDIENT_OFF, None)diff --git a/wger/utils/helpers.py b/wger/utils/helpers.pyindex d88e94fe8..c7f624980 100644--- a/wger/utils/helpers.py+++ b/wger/utils/helpers.py@@ -273,9 +273,7 @@ class BaseImage:)@classmethod- def from_json(- cls, connect_to, retrieved_image, json_data: dict, headers, generate_uuid: bool = False- ):+ def from_json(cls, connect_to, retrieved_image, json_data: dict, generate_uuid: bool = False):image: cls = cls()if not generate_uuid:image.uuid = json_data['uuid']