Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fetch ingredient images when used in plans or diary

Roland Geider1a83c1e40075
diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.py
index f42bfd3f0..2861d8e4a 100644
--- a/wger/nutrition/models/ingredient.py
+++ b/wger/nutrition/models/ingredient.py
@@ -427,11 +427,19 @@ class Ingredient(AbstractLicenseModel, models.Model):
if not request.user.is_authenticated:
return
+ self.fetch_image_in_background()
+
+ def fetch_image_in_background(self):
+ """
+ Lets celery fetch the image if it is not available locally
+ """
+ if hasattr(self, 'image'):
+ return
+
if not settings.WGER_SETTINGS['USE_CELERY']:
- logger.info('Celery deactivated, skipping retrieving ingredient image')
+ logger.debug('Celery deactivated, skipping retrieving ingredient image')
return
- # Let celery fetch the image
# wger
from wger.nutrition.tasks import fetch_ingredient_image_task
diff --git a/wger/nutrition/signals.py b/wger/nutrition/signals.py
index 287ebd71b..3955720bc 100644
--- a/wger/nutrition/signals.py
+++ b/wger/nutrition/signals.py
@@ -25,6 +25,7 @@ from easy_thumbnails.files import get_thumbnailer
# wger
from wger.nutrition.models import (
Image,
+ LogItem,
Meal,
MealItem,
NutritionPlan,
@@ -47,6 +48,20 @@ post_save.connect(reset_nutritional_values_canonical_form, sender=MealItem)
post_delete.connect(reset_nutritional_values_canonical_form, sender=MealItem)
+def fetch_ingredient_image(sender, instance: MealItem | LogItem, raw: bool = False, **kwargs):
+ """
+ Fetch the image of ingredients that are used in a plan or the diary
+ """
+ if raw:
+ return
+
+ instance.ingredient.fetch_image_in_background()
+
+
+post_save.connect(fetch_ingredient_image, sender=MealItem)
+post_save.connect(fetch_ingredient_image, sender=LogItem)
+
+
def auto_delete_file_on_delete(sender, instance: Image, **kwargs):
"""
Delete the image along with its thumbnails