Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Merge branch 'master' into pr-1875

Cam Cecil6239cfdbe832
diff --cc AUTHORS.rst
index 7b3361b5f,5e5eb9178..e033ae525
--- a/AUTHORS.rst
+++ b/AUTHORS.rst
@@@ -85,7 -85,7 +85,8 @@@ Developer
* eyJhb - https://github.com/eyJhb
* Joshua Shelley - https://github.com/navyjosh
* Matt Harrison - https://github.com/Maralai
+* Cam Cecil - https://github.com/scrapcode
+ * Ali Rahbar - https://github.com/crypto-a
Translators
diff --cc wger/nutrition/sync.py
index 06663c22e,937e9bd5b..bee91cc6b
--- a/wger/nutrition/sync.py
+++ b/wger/nutrition/sync.py
@@@ -225,69 -219,37 +225,101 @@@ def sync_ingredients
style_fn=lambda x: x,
):
"""Synchronize the ingredients from the remote server"""
+
+ def _sync_ingredients(language_id: Optional[int] = None):
+ if language_id is not None:
+ url = make_uri(
+ INGREDIENTS_ENDPOINT,
+ server_url=remote_url,
+ query={'limit': API_MAX_ITEMS, 'language__in': language_id},
+ )
+ else:
+ url = make_uri(
+ INGREDIENTS_ENDPOINT,
+ server_url=remote_url,
+ query={'limit': API_MAX_ITEMS},
+ )
+ for data in get_paginated(url, headers=wger_headers()):
+ uuid = data['uuid']
+ name = data['name']
+
+ ingredient, created = Ingredient.objects.update_or_create(
+ uuid=uuid,
+ defaults={
+ 'name': name,
+ 'code': data['code'],
+ 'language_id': data['language'],
+ 'created': data['created'],
+ 'license_id': data['license'],
+ 'license_object_url': data['license_object_url'],
+ 'license_author': data['license_author_url'],
+ 'license_author_url': data['license_author_url'],
+ 'license_title': data['license_title'],
+ 'license_derivative_source_url': data['license_derivative_source_url'],
+ 'energy': data['energy'],
+ 'carbohydrates': data['carbohydrates'],
+ 'carbohydrates_sugar': data['carbohydrates_sugar'],
+ 'fat': data['fat'],
+ 'fat_saturated': data['fat_saturated'],
+ 'protein': data['protein'],
+ 'fiber': data['fiber'],
+ 'sodium': data['sodium'],
+ },
+ )
+
+ print_fn(f'{"created" if created else "updated"} ingredient {uuid} - {name}')
+
print_fn('*** Synchronizing ingredients...')
+ if language_codes is not None:
+ language_ids: List[int] = []
+ for code in language_codes.split(','):
+ # Leaving the try except in here even though we've already validated on the sync-ingredients command itself.
+ # This is in case we ever want to re-use this function for anything else where user can input language codes.
+ try:
+ lang = load_language(code, default_to_english=False)
+ language_ids.append(lang.id)
+ except Language.DoesNotExist as e:
+ print_fn(
+ f'Error: The language code you provided ("{code}") does not exist in this database. Please try again.'
+ )
+ return 0
+
+ for language_id in language_ids:
+ _sync_ingredients(language_id)
+ else:
+ _sync_ingredients()
+
+ url = make_uri(INGREDIENTS_ENDPOINT, server_url=remote_url, query={'limit': API_MAX_ITEMS})
+ for data in get_paginated(url, headers=wger_headers()):
+ uuid = data['uuid']
+ name = data['name']
+
+ ingredient, created = Ingredient.objects.update_or_create(
+ uuid=uuid,
+ defaults={
+ 'name': name,
+ 'code': data['code'],
+ 'language_id': data['language'],
+ 'created': data['created'],
+ 'license_id': data['license'],
+ 'license_object_url': data['license_object_url'],
+ 'license_author': data['license_author_url'],
+ 'license_author_url': data['license_author_url'],
+ 'license_title': data['license_title'],
+ 'license_derivative_source_url': data['license_derivative_source_url'],
+ 'energy': data['energy'],
+ 'carbohydrates': data['carbohydrates'],
+ 'carbohydrates_sugar': data['carbohydrates_sugar'],
+ 'fat': data['fat'],
+ 'fat_saturated': data['fat_saturated'],
+ 'protein': data['protein'],
+ 'fiber': data['fiber'],
+ 'sodium': data['sodium'],
+ },
+ )
+
+ print_fn(f'{"created" if created else "updated"} ingredient {uuid} - {name}')
+
++
print_fn(style_fn('done!\n'))