Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Exercis

nikithamurikinati4bde5ac2102a
diff --git a/extras/scripts/create_ingredients_from_foodfacts.py b/extras/scripts/create_ingredients_from_foodfacts.py
index 7b7c95394..bf790ad0f 100644
--- a/extras/scripts/create_ingredients_from_foodfacts.py
+++ b/extras/scripts/create_ingredients_from_foodfacts.py
@@ -1,7 +1,11 @@
from pymongo import MongoClient
+import os
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
+import django
+django.setup()
from wger.nutrition.models import Ingredient
-client = MongoClient('192.168.178.20', 27017)
+client = MongoClient(port=27017)
db = client.off
# loop through each product and create Ingredient object
@@ -9,10 +13,30 @@ db = client.off
# energy, protein, fiber, carbs, sugar, fat, saturated, sodium, code, source_name, source_url,
# last_imported, common_name, image, category, brand
-
-for product in db.find({"lang": "de"}):
- name = product.get('product_name')
- print(name)
+# print(db.getCollectionNames())
+for product in db.products.find({"lang": "de"}):
+ name = product['product_name']
+ lang = product['lang']
+ # nutriment data not always complete (should make these all optional)
+ energy = product['nutriments']['energy-kcal_100g']
+ protein = product['nutriments']['proteins_100g']
+ fiber = product['nutriments']['fiber_100g']
+ carbs = product['nutriments']['carbohydrates_100g']
+ sugar = product['nutriments']['sugars_100g']
+ fat = product['nutriments']['fat_100g']
+ saturated = product['nutriments']['saturated-fat_100g']
+ sodium = product['nutriments']['sodium_100g']
+
+ code = product['code']
+ # source_name =
+ # source_url =
+ # last_imported = default today?
+ common_name = product['generic_name'] # often empty
+ # image =
+ # category =
+ brand = product['brands']
+ print(name)
+ break
# english, german, bulgarian, greek, spanish, russian, dutch, portuguese, czech, swedish, norwegian, french
-# Ingredient.create(blah)
\ No newline at end of file
+# Ingredient.create(blah)
diff --git a/extras/scripts/import_foodfacts.sh b/extras/scripts/import_foodfacts.sh
index fb337cc15..f685f1f6e 100644
--- a/extras/scripts/import_foodfacts.sh
+++ b/extras/scripts/import_foodfacts.sh
@@ -6,7 +6,8 @@ wget https://static.openfoodfacts.org/data/openfoodfacts-mongodbdump.tar.gz
tar xzvf openfoodfacts-mongodbdump.tar.gz
docker pull mongo
-run --name wger_mongo --mount type=bind,source='"C:\Users\User\GitHub\openfoodfacts\dump"',target=/tmp/mongo_dump -p 27017:27017 -d mongo
+docker run -it --name wger_mongo -v $(pwd)/dump:/tmp/mongo_dump -p 27017:27017 -d mongo:latest
+mongorestore -d off -c products tmp/mongo_dump/off/products.bson
python create_ingredients_from_foodfacts.py
diff --git a/wger/nutrition/views/ingredient.py b/wger/nutrition/views/ingredient.py
index 11054c2f7..f55be50bb 100644
--- a/wger/nutrition/views/ingredient.py
+++ b/wger/nutrition/views/ingredient.py
@@ -47,8 +47,8 @@ from django.views.generic import (
# wger
from wger.nutrition.forms import (
- UnitChooserForm,
IngredientForm,
+ UnitChooserForm
)
from wger.nutrition.models import Ingredient
from wger.utils.cache import cache_mapper