Self hosted FLOSS fitness/workout, nutrition and weight tracker
Use an enum to store the possible values for import sources
Roland Geider3c83a18e6a27
diff --git a/extras/scripts/create_ingredients_from_foodfacts.py b/extras/scripts/create_ingredients_from_foodfacts.pyindex 38335545f..ef053267a 100644--- a/extras/scripts/create_ingredients_from_foodfacts.py+++ b/extras/scripts/create_ingredients_from_foodfacts.py@@ -23,6 +23,7 @@ django.setup()from django.conf import settings # noqa: E402from wger.nutrition.models import Ingredient # noqa: E402+from wger.nutrition.models.ingredient import Source # noqa: E402from wger.core.models import Language # noqa: E402@@ -149,7 +150,7 @@ for product in db.products.find({'lang': {"$in": list(languages.keys())}, 'complif common_name and len(common_name) > 200:continue- source_name = "Open Food Facts"+ source_name = Source.OPEN_FOOD_FACTSsource_url = f'https://world.openfoodfacts.org/api/v0/product/{code}.json'ingredient_data = {@@ -170,7 +171,7 @@ for product in db.products.find({'lang': {"$in": list(languages.keys())}, 'compl'brand': brand,'status': 2,'license_id': 5,- 'license_author': 'Open Food Facts',+ 'license_author': Source.OPEN_FOOD_FACTS,}# Add entries as new productsdiff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.pyindex 386e7c5a1..ab39f9137 100644--- a/wger/nutrition/models/ingredient.py+++ b/wger/nutrition/models/ingredient.py@@ -19,6 +19,7 @@ import loggingimport osimport uuid as uuidfrom decimal import Decimal+from enum import Enumfrom typing import Optional# Django@@ -64,6 +65,10 @@ from .ingredient_category import IngredientCategorylogger = logging.getLogger(__name__)+class Source(Enum):+ WGER = 'wger'+ OPEN_FOOD_FACTS = 'Open Food Facts'+class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):"""@@ -436,6 +441,11 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):else:return 0+ @property+ def off_link(self):+ if self.source_name == Source.OPEN_FOOD_FACTS:+ return f'https://world.openfoodfacts.org/product/{self.code}/'+def get_image(self, request: HttpRequest):"""Returns the ingredient image@@ -450,7 +460,7 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):if not request.user.is_authenticated:return- if self.source_name != 'Open Food Facts':+ if self.source_name != Source.OPEN_FOOD_FACTS:returnif not settings.WGER_SETTINGS['DOWNLOAD_FROM_OFF']:@@ -460,7 +470,7 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):return# Everything looks fine, go ahead- logger.info(f'Trying to fetch image from OFF for {self.name}')+ logger.info(f'Trying to fetch image from OFF for {self.name} (UUID: {self.uuid})')headers = {'User-agent':default_user_agent(f'wger/{get_version()} - https://github.com/wger-project')diff --git a/wger/nutrition/templates/ingredient/view.html b/wger/nutrition/templates/ingredient/view.htmlindex 5dc69a050..33145a80d 100644--- a/wger/nutrition/templates/ingredient/view.html+++ b/wger/nutrition/templates/ingredient/view.html@@ -208,7 +208,9 @@ the search.{% endblocktranslate %}{% endif %}{% if image %}- <img src="{{ image.image.url }}" class="img-fluid">+ <a href="{{ ingredient.off_link }}">+ <img src="{{ image.image.url }}" class="img-fluid">+ </a>{% endif %}{% license_sidebar ingredient.license ingredient.license_author %}