Self hosted FLOSS fitness/workout, nutrition and weight tracker
Move endpoints to their own files to reduce duplication
Roland Geider16b74c1c512a
diff --git a/wger/core/api/endpoints.py b/wger/core/api/endpoints.pynew file mode 100644index 000000000..68f2f6818--- /dev/null+++ b/wger/core/api/endpoints.py@@ -0,0 +1 @@+LANGUAGE_ENDPOINT = "{0}/api/v2/language/"diff --git a/wger/core/checks.py b/wger/core/checks.pyindex 738339e03..56c8fb71b 100644--- a/wger/core/checks.py+++ b/wger/core/checks.py@@ -31,7 +31,7 @@ def settings_check(app_configs, **kwargs):Error('Ingredient images configuration error',hint=f'Origin for ingredient images misconfigured. Valid options are '- f'{DOWNLOAD_INGREDIENT_OPTIONS}',+ f'{DOWNLOAD_INGREDIENT_OPTIONS}',obj=settings,id='wger.E001',)diff --git a/wger/core/tests/base_testcase.py b/wger/core/tests/base_testcase.pyindex b1c4a489c..49300d20c 100644--- a/wger/core/tests/base_testcase.py+++ b/wger/core/tests/base_testcase.py@@ -33,6 +33,7 @@ from django.utils.translation import activate# wgerfrom wger.utils.constants import TWOPLACES+STATUS_CODES_FAIL = (302, 403, 404)@@ -75,6 +76,7 @@ def delete_testcase_add_methods(cls):"""for user in get_user_list(cls.user_fail):+def test_unauthorized(self):self.user_login(user)self.delete_object(fail=False)@@ -82,6 +84,7 @@ def delete_testcase_add_methods(cls):setattr(cls, f'test_unauthorized_{user}', test_unauthorized)for user in get_user_list(cls.user_success):+def test_authorized(self):self.user_login(user)self.delete_object(fail=False)diff --git a/wger/exercises/api/endpoints.py b/wger/exercises/api/endpoints.pynew file mode 100644index 000000000..ab5e165cc--- /dev/null+++ b/wger/exercises/api/endpoints.py@@ -0,0 +1,7 @@+EXERCISE_ENDPOINT = "{0}/api/v2/exercisebaseinfo/?limit=100"+DELETION_LOG_ENDPOINT = "{0}/api/v2/deletion-log/?limit=100"+CATEGORY_ENDPOINT = "{0}/api/v2/exercisecategory/"+MUSCLE_ENDPOINT = "{0}/api/v2/muscle/"+EQUIPMENT_ENDPOINT = "{0}/api/v2/equipment/"+IMAGE_ENDPOINT = "{0}/api/v2/exerciseimage/"+VIDEO_ENDPOINT = "{0}/api/v2/video/"diff --git a/wger/exercises/sync.py b/wger/exercises/sync.pyindex f79403c4d..9ad663843 100644--- a/wger/exercises/sync.py+++ b/wger/exercises/sync.py@@ -23,7 +23,17 @@ from django.core.files.temp import NamedTemporaryFileimport requests# wger+from wger.core.api.endpoints import LANGUAGE_ENDPOINTfrom wger.core.models import Language+from wger.exercises.api.endpoints import (+ CATEGORY_ENDPOINT,+ DELETION_LOG_ENDPOINT,+ EQUIPMENT_ENDPOINT,+ EXERCISE_ENDPOINT,+ IMAGE_ENDPOINT,+ MUSCLE_ENDPOINT,+ VIDEO_ENDPOINT,+)from wger.exercises.models import (DeletionLog,Equipment,@@ -37,16 +47,6 @@ from wger.exercises.models import (from wger.utils.requests import wger_headers-EXERCISE_API = "{0}/api/v2/exercisebaseinfo/?limit=100"-DELETION_LOG_API = "{0}/api/v2/deletion-log/?limit=100"-CATEGORY_API = "{0}/api/v2/exercisecategory/"-MUSCLE_API = "{0}/api/v2/muscle/"-LANGUAGE_API = "{0}/api/v2/language/"-EQUIPMENT_API = "{0}/api/v2/equipment/"-IMAGE_API = "{0}/api/v2/exerciseimage/"-VIDEO_API = "{0}/api/v2/video/"--def sync_exercises(print_fn,remote_url=settings.WGER_SETTINGS['WGER_INSTANCE'],@@ -58,7 +58,7 @@ def sync_exercises(print_fn('*** Synchronizing exercises...')page = 1all_exercise_processed = False- result = requests.get(EXERCISE_API.format(remote_url), headers=headers).json()+ result = requests.get(EXERCISE_ENDPOINT.format(remote_url), headers=headers).json()while not all_exercise_processed:for data in result['results']:@@ -124,7 +124,7 @@ def sync_languages(headers = wger_headers()print_fn('*** Synchronizing languages...')- result = requests.get(LANGUAGE_API.format(remote_url), headers=headers).json()+ result = requests.get(LANGUAGE_ENDPOINT.format(remote_url), headers=headers).json()for data in result['results']:short_name = data['short_name']full_name = data['full_name']@@ -150,7 +150,7 @@ def sync_categories(headers = wger_headers()print_fn('*** Synchronizing categories...')- result = requests.get(CATEGORY_API.format(remote_url), headers=headers).json()+ result = requests.get(CATEGORY_ENDPOINT.format(remote_url), headers=headers).json()for data in result['results']:category_id = data['id']category_name = data['name']@@ -176,7 +176,7 @@ def sync_muscles(headers = wger_headers()print_fn('*** Synchronizing muscles...')- result = requests.get(MUSCLE_API.format(remote_url), headers=headers).json()+ result = requests.get(MUSCLE_ENDPOINT.format(remote_url), headers=headers).json()for data in result['results']:muscle_id = data['id']muscle_name = data['name']@@ -212,7 +212,7 @@ def sync_equipment(headers = wger_headers()print_fn('*** Synchronizing equipment...')- result = requests.get(EQUIPMENT_API.format(remote_url), headers=headers).json()+ result = requests.get(EQUIPMENT_ENDPOINT.format(remote_url), headers=headers).json()for data in result['results']:equipment_id = data['id']equipment_name = data['name']@@ -241,7 +241,7 @@ def delete_entries(page = 1all_entries_processed = False- result = requests.get(DELETION_LOG_API.format(remote_url), headers=headers).json()+ result = requests.get(DELETION_LOG_ENDPOINT.format(remote_url), headers=headers).json()while not all_entries_processed:for data in result['results']:uuid = data['uuid']@@ -297,7 +297,7 @@ def download_exercise_images(# Get all imagespage = 1all_images_processed = False- result = requests.get(IMAGE_API.format(remote_url), headers=headers).json()+ result = requests.get(IMAGE_ENDPOINT.format(remote_url), headers=headers).json()print_fn('*** Processing images ***')while not all_images_processed:print_fn('')@@ -363,7 +363,7 @@ def download_exercise_videos(# Get all videospage = 1all_videos_processed = False- result = requests.get(VIDEO_API.format(remote_url), headers=headers).json()+ result = requests.get(VIDEO_ENDPOINT.format(remote_url), headers=headers).json()print_fn('*** Processing videos ***')while not all_videos_processed:print_fn('')diff --git a/wger/nutrition/api/endpoints.py b/wger/nutrition/api/endpoints.pynew file mode 100644index 000000000..292cddcee--- /dev/null+++ b/wger/nutrition/api/endpoints.py@@ -0,0 +1 @@+IMAGE_ENDPOINT = "{0}/api/v2/ingredient-image/"diff --git a/wger/nutrition/management/commands/download-off-ingredient-images.py b/wger/nutrition/management/commands/download-off-ingredient-images.pyindex 7a578776a..29fc19e50 100644--- a/wger/nutrition/management/commands/download-off-ingredient-images.py+++ b/wger/nutrition/management/commands/download-off-ingredient-images.py@@ -23,6 +23,7 @@ from django.core.management.base import BaseCommandfrom wger.nutrition.models import MealItemfrom wger.nutrition.tasks import fetch_ingredient_image_task+logger = logging.getLogger(__name__)diff --git a/wger/nutrition/sync.py b/wger/nutrition/sync.pyindex cbe6ea99b..ace2c52fc 100644--- a/wger/nutrition/sync.py+++ b/wger/nutrition/sync.py@@ -25,6 +25,7 @@ from django.db import IntegrityErrorimport requests# wger+from wger.nutrition.api.endpoints import IMAGE_ENDPOINTfrom wger.nutrition.models import (Image,Ingredient,@@ -36,9 +37,8 @@ from wger.utils.constants import ()from wger.utils.requests import wger_headers-logger = logging.getLogger(__name__)-IMAGE_API = "{0}/api/v2/ingredient-image/"+logger = logging.getLogger(__name__)def fetch_ingredient_image(pk: int):@@ -67,7 +67,7 @@ def fetch_ingredient_image(pk: int):def fetch_image_from_wger_instance(ingredient):- url = f"{settings.WGER_SETTINGS['WGER_INSTANCE']}/api/v2/ingredient-image/{ingredient.pk}/"+ url = f"{IMAGE_ENDPOINT.format(settings.WGER_SETTINGS['WGER_INSTANCE'])}/{ingredient.pk}/"logger.info(f'Trying to fetch image from WGER for {ingredient.name} (UUID: {ingredient.uuid})')result = requests.get(url, headers=wger_headers()).json()image_uuid = result['uuid']@@ -137,7 +137,7 @@ def download_ingredient_images(# Get all imagespage = 1all_images_processed = False- result = requests.get(IMAGE_API.format(remote_url), headers=headers).json()+ result = requests.get(IMAGE_ENDPOINT.format(remote_url), headers=headers).json()print_fn('*** Processing images ***')while not all_images_processed:print_fn('')diff --git a/wger/nutrition/tests/test_tasks.py b/wger/nutrition/tests/test_tasks.pyindex ee60f9e8f..47c95ee6f 100644--- a/wger/nutrition/tests/test_tasks.py+++ b/wger/nutrition/tests/test_tasks.py@@ -42,7 +42,7 @@ class MockOffResponse:return {"product": {'image_front_url':- 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',+ 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg','images': {'front_en': {'imgid': '12345',@@ -69,7 +69,7 @@ class MockWgerApiResponse:"ingredient_id": "12345","ingredient_uuid": "e9baa8bd-84fc-4756-8d90-5b9739b06cf8","image": "http://localhost:8000/media/ingredients/e9baa8bd-84fc-4756-8d90-5b9739b06cf8"- "/188324b5-587f-42d7-9abc-d2ca64c73d45.jpg",+ "/188324b5-587f-42d7-9abc-d2ca64c73d45.jpg","last_update": "2023-03-15T23:20:10.969369+01:00","size": 20179,"source_url": "",diff --git a/wger/settings_global.py b/wger/settings_global.pyindex f00610b6d..e83305cbd 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -24,6 +24,7 @@ from datetime import timedelta# wgerfrom wger.utils.constants import DOWNLOAD_INGREDIENT_WGER+"""This file contains the global settings that don't usually need to be changed.For a full list of options, visit:@@ -170,7 +171,7 @@ TEMPLATES = ['django.template.loaders.app_directories.Loader',],'debug':- False+ False},},]@@ -253,7 +254,7 @@ AVAILABLE_LANGUAGES = (LANGUAGE_CODE = 'en'# All translation files are in one place-LOCALE_PATHS = (os.path.join(SITE_ROOT, 'locale'),)+LOCALE_PATHS = (os.path.join(SITE_ROOT, 'locale'), )# Primary keys are AutoFieldsDEFAULT_AUTO_FIELD = 'django.db.models.AutoField'@@ -426,11 +427,15 @@ COMPRESS_ROOT = STATIC_ROOT# Django Rest Framework#REST_FRAMEWORK = {- 'DEFAULT_PERMISSION_CLASSES': ('wger.utils.permissions.WgerPermission',),- 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',- 'PAGE_SIZE': 20,- 'PAGINATE_BY_PARAM': 'limit', # Allow client to override, using `?limit=xxx`.- 'TEST_REQUEST_DEFAULT_FORMAT': 'json',+ 'DEFAULT_PERMISSION_CLASSES': ('wger.utils.permissions.WgerPermission', ),+ 'DEFAULT_PAGINATION_CLASS':+ 'rest_framework.pagination.LimitOffsetPagination',+ 'PAGE_SIZE':+ 20,+ 'PAGINATE_BY_PARAM':+ 'limit', # Allow client to override, using `?limit=xxx`.+ 'TEST_REQUEST_DEFAULT_FORMAT':+ 'json','DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework.authentication.SessionAuthentication','rest_framework.authentication.TokenAuthentication',@@ -466,7 +471,7 @@ CORS_URLS_REGEX = r'^/api/.*$'## Ignore these URLs if they cause 404#-IGNORABLE_404_URLS = (re.compile(r'^/favicon\.ico$'),)+IGNORABLE_404_URLS = (re.compile(r'^/favicon\.ico$'), )## Password rules