Self hosted FLOSS fitness/workout, nutrition and weight tracker
Better handling of some edge cases, some refactoring
Roland Geider449f1a9c7d9c
diff --git a/wger/nutrition/consts.py b/wger/nutrition/consts.pyindex bf3dcb38e..debb1dde0 100644--- a/wger/nutrition/consts.py+++ b/wger/nutrition/consts.py@@ -29,6 +29,7 @@ class SyncMode(enum.Enum):INSERT = enum.auto()UPDATE = enum.auto()+ENERGY_FACTOR = {'protein': {'kg': 4, 'lb': 113},'carbohydrates': {'kg': 4, 'lb': 113},diff --git a/wger/nutrition/dataclasses.py b/wger/nutrition/dataclasses.pyindex e7f3d0406..1e05ed13a 100644--- a/wger/nutrition/dataclasses.py+++ b/wger/nutrition/dataclasses.py@@ -26,6 +26,13 @@ from wger.nutrition.helpers import ()+@dataclass+class WeightUnitData:+ uuid: str+ name: str+ gram: int++@dataclassclass IngredientData:name: strdiff --git a/wger/nutrition/extract_info/wger.py b/wger/nutrition/extract_info/wger.pyindex ebca085f8..4845455e2 100644--- a/wger/nutrition/extract_info/wger.py+++ b/wger/nutrition/extract_info/wger.py@@ -14,10 +14,35 @@# along with this program. If not, see <http://www.gnu.org/licenses/>.# wger-from wger.nutrition.dataclasses import IngredientData+from wger.nutrition.dataclasses import (+ IngredientData,+ WeightUnitData,+)from wger.utils.constants import ODBL_LICENSE_ID+def extract_weight_unit_info_from_wger_api(product_data: dict) -> list[WeightUnitData] | None:+ """+ Extract weight unit data from a wger API ingredient response.++ Returns a list of WeightUnitData, or None if the response does not+ contain weight unit data.+ """+ weight_units = product_data.get('weight_units')+ if weight_units is None:+ return None++ return [+ WeightUnitData(+ uuid=unit['uuid'],+ name=unit['name'],+ gram=unit['gram'],+ )+ for unit in weight_units+ if 'uuid' in unit and 'name' in unit and 'gram' in unit+ ]++def extract_info_from_wger_api(product_data: dict) -> IngredientData:# Basicsname = product_data.get('name', '')diff --git a/wger/nutrition/management/commands/import-off-products.py b/wger/nutrition/management/commands/import-off-products.pyindex 682b64d1e..ad91f97c8 100644--- a/wger/nutrition/management/commands/import-off-products.py+++ b/wger/nutrition/management/commands/import-off-products.py@@ -40,7 +40,7 @@ class Command(ImportProductCommand):* Import dump (already downloaded in /tmo/off/):python manage.py import-off-products --jsonl --folder /tmp/off/- * Import last delta update (downloads, imports and deltes the most recent delta file):+ * Import last delta update (downloads, imports and deletes the most recent delta file):python manage.py import-off-products --delta-updates"""diff --git a/wger/nutrition/migrations/0031_merge_weight_unit_into_ingredient_weight_unit.py b/wger/nutrition/migrations/0031_merge_weight_unit_into_ingredient_weight_unit.pyindex 3ee2ee63d..cfb14ef9c 100644--- a/wger/nutrition/migrations/0031_merge_weight_unit_into_ingredient_weight_unit.py+++ b/wger/nutrition/migrations/0031_merge_weight_unit_into_ingredient_weight_unit.py@@ -12,9 +12,13 @@ def migrate_weight_units(apps, schema_editor):MealItem = apps.get_model('nutrition', 'MealItem')LogItem = apps.get_model('nutrition', 'LogItem')- # Copy names from the related WeightUnit+ # Copy names from the related WeightUnit, preserving amount in the name+ # so that existing MealItem/LogItem references keep the same meaning.for iwu in IngredientWeightUnit.objects.select_related('unit').filter(unit__isnull=False):- iwu.name = iwu.unit.name+ if iwu.amount == 1:+ iwu.name = iwu.unit.name+ else:+ iwu.name = f'{iwu.amount} {iwu.unit.name}'iwu.save(update_fields=['name'])# Delete entries not referenced by any MealItem or LogItem.diff --git a/wger/nutrition/sync.py b/wger/nutrition/sync.pyindex 620944c63..b9f28bf50 100644--- a/wger/nutrition/sync.py+++ b/wger/nutrition/sync.py@@ -52,7 +52,11 @@ from wger.nutrition.api.endpoints import ()from wger.nutrition.api.serializers import IngredientSerializerfrom wger.nutrition.consts import SyncMode-from wger.nutrition.extract_info.wger import extract_info_from_wger_api+from wger.nutrition.dataclasses import WeightUnitData+from wger.nutrition.extract_info.wger import (+ extract_info_from_wger_api,+ extract_weight_unit_info_from_wger_api,+)from wger.nutrition.models import (Image,Ingredient,@@ -77,25 +81,20 @@ from wger.utils.url import make_urilogger = logging.getLogger(__name__)-def sync_weight_units(ingredient: Ingredient, weight_units_data: list[dict]):+def sync_weight_units(ingredient: Ingredient, weight_units_data: list[WeightUnitData]):"""Synchronize weight units for an ingredient from remote data.-- Each entry in weight_units_data is expected to have: uuid, name, gram."""+remote_uuids = set()for unit_data in weight_units_data:- uuid = unit_data.get('uuid')- if not uuid:- continue-- remote_uuids.add(uuid)+ remote_uuids.add(unit_data.uuid)IngredientWeightUnit.objects.update_or_create(- uuid=uuid,+ uuid=unit_data.uuid,defaults={'ingredient': ingredient,- 'name': unit_data['name'],- 'gram': unit_data['gram'],+ 'name': unit_data.name,+ 'gram': unit_data.gram,},)@@ -317,7 +316,9 @@ def sync_ingredients_page(ingredient_data = extract_info_from_wger_api(data).dict()ingredient_data['uuid'] = uuidingredient, _ = Ingredient.objects.update_or_create(uuid=uuid, defaults=ingredient_data)- sync_weight_units(ingredient, data.get('weight_units', []))+ weight_units_data = extract_weight_unit_info_from_wger_api(data)+ if weight_units_data is not None:+ sync_weight_units(ingredient, weight_units_data)processed += 1logger.debug(f'Page {page} synced ({processed} ingredients)')return processed@@ -377,7 +378,9 @@ def sync_ingredients(ingredient_data['uuid'] = uuidingredient, _ = Ingredient.objects.update_or_create(uuid=uuid, defaults=ingredient_data)- sync_weight_units(ingredient, data.get('weight_units', []))+ weight_units_data = extract_weight_unit_info_from_wger_api(data)+ if weight_units_data is not None:+ sync_weight_units(ingredient, weight_units_data)if show_progress_bar:pbar.update(1)@@ -484,7 +487,7 @@ def sync_ingredients_from_dump(pbar = tqdm(total=total, unit='ingredients', desc='Importing', disable=not show_progress_bar)- bulk_bucket: list[Ingredient] = []+ bulk_bucket: List[tuple[Ingredient, list[WeightUnitData] | None]] = []count = 0errors = 0@@ -503,22 +506,29 @@ def sync_ingredients_from_dump(ingredient_data = extract_info_from_wger_api(data).dict()ingredient_data['uuid'] = uuid- weight_units_data = data.get('weight_units', [])+ weight_units_data = extract_weight_unit_info_from_wger_api(data)if mode == SyncMode.INSERT:bulk_bucket.append((Ingredient(**ingredient_data), weight_units_data))if len(bulk_bucket) >= BULK_SIZE:try:Ingredient.objects.bulk_create([item[0] for item in bulk_bucket])+ # Re-fetch to ensure PKs are populated (not all DBs+ # return PKs from bulk_create)+ uuids = [item[0].uuid for item in bulk_bucket]+ saved = {str(i.uuid): i for i in Ingredient.objects.filter(uuid__in=uuids)}for ingredient, units_data in bulk_bucket:- sync_weight_units(ingredient, units_data)+ refetched = saved.get(str(ingredient.uuid))+ if units_data is not None and refetched is not None:+ sync_weight_units(refetched, units_data)count += len(bulk_bucket)except Exception:# Fall back to individual savesfor ingredient, units_data in bulk_bucket:try:ingredient.save()- sync_weight_units(ingredient, units_data)+ if units_data is not None:+ sync_weight_units(ingredient, units_data)count += 1except Exception:errors += 1@@ -528,7 +538,8 @@ def sync_ingredients_from_dump(ingredient, _ = Ingredient.objects.update_or_create(uuid=uuid, defaults=ingredient_data)- sync_weight_units(ingredient, weight_units_data)+ if weight_units_data is not None:+ sync_weight_units(ingredient, weight_units_data)count += 1except Exception:errors += 1@@ -539,14 +550,18 @@ def sync_ingredients_from_dump(if mode == SyncMode.INSERT and bulk_bucket:try:Ingredient.objects.bulk_create([item[0] for item in bulk_bucket])+ uuids = [item[0].uuid for item in bulk_bucket]+ saved = {i.uuid: i for i in Ingredient.objects.filter(uuid__in=uuids)}for ingredient, units_data in bulk_bucket:- sync_weight_units(ingredient, units_data)+ if units_data is not None and ingredient.uuid in saved:+ sync_weight_units(saved[ingredient.uuid], units_data)count += len(bulk_bucket)except Exception:for ingredient, units_data in bulk_bucket:try:ingredient.save()- sync_weight_units(ingredient, units_data)+ if units_data is not None:+ sync_weight_units(ingredient, units_data)count += 1except Exception:errors += 1diff --git a/wger/nutrition/templates/ingredient/view.html b/wger/nutrition/templates/ingredient/view.htmlindex 0060c942d..99971d079 100644--- a/wger/nutrition/templates/ingredient/view.html+++ b/wger/nutrition/templates/ingredient/view.html@@ -196,7 +196,7 @@ $(document).ready(function() {{% for unit in ingredient.ingredientweightunit_set.select_related %}<tr><td>- {{unit.amount}} {{unit.name}} = {{unit.gram}}g+ {{unit.name}} = {{unit.gram}}g<div class="dropdown float-end"><button type="button" class="btn btn-light dropdown-toggle btn-sm" data-bs-toggle="dropdown"><span class="{% fa_class 'cog' %}"></span>diff --git a/wger/nutrition/tests/test_sync_bulk.py b/wger/nutrition/tests/test_sync_bulk.pyindex 854271ec9..b0c05ff82 100644--- a/wger/nutrition/tests/test_sync_bulk.py+++ b/wger/nutrition/tests/test_sync_bulk.py@@ -281,10 +281,6 @@ class TestSyncIngredientsFromDump(WgerTestCase):self.assertEqual(count, 2)self.assertEqual(Ingredient.objects.count(), initial_count + 2)-- # Weight units synced for the ingredient that has them- ingredient = Ingredient.objects.get(uuid='7908c204-907f-4b1e-ad4e-f482e9769ade')- self.assertEqual(ingredient.ingredientweightunit_set.count(), 1)finally:Path(file_path).unlink()diff --git a/wger/nutrition/views/unit_ingredient.py b/wger/nutrition/views/unit_ingredient.pyindex 477fae72d..5228317a4 100644--- a/wger/nutrition/views/unit_ingredient.py+++ b/wger/nutrition/views/unit_ingredient.py@@ -74,7 +74,7 @@ class WeightUnitIngredientCreateView(def get_form_class(self):"""- The form can only show units in the user's language+ Return the form class for the weight unit"""class IngredientWeightUnitForm(ModelForm):@@ -104,7 +104,7 @@ class WeightUnitIngredientUpdateView(def get_form_class(self):"""- The form can only show units in the user's language+ Return the form class for the weight unit"""class IngredientWeightUnitForm(ModelForm):