Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Use function to return complete header object

Roland Geider7995c28b3614
diff --git a/wger/exercises/management/commands/download-exercise-images.py b/wger/exercises/management/commands/download-exercise-images.py
index 3a21676d8..3694148e5 100644
--- a/wger/exercises/management/commands/download-exercise-images.py
+++ b/wger/exercises/management/commands/download-exercise-images.py
@@ -39,8 +39,7 @@ from wger.exercises.models import (
ExerciseBase,
ExerciseImage,
)
-from wger.utils.requests import wger_user_agent
-
+from wger.utils.requests import wger_headers
IMAGE_API = "{0}/api/v2/exerciseimage/?status=2"
@@ -84,7 +83,7 @@ class Command(BaseCommand):
except ValidationError:
raise CommandError('Please enter a valid URL')
- headers = {'User-agent': wger_user_agent()}
+ headers = wger_headers()
# Get all images
page = 1
diff --git a/wger/exercises/management/commands/download-exercise-videos.py b/wger/exercises/management/commands/download-exercise-videos.py
index c5cea42db..790b2ad22 100644
--- a/wger/exercises/management/commands/download-exercise-videos.py
+++ b/wger/exercises/management/commands/download-exercise-videos.py
@@ -39,8 +39,7 @@ from wger.exercises.models import (
ExerciseBase,
ExerciseVideo,
)
-from wger.utils.requests import wger_user_agent
-
+from wger.utils.requests import wger_headers
VIDEO_API = "{0}/api/v2/video/"
@@ -77,7 +76,7 @@ class Command(BaseCommand):
except ValidationError:
raise CommandError('Please enter a valid URL')
- headers = {'User-agent': wger_user_agent()}
+ headers = wger_headers()
# Get all videos
page = 1
diff --git a/wger/exercises/management/commands/sync-exercises.py b/wger/exercises/management/commands/sync-exercises.py
index 5eaa174b8..0fc25e12e 100644
--- a/wger/exercises/management/commands/sync-exercises.py
+++ b/wger/exercises/management/commands/sync-exercises.py
@@ -33,8 +33,7 @@ from wger.exercises.models import (
ExerciseCategory,
Muscle,
)
-from wger.utils.requests import wger_user_agent
-
+from wger.utils.requests import wger_headers
EXERCISE_API = "{0}/api/v2/exerciseinfo/?limit=100"
CATEGORY_API = "{0}/api/v2/exercisecategory/"
@@ -76,7 +75,7 @@ class Command(BaseCommand):
except ValidationError:
raise CommandError('Please enter a valid URL')
- headers = {'User-agent': wger_user_agent()}
+ headers = wger_headers()
self.sync_categories(headers, remote_url)
self.sync_muscles(headers, remote_url)
self.sync_equipment(headers, remote_url)
diff --git a/wger/nutrition/tasks.py b/wger/nutrition/tasks.py
index 88a57cbab..e51fa98a7 100644
--- a/wger/nutrition/tasks.py
+++ b/wger/nutrition/tasks.py
@@ -28,8 +28,7 @@ from celery import shared_task
# wger
from wger.nutrition.models import Image
from wger.nutrition.models.sources import Source
-from wger.utils.requests import wger_user_agent
-
+from wger.utils.requests import wger_headers
logger = logging.getLogger(__name__)
@@ -69,7 +68,7 @@ def fetch_ingredient_image_function(pk: int):
# Everything looks fine, go ahead
logger.info(f'Trying to fetch image from OFF for {ingredient.name} (UUID: {ingredient.uuid})')
- headers = {'User-agent': wger_user_agent()}
+ headers = wger_headers()
# Fetch the product data
product_data = requests.get(ingredient.source_url, headers=headers).json()
diff --git a/wger/nutrition/tests/test_tasks.py b/wger/nutrition/tests/test_tasks.py
index be00e0e9d..0968b1f97 100644
--- a/wger/nutrition/tests/test_tasks.py
+++ b/wger/nutrition/tests/test_tasks.py
@@ -27,8 +27,7 @@ from wger.nutrition.tasks import (
fetch_ingredient_image_function,
logger,
)
-from wger.utils.requests import wger_user_agent
-
+from wger.utils.requests import wger_headers
loggerMock = MagicMock()
@@ -116,13 +115,13 @@ class FetchIngredientImageTestCase(WgerTestCase):
self.assertTrue(
request1.called_with(
'https://world.openfoodfacts.org/api/v0/product/5055365635003.json',
- headers={'User-agent': wger_user_agent()}
+ headers=wger_headers()
)
)
self.assertTrue(
request2.called_with(
'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',
- headers={'User-agent': wger_user_agent()}
+ headers=wger_headers()
)
)
diff --git a/wger/utils/requests.py b/wger/utils/requests.py
index 4ed395869..2e570f03c 100644
--- a/wger/utils/requests.py
+++ b/wger/utils/requests.py
@@ -18,3 +18,7 @@ from wger import get_version
def wger_user_agent():
return f'wger/{get_version()} - https://github.com/wger-project'
+
+
+def wger_headers():
+ return {'User-agent': wger_user_agent()}