Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Automatically log deleted exercises and their images

Roland Geider0dfd434f6911
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index 5692083d8..7d1b2f323 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -23,6 +23,7 @@ from rest_framework import serializers
# wger
from wger.exercises.models import (
Alias,
+ DeletionLog,
Equipment,
Exercise,
ExerciseBase,
@@ -66,6 +67,21 @@ class EquipmentSerializer(serializers.ModelSerializer):
fields = ['id', 'name']
+class DeletionLogSerializer(serializers.ModelSerializer):
+ """
+ Deletion log serializer
+ """
+
+ class Meta:
+ model = DeletionLog
+ fields = [
+ 'model_type',
+ 'uuid',
+ 'timestamp',
+ 'comment',
+ ]
+
+
class ExerciseImageSerializer(serializers.ModelSerializer):
"""
ExerciseImage serializer
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index d2d731365..6cc898431 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -21,7 +21,6 @@ import logging
# Django
from django.conf import settings
from django.db.models import Q
-from django.shortcuts import get_object_or_404
from django.utils.decorators import method_decorator
from django.utils.translation import gettext as _
from django.views.decorators.cache import cache_page
@@ -41,9 +40,9 @@ from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
# wger
-from wger.core.models import Language
from wger.exercises.api.permissions import CanContributeExercises
from wger.exercises.api.serializers import (
+ DeletionLogSerializer,
EquipmentSerializer,
ExerciseAliasSerializer,
ExerciseBaseInfoSerializer,
@@ -60,6 +59,7 @@ from wger.exercises.api.serializers import (
)
from wger.exercises.models import (
Alias,
+ DeletionLog,
Equipment,
Exercise,
ExerciseBase,
@@ -271,10 +271,10 @@ def search(request):
return Response(response)
language = load_language(language_code)
- exercises = Exercise.objects\
- .filter(Q(name__icontains=q) | Q(alias__alias__icontains=q))\
- .filter(language=language)\
- .order_by('exercise_base__category__name', 'name')\
+ exercises = Exercise.objects \
+ .filter(Q(name__icontains=q) | Q(alias__alias__icontains=q)) \
+ .filter(language=language) \
+ .order_by('exercise_base__category__name', 'name') \
.distinct()
for exercise in exercises:
@@ -364,6 +364,16 @@ class EquipmentViewSet(viewsets.ReadOnlyModelViewSet):
return super().dispatch(request, *args, **kwargs)
+class DeletionLogViewSet(viewsets.ReadOnlyModelViewSet):
+ """
+ API endpoint for exercise deletion logs
+ """
+ queryset = DeletionLog.objects.all()
+ serializer_class = DeletionLogSerializer
+ ordering_fields = '__all__'
+ filterset_fields = ('model_type', )
+
+
class ExerciseCategoryViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint for exercise categories objects
diff --git a/wger/exercises/management/commands/sync-exercises.py b/wger/exercises/management/commands/sync-exercises.py
index 25ae541c4..4948dc1dd 100644
--- a/wger/exercises/management/commands/sync-exercises.py
+++ b/wger/exercises/management/commands/sync-exercises.py
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 *-*
-
# This file is part of wger Workout Manager.
#
# wger Workout Manager is free software: you can redistribute it and/or modify
@@ -29,15 +27,19 @@ from requests.utils import default_user_agent
# wger
from wger import get_version
from wger.exercises.models import (
+ DeletionLog,
Equipment,
Exercise,
ExerciseBase,
ExerciseCategory,
+ ExerciseImage,
+ ExerciseVideo,
Muscle,
)
EXERCISE_API = "{0}/api/v2/exerciseinfo/?limit=100"
+DELETION_LOG_API = "{0}/api/v2/deletion-log/?limit=100"
CATEGORY_API = "{0}/api/v2/exercisecategory/"
MUSCLE_API = "{0}/api/v2/muscle/"
EQUIPMENT_API = "{0}/api/v2/equipment/"
@@ -47,8 +49,12 @@ class Command(BaseCommand):
"""
Synchronizes exercise data from a wger instance to the local database
"""
+ remote_url = 'https://wger.de'
+ headers = {}
help = """Synchronizes exercise data from a wger instance to the local database.
+ This script also deletes entries that were removed on the server such
+ as exercises, images or videos.
Please note that at the moment the following objects can only identified
by their id. If you added new objects they might have the same IDs as the
@@ -63,33 +69,46 @@ class Command(BaseCommand):
'--remote-url',
action='store',
dest='remote_url',
- default='https://wger.de',
- help='Remote URL to fetch the exercises from (default: '
- 'https://wger.de)'
+ default=self.remote_url,
+ help=f'Remote URL to fetch the exercises from (default: {self.remote_url})'
+ )
+
+ parser.add_argument(
+ '--dont-delete',
+ action='store_true',
+ dest='skip_delete',
+ default=True,
+ help='Skips deleting any entries'
)
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')
- headers = {'User-agent': default_user_agent('wger/{} + requests'.format(get_version()))}
- self.sync_categories(headers, remote_url)
- self.sync_muscles(headers, remote_url)
- self.sync_equipment(headers, remote_url)
- self.sync_exercises(headers, remote_url)
+ self.headers = {
+ 'User-agent': default_user_agent('wger/{} + requests'.format(get_version()))
+ }
+ self.sync_categories()
+ self.sync_muscles()
+ self.sync_equipment()
+ self.sync_exercises()
+ if not options['skip_delete']:
+ self.delete_entries()
- def sync_exercises(self, headers: dict, remote_url: str):
+ def sync_exercises(self):
"""Synchronize the exercises from the remote server"""
self.stdout.write('*** Synchronizing exercises...')
page = 1
all_exercise_processed = False
- result = requests.get(EXERCISE_API.format(remote_url), headers=headers).json()
+ result = requests.get(EXERCISE_API.format(self.remote_url), headers=self.headers).json()
while not all_exercise_processed:
for data in result['results']:
@@ -140,16 +159,68 @@ class Command(BaseCommand):
if result['next']:
page += 1
- result = requests.get(result['next'], headers=headers).json()
+ result = requests.get(result['next'], headers=self.headers).json()
else:
all_exercise_processed = True
self.stdout.write(self.style.SUCCESS('done!\n'))
- def sync_equipment(self, headers: dict, remote_url: str):
+ def delete_entries(self):
+ """Delete exercises that were removed on the server"""
+
+ self.stdout.write('*** Deleting exercises that were removed on the server...')
+
+ page = 1
+ all_entries_processed = False
+ result = requests.get(DELETION_LOG_API.format(self.remote_url), headers=self.headers).json()
+ while not all_entries_processed:
+ for data in result['results']:
+ uuid = data['uuid']
+ model_type = data['model_type']
+
+ if model_type == DeletionLog.MODEL_BASE:
+ try:
+ obj = ExerciseBase.objects.get(uuid=uuid)
+ obj.delete()
+ self.stdout.write(f'Deleted exercise base {uuid}')
+ except ExerciseBase.DoesNotExist:
+ pass
+
+ elif model_type == DeletionLog.MODEL_TRANSLATION:
+ try:
+ obj = Exercise.objects.get(uuid=uuid)
+ obj.delete()
+ self.stdout.write(f"Deleted translation {uuid} ({data['comment']})")
+ except Exercise.DoesNotExist:
+ pass
+
+ elif model_type == DeletionLog.MODEL_IMAGE:
+ try:
+ obj = ExerciseImage.objects.get(uuid=uuid)
+ obj.delete()
+ self.stdout.write(f'Deleted image {uuid}')
+ except ExerciseImage.DoesNotExist:
+ pass
+
+ elif model_type == DeletionLog.MODEL_VIDEO:
+ try:
+ obj = ExerciseVideo.objects.get(uuid=uuid)
+ obj.delete()
+ self.stdout.write(f'Deleted video {uuid}')
+ except ExerciseVideo.DoesNotExist:
+ pass
+
+ if result['next']:
+ page += 1
+ result = requests.get(result['next'], headers=self.headers).json()
+ else:
+ all_entries_processed = True
+ self.stdout.write(self.style.SUCCESS('done!\n'))
+
+ def sync_equipment(self):
"""Synchronize the equipment from the remote server"""
self.stdout.write('*** Synchronizing equipment...')
- result = requests.get(EQUIPMENT_API.format(remote_url), headers=headers).json()
+ result = requests.get(EQUIPMENT_API.format(self.remote_url), headers=self.headers).json()
for equipment_data in result['results']:
equipment_id = equipment_data['id']
equipment_name = equipment_data['name']
@@ -164,11 +235,11 @@ class Command(BaseCommand):
equipment.save()
self.stdout.write(self.style.SUCCESS('done!\n'))
- def sync_muscles(self, headers: dict, remote_url: str):
+ def sync_muscles(self):
"""Synchronize the muscles from the remote server"""
self.stdout.write('*** Synchronizing muscles...')
- result = requests.get(MUSCLE_API.format(remote_url), headers=headers).json()
+ result = requests.get(MUSCLE_API.format(self.remote_url), headers=self.headers).json()
for muscle_data in result['results']:
muscle_id = muscle_data['id']
muscle_name = muscle_data['name']
@@ -201,11 +272,11 @@ class Command(BaseCommand):
self.stdout.write(self.style.WARNING(muscle_url_secondary))
self.stdout.write(self.style.SUCCESS('done!\n'))
- def sync_categories(self, headers: dict, remote_url: str):
+ def sync_categories(self):
"""Synchronize the categories from the remote server"""
self.stdout.write('*** Synchronizing categories...')
- result = requests.get(CATEGORY_API.format(remote_url), headers=headers).json()
+ result = requests.get(CATEGORY_API.format(self.remote_url), headers=self.headers).json()
for category_data in result['results']:
category_id = category_data['id']
category_name = category_data['name']
diff --git a/wger/exercises/migrations/0021_deletionlog.py b/wger/exercises/migrations/0021_deletionlog.py
new file mode 100644
index 000000000..a02558c97
--- /dev/null
+++ b/wger/exercises/migrations/0021_deletionlog.py
@@ -0,0 +1,38 @@
+# Generated by Django 4.0.8 on 2023-01-23 17:41
+
+from django.db import migrations, models
+import uuid
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('exercises', '0020_historicalexerciseimage_historicalexercisevideo'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='DeletionLog',
+ fields=[
+ (
+ 'id',
+ models.AutoField(
+ auto_created=True, primary_key=True, serialize=False, verbose_name='ID'
+ )
+ ),
+ (
+ 'model_type',
+ models.CharField(
+ choices=[
+ ('base', 'base'), ('translation', 'translation'), ('image', 'image'),
+ ('video', 'video')
+ ],
+ max_length=11
+ )
+ ),
+ ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, verbose_name='UUID')),
+ ('timestamp', models.DateTimeField(auto_now=True)),
+ ('comment', models.CharField(default='', max_length=200)),
+ ],
+ ),
+ ]
diff --git a/wger/exercises/models/__init__.py b/wger/exercises/models/__init__.py
index 1a325dd99..ddb7e1366 100644
--- a/wger/exercises/models/__init__.py
+++ b/wger/exercises/models/__init__.py
@@ -18,6 +18,7 @@
from .base import ExerciseBase
from .category import ExerciseCategory
from .comment import ExerciseComment
+from .deletion_log import DeletionLog
from .equipment import Equipment
from .exercise import Exercise
from .exercise_alias import Alias
diff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.py
index 41cc2bc7a..a756e25a9 100644
--- a/wger/exercises/models/base.py
+++ b/wger/exercises/models/base.py
@@ -22,7 +22,6 @@ from typing import (
)
# Django
-from django.core.checks import translation
from django.db import models
from django.db.models import Q
from django.urls import reverse
diff --git a/wger/exercises/models/deletion_log.py b/wger/exercises/models/deletion_log.py
new file mode 100644
index 000000000..eb2b8e012
--- /dev/null
+++ b/wger/exercises/models/deletion_log.py
@@ -0,0 +1,58 @@
+# This file is part of wger Workout Manager <https://github.com/wger-project>.
+#
+# 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 Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Standard Library
+import uuid
+
+# Django
+from django.db import models
+
+
+class DeletionLog(models.Model):
+ """
+ Model to log deleted exercises
+
+ This is needed to keep the local instances in sync with upstream wger. Sometimes
+ it is necessary to delete entries, either because they are duplicated, the submission
+ wasn't of the needed quality, or other reasons and these would land in the
+ different DBs, without any way of cleaning them up.
+ """
+
+ MODEL_BASE = 'base'
+ MODEL_TRANSLATION = 'translation'
+ MODEL_IMAGE = 'image'
+ MODEL_VIDEO = 'video'
+
+ MODELS = [
+ (MODEL_BASE, 'base'),
+ (MODEL_TRANSLATION, 'translation'),
+ (MODEL_IMAGE, 'image'),
+ (MODEL_VIDEO, 'video'),
+ ]
+
+ model_type = models.CharField(
+ max_length=11,
+ choices=MODELS,
+ )
+
+ uuid = models.UUIDField(
+ default=uuid.uuid4,
+ editable=False,
+ verbose_name='UUID',
+ )
+
+ timestamp = models.DateTimeField(auto_now=True)
+
+ comment = models.CharField(max_length=200, default='')
diff --git a/wger/exercises/models/exercise.py b/wger/exercises/models/exercise.py
index 0db2ff9f2..27b4426ee 100644
--- a/wger/exercises/models/exercise.py
+++ b/wger/exercises/models/exercise.py
@@ -18,30 +18,20 @@
import uuid
# Django
-from django.conf import settings
-from django.contrib.auth.models import User
-from django.contrib.sites.models import Site
-from django.core import mail
from django.core.validators import MinLengthValidator
from django.db import models
-from django.template.loader import render_to_string
from django.urls import reverse
-from django.utils import translation
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
# Third Party
import bleach
-from actstream import action
from simple_history.models import HistoricalRecords
# wger
from wger.core.models import Language
from wger.exercises.models import ExerciseBase
-from wger.utils.cache import (
- delete_template_fragment_cache,
- reset_workout_canonical_form,
-)
+from wger.utils.cache import reset_workout_canonical_form
from wger.utils.models import (
AbstractHistoryMixin,
AbstractLicenseModel,
diff --git a/wger/exercises/signals.py b/wger/exercises/signals.py
index 7bee3350e..b0eaaecf9 100644
--- a/wger/exercises/signals.py
+++ b/wger/exercises/signals.py
@@ -20,6 +20,7 @@ import pathlib
# Django
from django.db.models.signals import (
post_delete,
+ pre_delete,
pre_save,
)
from django.dispatch import receiver
@@ -31,6 +32,9 @@ from easy_thumbnails.signals import saved_file
# wger
from wger.exercises.models import (
+ DeletionLog,
+ Exercise,
+ ExerciseBase,
ExerciseImage,
ExerciseVideo,
)
@@ -102,3 +106,38 @@ def delete_exercise_video_on_update(sender, instance: ExerciseVideo, **kwargs):
path = pathlib.Path(old_file.path)
if path.is_file():
path.unlink()
+
+
+@receiver(pre_delete, sender=ExerciseBase)
+def add_deletion_log_base(sender, instance: ExerciseBase, **kwargs):
+ log = DeletionLog(
+ model_type=DeletionLog.MODEL_BASE,
+ uuid=instance.uuid,
+ )
+ log.save()
+
+
+@receiver(pre_delete, sender=Exercise)
+def add_deletion_log_translation(sender, instance: Exercise, **kwargs):
+ log = DeletionLog(
+ model_type=DeletionLog.MODEL_TRANSLATION, uuid=instance.uuid, comment=instance.name
+ )
+ log.save()
+
+
+@receiver(pre_delete, sender=ExerciseImage)
+def add_deletion_log_image(sender, instance: ExerciseImage, **kwargs):
+ log = DeletionLog(
+ model_type=DeletionLog.MODEL_IMAGE,
+ uuid=instance.uuid,
+ )
+ log.save()
+
+
+@receiver(pre_delete, sender=ExerciseVideo)
+def add_deletion_log_video(sender, instance: ExerciseVideo, **kwargs):
+ log = DeletionLog(
+ model_type=DeletionLog.MODEL_VIDEO,
+ uuid=instance.uuid,
+ )
+ log.save()
diff --git a/wger/exercises/tests/test_deletion_log.py b/wger/exercises/tests/test_deletion_log.py
new file mode 100644
index 000000000..d5c0de7ce
--- /dev/null
+++ b/wger/exercises/tests/test_deletion_log.py
@@ -0,0 +1,60 @@
+# 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
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.exercises.models import (
+ DeletionLog,
+ Exercise,
+ ExerciseBase,
+)
+
+
+class DeletionLogTestCase(WgerTestCase):
+ """
+ Test that the deletion log entries are correctly generated
+ """
+
+ def test_base(self):
+ """
+ Test that an entry is generated when a base is deleted
+ """
+ self.assertEqual(DeletionLog.objects.all().count(), 0)
+
+ base = ExerciseBase.objects.get(pk=1)
+ base.delete()
+
+ # Base is deleted
+ count_base = DeletionLog.objects.filter(model_type=DeletionLog.MODEL_BASE,
+ uuid=base.uuid).count()
+ self.assertEqual(count_base, 1)
+
+ # All translations are also deleted
+ count = DeletionLog.objects.filter(model_type=DeletionLog.MODEL_TRANSLATION).count()
+ self.assertEqual(count, 2)
+
+ def test_translation(self):
+ """
+ Test that an entry is generated when a translation is deleted
+ """
+ self.assertEqual(DeletionLog.objects.all().count(), 0)
+
+ translation = Exercise.objects.get(pk=1)
+ translation.delete()
+
+ # Translation is deleted
+ count = DeletionLog.objects.filter(
+ model_type=DeletionLog.MODEL_TRANSLATION, uuid=translation.uuid
+ ).count()
+ self.assertEqual(count, 1)
diff --git a/wger/urls.py b/wger/urls.py
index f5a391c60..13572f6a9 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -121,7 +121,12 @@ router.register(
router.register(
r'equipment',
exercises_api_views.EquipmentViewSet,
- basename='api',
+ basename='equipment',
+)
+router.register(
+ r'deletion-log',
+ exercises_api_views.DeletionLogViewSet,
+ basename='deletion-log',
)
router.register(
r'exercisecategory',