Self hosted FLOSS fitness/workout, nutrition and weight tracker
Somewhat simplify ingredient image sync function
Roland Geiderdd3572fd1c2b
diff --git a/wger/nutrition/sync.py b/wger/nutrition/sync.pyindex cb3ab39ae..ffc0b2599 100644--- a/wger/nutrition/sync.py+++ b/wger/nutrition/sync.py@@ -36,7 +36,10 @@ from wger.utils.constants import (DOWNLOAD_INGREDIENT_OFF,DOWNLOAD_INGREDIENT_WGER,)-from wger.utils.requests import wger_headers+from wger.utils.requests import (+ get_paginated_generator,+ wger_headers,+)from wger.utils.url import make_uri@@ -156,19 +159,13 @@ def download_ingredient_images(remote_url=settings.WGER_SETTINGS['WGER_INSTANCE'],style_fn=lambda x: x,):- page = 1- all_images_processed = Falseheaders = wger_headers()- url = make_uri(IMAGE_ENDPOINT, server_url=remote_url)- result = requests.get(url, headers=headers).json()+ url = make_uri(IMAGE_ENDPOINT, server_url=remote_url, query={'limit': 100})print_fn('*** Processing images ***')- while not all_images_processed:- print_fn('')- print_fn(f'*** Page {page}')- print_fn('')+ for result in get_paginated_generator(url, headers=headers):- for image_data in result['results']:+ for image_data in result:image_uuid = image_data['uuid']print_fn(f'Processing image {image_uuid}')@@ -179,6 +176,9 @@ def download_ingredient_images(print_fn(' Remote ingredient not found in local DB, skipping...')continue+ if hasattr(ingredient, 'image'):+ continue+try:Image.objects.get(uuid=image_uuid)print_fn(' Image already present locally, skipping...')@@ -189,9 +189,3 @@ def download_ingredient_images(Image.from_json(ingredient, retrieved_image, image_data)print_fn(style_fn(' successfully saved'))-- if result['next']:- page += 1- result = requests.get(result['next'], headers=headers).json()- else:- all_images_processed = Truediff --git a/wger/utils/requests.py b/wger/utils/requests.pyindex 4723dca65..cbb55c4de 100644--- a/wger/utils/requests.py+++ b/wger/utils/requests.py@@ -43,7 +43,27 @@ def get_paginated(url: str, headers=None):response = requests.get(url, headers=headers).json()results.extend(response['results'])+ url = response['next']if not response['next']:break- url = response['next']return results+++def get_paginated_generator(url: str, headers=None):+ """+ Generator that iterates over a paginated endpoint++ :param url: The URL to fetch from.+ :param headers: Optional headers to send with the request.+ :return: Generator with the contents of the 'result' key+ """+ if headers is None:+ headers = {}++ while True:+ response = requests.get(url, headers=headers).json()+ yield response['results']++ url = response['next']+ if not response['next']:+ break