Self hosted FLOSS fitness/workout, nutrition and weight tracker
Sync licenses as well
Roland Geider078d71cd1eb4
diff --git a/wger/core/api/endpoints.py b/wger/core/api/endpoints.pyindex 68ce8db5f..947b78508 100644--- a/wger/core/api/endpoints.py+++ b/wger/core/api/endpoints.py@@ -1 +1,2 @@LANGUAGE_ENDPOINT = "language"+LICENSE_ENDPOINT = "license"diff --git a/wger/exercises/management/commands/read-exercise-cleanup.py b/wger/exercises/management/commands/read-exercise-cleanup.pyindex 15b834b41..eacc3b4c9 100644--- a/wger/exercises/management/commands/read-exercise-cleanup.py+++ b/wger/exercises/management/commands/read-exercise-cleanup.py@@ -37,6 +37,7 @@ from wger.exercises.models import ()from wger.utils.constants import CC_BY_SA_4_ID+UUID_NEW = 'NEW'VIDEO_AUTHOR = 'Goulart'VIDEO_PATH = pathlib.Path('videos-tmp')diff --git a/wger/exercises/management/commands/sync-exercises.py b/wger/exercises/management/commands/sync-exercises.pyindex 6d1672272..b72023ef8 100644--- a/wger/exercises/management/commands/sync-exercises.py+++ b/wger/exercises/management/commands/sync-exercises.py@@ -28,6 +28,7 @@ from wger.exercises.sync import (sync_equipment,sync_exercises,sync_languages,+ sync_licenses,sync_muscles,)@@ -84,6 +85,7 @@ class Command(BaseCommand):sync_categories(self.stdout.write, self.remote_url, self.style.SUCCESS)sync_muscles(self.stdout.write, self.remote_url, self.style.SUCCESS)sync_equipment(self.stdout.write, self.remote_url, self.style.SUCCESS)+ sync_licenses(self.stdout.write, self.remote_url, self.style.SUCCESS)sync_exercises(self.stdout.write, self.remote_url, self.style.SUCCESS)if not options['skip_delete']:delete_entries(self.stdout.write, self.remote_url, self.style.SUCCESS)diff --git a/wger/exercises/sync.py b/wger/exercises/sync.pyindex 041744b8a..1e2377d93 100644--- a/wger/exercises/sync.py+++ b/wger/exercises/sync.py@@ -24,8 +24,14 @@ from django.core.files.temp import NamedTemporaryFileimport requests# wger-from wger.core.api.endpoints import LANGUAGE_ENDPOINT-from wger.core.models import Language+from wger.core.api.endpoints import (+ LANGUAGE_ENDPOINT,+ LICENSE_ENDPOINT,+)+from wger.core.models import (+ Language,+ License,+)from wger.exercises.api.endpoints import (CATEGORY_ENDPOINT,DELETION_LOG_ENDPOINT,@@ -149,6 +155,35 @@ def sync_languages(print_fn(style_fn('done!\n'))+def sync_licenses(+ print_fn,+ remote_url=settings.WGER_SETTINGS['WGER_INSTANCE'],+ style_fn=lambda x: x,+):+ """Synchronize the lincenses from the remote server"""+ print_fn('*** Synchronizing licenses...')+ headers = wger_headers()+ url = make_uri(LICENSE_ENDPOINT, server_url=remote_url)+ result = requests.get(url, headers=headers).json()+ for data in result['results']:+ short_name = data['short_name']+ full_name = data['full_name']+ license_url = data['url']++ language, created = License.objects.update_or_create(+ short_name=short_name,+ defaults={+ 'full_name': full_name,+ 'url': license_url+ },+ )++ if created:+ print_fn(f'Saved new license {full_name}')++ print_fn(style_fn('done!\n'))++def sync_categories(print_fn,remote_url=settings.WGER_SETTINGS['WGER_INSTANCE'],diff --git a/wger/exercises/tasks.py b/wger/exercises/tasks.pyindex ad183612a..757bacb55 100644--- a/wger/exercises/tasks.py+++ b/wger/exercises/tasks.py@@ -32,6 +32,7 @@ from wger.exercises.sync import (sync_equipment,sync_exercises,sync_languages,+ sync_licenses,sync_muscles,)@@ -45,11 +46,11 @@ def sync_exercises_task():Fetches the current exercises from the default wger instance"""sync_languages(logger.info)+ sync_licenses(logger.info)sync_categories(logger.info)sync_muscles(logger.info)sync_equipment(logger.info)sync_exercises(logger.info)- sync_exercises(logger.info)delete_entries(logger.info)diff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.pyindex 6faaaea2a..001034fae 100644--- a/wger/exercises/tests/test_sync.py+++ b/wger/exercises/tests/test_sync.py@@ -16,7 +16,10 @@from unittest.mock import patch# wger-from wger.core.models import Language+from wger.core.models import (+ Language,+ License,+)from wger.core.tests.base_testcase import WgerTestCasefrom wger.exercises.models import (Equipment,@@ -31,6 +34,7 @@ from wger.exercises.sync import (sync_equipment,sync_exercises,sync_languages,+ sync_licenses,sync_muscles,)from wger.utils.requests import wger_headers@@ -80,6 +84,43 @@ class MockLanguageResponse:# yapf: enable+class MockLicenseResponse:++ def __init__(self):+ self.status_code = 200+ self.content = b'1234'++ # yapf: disable+ @staticmethod+ def json():+ return {+ "count": 24,+ "next": "http://localhost:8000/api/v2/language/?limit=20&offset=20",+ "previous": None,+ "results": [+ {+ "id": 1,+ "full_name": "A cool and free license - Germany",+ "short_name": "ACAFL - DE",+ "url": "http://creativecommons.org/licenses/aca/fl/4.0/"+ },+ {+ "id": 2,+ "full_name": " Another cool license 2.1",+ "short_name": "ACL 2.1",+ "url": "https://another-cool-license.org/acl-2.1"+ },+ {+ "id": 3,+ "full_name": "Creative Commons Attribution Share Alike 4",+ "short_name": "CC-BY-SA 4",+ "url": "https://creativecommons.org/licenses/by-sa/4.0/deed.en"+ },+ ]+ }+ # yapf: enable++class MockCategoryResponse:def __init__(self):@@ -493,6 +534,24 @@ class TestSyncMethods(WgerTestCase):self.assertEqual(Language.objects.get(pk=5).full_name, 'Esperanto')self.assertEqual(Language.objects.count(), 5)+ @patch('requests.get', return_value=MockLicenseResponse())+ def test_license_sync(self, mock_request):+ self.assertEqual(License.objects.count(), 2)+ self.assertEqual(License.objects.get(pk=1).url, '')++ sync_licenses(lambda x: x)+ mock_request.assert_called_with(+ 'https://wger.de/api/v2/license/',+ headers=wger_headers(),+ )+ self.assertEqual(+ License.objects.get(pk=1).url, 'http://creativecommons.org/licenses/aca/fl/4.0/'+ )+ self.assertEqual(+ License.objects.get(pk=3).full_name, 'Creative Commons Attribution Share Alike 4'+ )+ self.assertEqual(License.objects.count(), 3)+@patch('requests.get', return_value=MockCategoryResponse())def test_categories_sync(self, mock_request):self.assertEqual(ExerciseCategory.objects.count(), 4)diff --git a/wger/nutrition/api/serializers.py b/wger/nutrition/api/serializers.pyindex 38984f85d..1a6098f8a 100644--- a/wger/nutrition/api/serializers.py+++ b/wger/nutrition/api/serializers.py@@ -265,7 +265,7 @@ class NutritionPlanSerializer(serializers.ModelSerializer):class Meta:model = NutritionPlan- exclude = ('user',)+ exclude = ('user', )class NutritionPlanInfoSerializer(serializers.ModelSerializer):diff --git a/wger/nutrition/api/views.py b/wger/nutrition/api/views.pyindex 68717b7eb..b99979382 100644--- a/wger/nutrition/api/views.py+++ b/wger/nutrition/api/views.py@@ -207,12 +207,7 @@ class ImageViewSet(viewsets.ReadOnlyModelViewSet):queryset = Image.objects.all()serializer_class = ImageSerializerordering_fields = '__all__'- filterset_fields = (- 'uuid',- 'source_url',- 'ingredient_id',- 'ingredient__uuid'- )+ filterset_fields = ('uuid', 'source_url', 'ingredient_id', 'ingredient__uuid')@method_decorator(cache_page(settings.WGER_SETTINGS['EXERCISE_CACHE_TTL']))def dispatch(self, request, *args, **kwargs):diff --git a/wger/nutrition/migrations/0013_ingredient_image.py b/wger/nutrition/migrations/0013_ingredient_image.pyindex bafb33854..b282a3ba2 100644--- a/wger/nutrition/migrations/0013_ingredient_image.py+++ b/wger/nutrition/migrations/0013_ingredient_image.py@@ -57,7 +57,7 @@ class Migration(migrations.Migration):models.CharField(blank=True,help_text='If you are not the author, enter the name or source here. This '- 'is needed for some licenses e.g. the CC-BY-SA.',+ 'is needed for some licenses e.g. the CC-BY-SA.',max_length=60,null=True,verbose_name='Author',diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.pyindex 3f6769c11..711ec2f0e 100644--- a/wger/nutrition/models/ingredient.py+++ b/wger/nutrition/models/ingredient.py@@ -54,6 +54,7 @@ from ..consts import ENERGY_FACTORfrom .ingredient_category import IngredientCategoryfrom .sources import Source+logger = logging.getLogger(__name__)diff --git a/wger/nutrition/sync.py b/wger/nutrition/sync.pyindex fb39eac9a..0f0ea6318 100644--- a/wger/nutrition/sync.py+++ b/wger/nutrition/sync.py@@ -39,6 +39,7 @@ from wger.utils.constants import (from wger.utils.requests import wger_headersfrom wger.utils.url import make_uri+logger = logging.getLogger(__name__)diff --git a/wger/settings_global.py b/wger/settings_global.pyindex d24ef7046..4c096a481 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -23,6 +23,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:@@ -171,7 +172,8 @@ TEMPLATES = ['django.template.loaders.filesystem.Loader','django.template.loaders.app_directories.Loader',],- 'debug': False+ 'debug':+ False},},]@@ -254,7 +256,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'diff --git a/wger/utils/constants.py b/wger/utils/constants.pyindex 2abab039f..b2992c457 100644--- a/wger/utils/constants.py+++ b/wger/utils/constants.py@@ -17,6 +17,7 @@# Standard Libraryfrom decimal import Decimal+# NavigationWORKOUT_TAB = 'workout'EXERCISE_TAB = 'exercises'@@ -42,7 +43,7 @@ DATE_FORMATS = [# editor. Everything not in these whitelists is stripped.HTML_TAG_WHITELIST = {'b', 'i', 'strong', 'em', 'ul', 'ol', 'li', 'p'}HTML_ATTRIBUTES_WHITELIST = {'*': 'style'}-HTML_STYLES_WHITELIST = ('text-decoration',)+HTML_STYLES_WHITELIST = ('text-decoration', )# PaginationPAGINATION_OBJECTS_PER_PAGE = 25diff --git a/wger/utils/models.py b/wger/utils/models.pyindex 536c1b2ea..e36daa11b 100644--- a/wger/utils/models.py+++ b/wger/utils/models.py@@ -22,6 +22,7 @@ from django.utils.translation import gettext_lazy as _from wger.core.models import Licensefrom wger.utils.constants import CC_BY_SA_4_ID+"""Abstract model classes"""