Skip to content

armgit / wger

Public Imported from GitHub
0

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.py
index 37b8aa7a7..2e634c8be 100644
--- a/wger/nutrition/tasks.py
+++ b/wger/nutrition/tasks.py
@@ -19,6 +19,7 @@ from typing import Optional
# Django
from django.conf import settings
+from django.db import IntegrityError
# Third Party
import 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...')
+ return
logger.info('Image successfully saved')