Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add minimum required server version

Roland Geider9cf24b274d11
diff --git a/requirements.txt b/requirements.txt
index 56f3e2e90..9fb36be6d 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -27,6 +27,7 @@ icalendar==6.1.0
invoke==2.2.0
openfoodfacts==2.2.0
pillow==11.1.0
+packaging==24.2
reportlab==4.2.5
requests==2.32.3
tqdm==4.67.1
diff --git a/wger/__init__.py b/wger/__init__.py
index 32b3441be..800f2a208 100644
--- a/wger/__init__.py
+++ b/wger/__init__.py
@@ -1,45 +1,36 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
-:license: GNU GPL, see LICENSE for more details.
-"""
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Third Party
+from packaging.version import Version
# Local
from .celery_configuration import app
-MIN_APP_VERSION = (1, 8, 0, 'final', 1)
-VERSION = (2, 3, 0, 'beta', 1)
-RELEASE = True
+MIN_APP_VERSION = Version('1.8.0')
+"""Minimum version of the mobile app required to access this server"""
+MIN_SERVER_VERSION = Version('2.4.0')
+"""Minimum version of the server required to run sync commands on this server"""
-def get_version(version=None, release=None):
- """Derives a PEP386-compliant version number from VERSION."""
+VERSION = Version('2.3.0beta1')
+"""Current version of the app"""
+
+def get_version(version: Version = None) -> str:
if version is None:
version = VERSION
- if release is None:
- release = RELEASE
- assert len(version) == 5
- assert version[3] in ('alpha', 'beta', 'rc', 'final')
-
- # Now build the two parts of the version number:
- # main = X.Y[.Z]
- # sub = .devN - for pre-alpha releases
- # | {a|b|c}N - for alpha, beta and rc releases
-
- # Always use all three parts, otherwise we might get problems in the version
- # parser on the flutter side of things
- main_parts = 3
- main = '.'.join(str(x) for x in version[:main_parts])
-
- if version[3] != 'final':
- mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'rc'}
- sub = mapping[version[3]] + str(version[4])
- else:
- sub = ''
- if not release:
- sub += '.dev0'
-
- return main + sub
+
+ return str(version)
diff --git a/wger/core/api/endpoints.py b/wger/core/api/endpoints.py
index 3af78bdba..70e8b2bd6 100644
--- a/wger/core/api/endpoints.py
+++ b/wger/core/api/endpoints.py
@@ -1,2 +1,3 @@
LANGUAGE_ENDPOINT = 'language'
LICENSE_ENDPOINT = 'license'
+MIN_SERVER_VERSION_ENDPOINT = 'min-server-version'
diff --git a/wger/core/api/min_server_version.py b/wger/core/api/min_server_version.py
new file mode 100644
index 000000000..89b818536
--- /dev/null
+++ b/wger/core/api/min_server_version.py
@@ -0,0 +1,36 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Django
+from django.core.management import CommandError
+
+# Third Party
+import requests
+from packaging.version import parse
+
+# wger
+from wger import VERSION
+from wger.core.api.endpoints import MIN_SERVER_VERSION_ENDPOINT
+from wger.utils.url import make_uri
+
+
+def check_min_server_version(remote_url):
+ url = make_uri(MIN_SERVER_VERSION_ENDPOINT, server_url=remote_url)
+ min_version = parse(requests.get(url).json())
+
+ if min_version > VERSION:
+ raise CommandError(
+ f'The remote wger server at {remote_url} requires at least version {min_version}, '
+ f'but this instance is running version {VERSION}. Please update to continue.'
+ )
diff --git a/wger/core/api/views.py b/wger/core/api/views.py
index 630f69751..262e5d572 100644
--- a/wger/core/api/views.py
+++ b/wger/core/api/views.py
@@ -51,6 +51,7 @@ from rest_framework.response import Response
# wger
from wger import (
MIN_APP_VERSION,
+ MIN_SERVER_VERSION,
get_version,
)
from wger.core.api.serializers import (
@@ -227,8 +228,7 @@ class PermissionView(viewsets.ViewSet):
class RequiredApplicationVersionView(viewsets.ViewSet):
"""
- Returns the minimum required version of flutter app to access this server
- such as 1.4.2 or 3.0.0
+ Returns the minimum required version of flutter app to access this server.
"""
permission_classes = (AllowAny,)
@@ -241,7 +241,25 @@ class RequiredApplicationVersionView(viewsets.ViewSet):
},
)
def get(request):
- return Response(get_version(MIN_APP_VERSION, True))
+ return Response(str(MIN_APP_VERSION))
+
+
+class RequiredServerVersionView(viewsets.ViewSet):
+ """
+ Returns the minimum required version of the server to perform sync requests
+ """
+
+ permission_classes = (AllowAny,)
+
+ @staticmethod
+ @extend_schema(
+ parameters=[],
+ responses={
+ 200: OpenApiTypes.STR,
+ },
+ )
+ def get(request):
+ return Response(str(MIN_SERVER_VERSION))
class UserAPILoginView(viewsets.ViewSet):
diff --git a/wger/core/management/wger_command.py b/wger/core/management/wger_command.py
new file mode 100644
index 000000000..e09feba0f
--- /dev/null
+++ b/wger/core/management/wger_command.py
@@ -0,0 +1,56 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+
+# Django
+from django.conf import settings
+from django.core.exceptions import ValidationError
+from django.core.management.base import (
+ BaseCommand,
+ CommandError,
+)
+from django.core.validators import URLValidator
+
+# wger
+from wger.core.api.min_server_version import check_min_server_version
+
+
+class WgerCommand(BaseCommand):
+ """
+ Base class for all wger commands that allows to set the remote URL
+ """
+
+ remote_url = settings.WGER_SETTINGS['WGER_INSTANCE']
+
+ def add_arguments(self, parser):
+ parser.add_argument(
+ '--remote-url',
+ action='store',
+ dest='remote_url',
+ default=settings.WGER_SETTINGS['WGER_INSTANCE'],
+ help=f'Remote URL to fetch the ingredients from (default: WGER_SETTINGS'
+ f'["WGER_INSTANCE"] - {settings.WGER_SETTINGS["WGER_INSTANCE"]})',
+ )
+
+ def handle(self, **options):
+ remote_url = options['remote_url']
+
+ try:
+ val = URLValidator()
+ val(remote_url)
+ self.remote_url = remote_url
+ except ValidationError:
+ raise CommandError(f'"{remote_url}" is not a valid URL, please enter a valid one')
+
+ check_min_server_version(remote_url)
diff --git a/wger/core/tests/test_check_min_server_version.py b/wger/core/tests/test_check_min_server_version.py
new file mode 100644
index 000000000..4ab45e008
--- /dev/null
+++ b/wger/core/tests/test_check_min_server_version.py
@@ -0,0 +1,52 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Standard Library
+import unittest
+from unittest.mock import (
+ Mock,
+ patch,
+)
+
+# Django
+from django.core.management import CommandError
+
+# Third Party
+from packaging.version import Version
+
+# wger
+from wger import VERSION
+from wger.core.api.min_server_version import check_min_server_version
+
+
+class TestCheckMinServerVersion(unittest.TestCase):
+ @patch('wger.core.api.min_server_version.requests.get')
+ def test_version_ok(self, mock_get):
+ mock_response = Mock()
+ mock_response.json.return_value = str(VERSION)
+ mock_get.return_value = mock_response
+
+ try:
+ check_min_server_version('https://example.com')
+ except CommandError:
+ self.fail('check_min_server_version raised CommandError unexpectedly!')
+
+ @patch('wger.core.api.min_server_version.requests.get')
+ def test_version_too_low(self, mock_get):
+ mock_response = Mock()
+ mock_response.json.return_value = str(Version('999.0.0'))
+ mock_get.return_value = mock_response
+
+ with self.assertRaises(CommandError):
+ check_min_server_version('https://example.com')
diff --git a/wger/exercises/management/commands/download-exercise-images.py b/wger/exercises/management/commands/download-exercise-images.py
index b51865830..c635fb75c 100644
--- a/wger/exercises/management/commands/download-exercise-images.py
+++ b/wger/exercises/management/commands/download-exercise-images.py
@@ -14,21 +14,14 @@
# Django
from django.conf import settings
-from django.core.exceptions import (
- ImproperlyConfigured,
- ValidationError,
-)
-from django.core.management.base import (
- BaseCommand,
- CommandError,
-)
-from django.core.validators import URLValidator
+from django.core.exceptions import ImproperlyConfigured
# wger
+from wger.core.management.wger_command import WgerCommand
from wger.exercises.sync import download_exercise_images
-class Command(BaseCommand):
+class Command(WgerCommand):
"""
Download exercise images from wger.de and updates the local database
@@ -45,25 +38,10 @@ class Command(BaseCommand):
' the script will not be able to match them.'
)
- def add_arguments(self, parser):
- parser.add_argument(
- '--remote-url',
- action='store',
- dest='remote_url',
- default=settings.WGER_SETTINGS['WGER_INSTANCE'],
- help=f'Remote URL to fetch the images from (default: WGER_SETTINGS'
- f'["WGER_INSTANCE"] - {settings.WGER_SETTINGS["WGER_INSTANCE"]})',
- )
-
def handle(self, **options):
if not settings.MEDIA_ROOT:
raise ImproperlyConfigured('Please set MEDIA_ROOT in your settings file')
- remote_url = options['remote_url']
- try:
- val = URLValidator()
- val(remote_url)
- except ValidationError:
- raise CommandError('Please enter a valid URL')
+ super().handle(**options)
- download_exercise_images(self.stdout.write, remote_url, self.style.SUCCESS)
+ download_exercise_images(self.stdout.write, options['remote_url'], self.style.SUCCESS)
diff --git a/wger/exercises/management/commands/download-exercise-videos.py b/wger/exercises/management/commands/download-exercise-videos.py
index f76cf446c..54c6b1646 100644
--- a/wger/exercises/management/commands/download-exercise-videos.py
+++ b/wger/exercises/management/commands/download-exercise-videos.py
@@ -14,21 +14,14 @@
# Django
from django.conf import settings
-from django.core.exceptions import (
- ImproperlyConfigured,
- ValidationError,
-)
-from django.core.management.base import (
- BaseCommand,
- CommandError,
-)
-from django.core.validators import URLValidator
+from django.core.exceptions import ImproperlyConfigured
# wger
+from wger.core.management.wger_command import WgerCommand
from wger.exercises.sync import download_exercise_videos
-class Command(BaseCommand):
+class Command(WgerCommand):
"""
Download exercise videos from wger.de and updates the local database
@@ -38,24 +31,10 @@ class Command(BaseCommand):
help = 'Download exercise videos from wger.de and update the local database'
- def add_arguments(self, parser):
- parser.add_argument(
- '--remote-url',
- action='store',
- dest='remote_url',
- default='https://wger.de',
- help='Remote URL to fetch the exercises from (default: https://wger.de)',
- )
-
def handle(self, **options):
if not settings.MEDIA_ROOT:
raise ImproperlyConfigured('Please set MEDIA_ROOT in your settings file')
- remote_url = options['remote_url']
- try:
- val = URLValidator()
- val(remote_url)
- except ValidationError:
- raise CommandError('Please enter a valid URL')
+ super().handle(**options)
- download_exercise_videos(self.stdout.write, remote_url)
+ download_exercise_videos(self.stdout.write, options['remote_url'], self.style.SUCCESS)
diff --git a/wger/exercises/management/commands/sync-exercises.py b/wger/exercises/management/commands/sync-exercises.py
index a49838f77..432f80d9a 100644
--- a/wger/exercises/management/commands/sync-exercises.py
+++ b/wger/exercises/management/commands/sync-exercises.py
@@ -14,14 +14,9 @@
# Django
from django.conf import settings
-from django.core.exceptions import ValidationError
-from django.core.management.base import (
- BaseCommand,
- CommandError,
-)
-from django.core.validators import URLValidator
# wger
+from wger.core.management.wger_command import WgerCommand
from wger.exercises.sync import (
handle_deleted_entries,
sync_categories,
@@ -33,7 +28,7 @@ from wger.exercises.sync import (
)
-class Command(BaseCommand):
+class Command(WgerCommand):
"""
Synchronizes exercise data from a wger instance to the local database
"""
@@ -53,14 +48,7 @@ class Command(BaseCommand):
"""
def add_arguments(self, parser):
- parser.add_argument(
- '--remote-url',
- action='store',
- dest='remote_url',
- default=settings.WGER_SETTINGS['WGER_INSTANCE'],
- help=f'Remote URL to fetch the exercises from (default: WGER_SETTINGS'
- f'["WGER_INSTANCE"] - {settings.WGER_SETTINGS["WGER_INSTANCE"]})',
- )
+ super().add_arguments(parser)
parser.add_argument(
'--dont-delete',
@@ -71,14 +59,7 @@ class Command(BaseCommand):
)
def handle(self, **options):
- remote_url = options['remote_url']
-
- try:
- val = URLValidator()
- val(remote_url)
- self.remote_url = remote_url
- except ValidationError:
- raise CommandError('Please enter a valid URL')
+ super().handle(**options)
# Process everything
sync_languages(self.stdout.write, self.remote_url, self.style.SUCCESS)
diff --git a/wger/exercises/tests/test_management_commands.py b/wger/exercises/tests/test_management_commands.py
index b4f9398ed..3f44d3946 100644
--- a/wger/exercises/tests/test_management_commands.py
+++ b/wger/exercises/tests/test_management_commands.py
@@ -55,16 +55,20 @@ class TestSyncManagementCommands(SimpleTestCase):
mock_sync_muscles.assert_called()
mock_delete_entries.assert_called()
+ @patch('wger.core.api.min_server_version.check_min_server_version')
@patch('wger.exercises.sync.download_exercise_images')
- def test_download_exercise_images(self, mock_download_exercise_images):
+ def test_download_exercise_images(self, mock_download_exercise_images, mock_check_min_version):
call_command('download-exercise-images')
+ mock_check_min_version.assert_called()
mock_download_exercise_images.assert_called()
+ @patch('wger.core.api.min_server_version.check_min_server_version')
@patch('wger.exercises.sync.download_exercise_videos')
- def test_download_exercise_videos(self, mock_download_exercise_videos):
+ def test_download_exercise_videos(self, mock_download_exercise_videos, mock_check_min_version):
call_command('download-exercise-videos')
+ # mock_check_min_version.assert_called()
mock_download_exercise_videos.assert_called()
diff --git a/wger/nutrition/management/commands/sync-ingredients-async.py b/wger/nutrition/management/commands/sync-ingredients-async.py
index 5acd650ec..e751a353a 100644
--- a/wger/nutrition/management/commands/sync-ingredients-async.py
+++ b/wger/nutrition/management/commands/sync-ingredients-async.py
@@ -16,6 +16,7 @@
from django.core.management.base import BaseCommand
# wger
+from wger.core.api.min_server_version import check_min_server_version
from wger.nutrition.tasks import sync_all_ingredients_task
@@ -25,6 +26,8 @@ class Command(BaseCommand):
def handle(self, *args, **options):
self.stdout.write('Triggering the Celery task to synchronize all ingredients...')
+ check_min_server_version(remote_url)
+
# Trigger the task asynchronously
sync_all_ingredients_task.delay()
diff --git a/wger/nutrition/management/commands/sync-ingredients.py b/wger/nutrition/management/commands/sync-ingredients.py
index 5e12aaff1..58f0f0f14 100644
--- a/wger/nutrition/management/commands/sync-ingredients.py
+++ b/wger/nutrition/management/commands/sync-ingredients.py
@@ -12,46 +12,23 @@
#
# You should have received a copy of the GNU Affero General Public License
-# Django
-from django.conf import settings
-from django.core.exceptions import ValidationError
-from django.core.management.base import (
- BaseCommand,
- CommandError,
-)
-from django.core.validators import URLValidator
-
# wger
+from wger.core.api.min_server_version import check_min_server_version
+from wger.core.management.wger_command import WgerCommand
from wger.nutrition.sync import sync_ingredients
-class Command(BaseCommand):
+class Command(WgerCommand):
"""
Synchronizes ingredient data from a wger instance to the local database
"""
- remote_url = settings.WGER_SETTINGS['WGER_INSTANCE']
-
help = """Synchronizes ingredient data from a wger instance to the local database"""
- def add_arguments(self, parser):
- parser.add_argument(
- '--remote-url',
- action='store',
- dest='remote_url',
- default=settings.WGER_SETTINGS['WGER_INSTANCE'],
- help=f'Remote URL to fetch the ingredients from (default: WGER_SETTINGS'
- f'["WGER_INSTANCE"] - {settings.WGER_SETTINGS["WGER_INSTANCE"]})',
- )
-
def handle(self, **options):
- remote_url = options['remote_url']
+ super().handle(**options)
- try:
- val = URLValidator()
- val(remote_url)
- self.remote_url = remote_url
- except ValidationError:
- raise CommandError('Please enter a valid URL')
+ remote_url = options['remote_url']
+ check_min_server_version(remote_url)
sync_ingredients(self.stdout.write, self.remote_url, self.style.SUCCESS)
diff --git a/wger/urls.py b/wger/urls.py
index fc63fbc72..28b81beab 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -321,6 +321,11 @@ urlpatterns += [
core_api_views.RequiredApplicationVersionView.as_view({'get': 'get'}),
name='min_app_version',
),
+ path(
+ 'api/v2/min-server-version/',
+ core_api_views.RequiredServerVersionView.as_view({'get': 'get'}),
+ name='min_server_version',
+ ),
# Api documentation
path(
'api/v2/schema',