Self hosted FLOSS fitness/workout, nutrition and weight tracker
Handle case where ingredient already has an image
Roland Geideraf1ed2d7b200
diff --git a/wger/nutrition/tasks.py b/wger/nutrition/tasks.pyindex 37b8aa7a7..2e634c8be 100644--- a/wger/nutrition/tasks.py+++ b/wger/nutrition/tasks.py@@ -19,6 +19,7 @@ from typing import Optional# Djangofrom django.conf import settings+from django.db import IntegrityError# Third Partyimport requests@@ -94,5 +95,12 @@ def fetch_ingredient_image(pk: int):'license_author': uploader_name,'size': len(response.content)}- Image.from_json(ingredient, response, image_data, headers, generate_uuid=True)+ try:+ Image.from_json(ingredient, response, image_data, headers, 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 error+ except IntegrityError:+ logger.info('Ingredient has already an image, skipping...')+ returnlogger.info('Image successfully saved')