Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Document use of download-ingredient-images

Roland Geiderc6533c758514
diff --git a/docs/commands.rst b/docs/commands.rst
index 87860b722..598c1d3e0 100644
--- a/docs/commands.rst
+++ b/docs/commands.rst
@@ -226,9 +226,10 @@ management functions that are sometimes needed. Call them with
``python manage.py <command_name>``:
**download-exercise-images**
- synchronizes the exercise images from wger.de to the local installation. Read
- its help text as it could save the wrong image to the wrong exercise should
- different IDs match.
+ synchronizes the exercise images from wger.de to the local installation.
+
+**download-ingredient-images**
+ synchronizes the ingredient images from wger.de to the local installation.
**redo-capitalize-names**
re-calculates the capitalized exercise names. This command can be called if the
diff --git a/docs/settings.rst b/docs/settings.rst
index 2008b97b1..ab7b95820 100644
--- a/docs/settings.rst
+++ b/docs/settings.rst
@@ -26,6 +26,11 @@ dictionary in your settings file. Currently the following options are supported:
**EMAIL_FROM**: Default `wger Workout Manager <[email protected]>`
The sender address used for sent emails by the system such as weight reminders
+**DOWNLOAD_FROM_OFF**: Default false. Whether to download missing ingredient
+ images from Open Food Facts. Please do not set to true. To download available
+ images, use the `download-ingredient-images` command, which downloads them
+ from a selected wger instance.
+
.. note::
If you want to override a default setting, don't overwrite all the dictionary
diff --git a/extras/docker/compose/README.md b/extras/docker/compose/README.md
index 9dbfcc41c..ebfa977dd 100644
--- a/extras/docker/compose/README.md
+++ b/extras/docker/compose/README.md
@@ -24,6 +24,7 @@ You might also want to download the exercise images and the ingredients
(will take some time):
docker-compose -f extras/docker/compose/docker-compose.yml exec web wger download-exercise-images
+ docker-compose -f extras/docker/compose/docker-compose.yml exec web wger download-ingredient-images
docker-compose -f extras/docker/compose/docker-compose.yml exec web wger load-online-fixtures
Then open <http://localhost:8000> and log in as: **admin**, password **adminadmin**
diff --git a/extras/docker/development/README.md b/extras/docker/development/README.md
index b59d42c9d..ebb824aa0 100644
--- a/extras/docker/development/README.md
+++ b/extras/docker/development/README.md
@@ -37,6 +37,7 @@ You might also want to download the exercise images and the ingredients
(will take some time):
docker exec wger.devel python3 manage.py download-exercise-images
+ docker exec wger.devel python3 manage.py download-ingredient-images
docker exec wger.devel wger load-online-fixtures
### 2 - Open the Application
diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.py
index 510105c40..518ccab4c 100644
--- a/wger/nutrition/models/ingredient.py
+++ b/wger/nutrition/models/ingredient.py
@@ -435,7 +435,9 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
def get_image(self, request: HttpRequest):
"""
- TODO: write this
+ Returns the ingredient image
+
+ If it is not available locally, it is fetched from Open Food Facts servers
"""
if self.image:
return self.image
@@ -449,11 +451,16 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
if not settings.WGER_SETTINGS['DOWNLOAD_FROM_OFF']:
return
+ if settings.TESTING:
+ return
+
+ # Everything looks fine, go ahead
headers = {
'User-agent':
default_user_agent(f'wger/{get_version()} - https://github.com/wger-project')
}
+ # Fetch the product data
product_data = requests.get(self.source_url, headers=headers).json()
image_url: Optional[str] = product_data['product'].get('image_front_url')
if not image_url:
@@ -461,23 +468,26 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
image_data = product_data['product']['images']
- # Parse the file name: https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg
+ # Download the image file
downloaded_image: requests.Response = requests.get(image_url, headers=headers)
if downloaded_image.status_code != 200:
return
+ # Parse the file name, looks something like this:
+ # https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg
image_name: str = image_url.rpartition("/")[2].partition(".")[0]
+
+ # Retrieve the uploader name
try:
image_id: str = image_data[image_name]['imgid']
uploader_name: str = image_data[image_id]['uploader']
except KeyError:
return
- #
+ # Save to DB
image_data: dict = {
'image': os.path.basename(image_url),
'license_author': uploader_name,
'size': len(downloaded_image.content)
}
-
return Image.from_json(self, downloaded_image, image_data, headers, generate_uuid=True)
diff --git a/wger/nutrition/static/js/nutrition.js b/wger/nutrition/static/js/nutrition.js
index 3a4ba488d..e34b53021 100644
--- a/wger/nutrition/static/js/nutrition.js
+++ b/wger/nutrition/static/js/nutrition.js
@@ -69,7 +69,7 @@ function wgerInitIngredientAutocompleter() {
paramName: 'term',
minChars: 3,
onSelect: function (suggestion) {
- var ingredientId = suggestion.data.id;
+ const ingredientId = suggestion.data.id;
// After clicking on a result set the value of the hidden field
diff --git a/wger/settings_global.py b/wger/settings_global.py
index d2f30614b..963e5bf9f 100644
--- a/wger/settings_global.py
+++ b/wger/settings_global.py
@@ -18,7 +18,7 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import re
-
+import sys
"""
This file contains the global settings that don't usually need to be changed.
@@ -431,3 +431,7 @@ WGER_SETTINGS = {
'TWITTER': False,
'DOWNLOAD_FROM_OFF': False
}
+
+
+# Whether the application is being run regularly or during tests
+TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'