Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add replaced_by to the deletion log
Roland Geider4fb9425e9d99
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.pyindex 50bf816c2..57cb3456c 100644--- a/wger/exercises/api/serializers.py+++ b/wger/exercises/api/serializers.py@@ -78,6 +78,7 @@ class DeletionLogSerializer(serializers.ModelSerializer):fields = ['model_type','uuid',+ 'replaced_by','timestamp','comment',]diff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.jsonindex 96fcd69f1..798edc8ab 100644--- a/wger/exercises/fixtures/test-exercises.json+++ b/wger/exercises/fixtures/test-exercises.json@@ -166,7 +166,7 @@"pk": 3,"model": "exercises.exercise","fields": {- "uuid": "946afe7b54a644a69c36c3e31e6b4c3b",+ "uuid": "946afe7b-54a6-44a6-9c36-c3e31e6b4c3b","language": 2,"exercise_base": 3,"description": "",diff --git a/wger/exercises/management/commands/sync-exercises.py b/wger/exercises/management/commands/sync-exercises.pyindex b72023ef8..6d87c314e 100644--- a/wger/exercises/management/commands/sync-exercises.py+++ b/wger/exercises/management/commands/sync-exercises.py@@ -23,7 +23,7 @@ from django.core.validators import URLValidator# wgerfrom wger.exercises.sync import (- delete_entries,+ handle_deleted_entries,sync_categories,sync_equipment,sync_exercises,@@ -88,4 +88,4 @@ class Command(BaseCommand):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)+ handle_deleted_entries(self.stdout.write, self.remote_url, self.style.SUCCESS)diff --git a/wger/exercises/migrations/0026_deletionlog_replaced_by.py b/wger/exercises/migrations/0026_deletionlog_replaced_by.pynew file mode 100644index 000000000..a31b23c54--- /dev/null+++ b/wger/exercises/migrations/0026_deletionlog_replaced_by.py@@ -0,0 +1,23 @@+# Generated by Django 4.1.9 on 2023-10-19 11:09++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ('exercises', '0025_rename_update_date_exercise_last_update_and_more'),+ ]++ operations = [+ migrations.AddField(+ model_name='deletionlog',+ name='replaced_by',+ field=models.UUIDField(+ default=None,+ editable=False,+ help_text='UUID of the object ',+ null=True,+ verbose_name='Replaced by'+ ),+ ),+ ]diff --git a/wger/exercises/models/deletion_log.py b/wger/exercises/models/deletion_log.pyindex b97f89717..8a2dc9f09 100644--- a/wger/exercises/models/deletion_log.py+++ b/wger/exercises/models/deletion_log.py@@ -54,6 +54,16 @@ class DeletionLog(models.Model):verbose_name='UUID',)+ replaced_by = models.UUIDField(+ default=None,+ unique=False,+ editable=False,+ null=True,+ verbose_name='Replaced by',+ help_text='UUID of the object replaced by the deleted one. At the moment only available '+ 'for exercise bases',+ )+timestamp = models.DateTimeField(auto_now=True)comment = models.CharField(max_length=200, default='')diff --git a/wger/exercises/sync.py b/wger/exercises/sync.pyindex b19bb597f..7838294a6 100644--- a/wger/exercises/sync.py+++ b/wger/exercises/sync.py@@ -53,6 +53,10 @@ from wger.exercises.models import (ExerciseVideo,Muscle,)+from wger.manager.models import (+ Setting,+ WorkoutLog,+)from wger.utils.requests import (get_paginated,wger_headers,@@ -75,6 +79,7 @@ def sync_exercises(for data in result:uuid = data['uuid']+ created = data['created']license_id = data['license']['id']category_id = data['category']['id']license_author = data['license_author']@@ -84,7 +89,10 @@ def sync_exercises(base, base_created = ExerciseBase.objects.update_or_create(uuid=uuid,- defaults={'category_id': category_id},+ defaults={+ 'category_id': category_id,+ 'created': created+ },)print_fn(f"{'created' if base_created else 'updated'} exercise {uuid}")@@ -272,13 +280,17 @@ def sync_equipment(print_fn(style_fn('done!\n'))-def delete_entries(- print_fn,+def handle_deleted_entries(+ print_fn=None,remote_url=settings.WGER_SETTINGS['WGER_INSTANCE'],style_fn=lambda x: x,):+ if not print_fn:+ def print_fn(_):+ return None+"""Delete exercises that were removed on the server"""- print_fn('*** Deleting exercises data that was removed on the server...')+ print_fn('*** Deleting exercise data that was removed on the server...')headers = wger_headers()url = make_uri(DELETION_LOG_ENDPOINT, server_url=remote_url, query={'limit': 100})@@ -286,13 +298,38 @@ def delete_entries(for data in result:uuid = data['uuid']+ replaced_by_uuid = data['replaced_by']model_type = data['model_type']if model_type == DeletionLog.MODEL_BASE:+ obj_replaced = None+ nr_settings = None+ nr_logs = None+ try:+ obj_replaced = ExerciseBase.objects.get(uuid=replaced_by_uuid)+ except ExerciseBase.DoesNotExist:+ pass+try:obj = ExerciseBase.objects.get(uuid=uuid)++ # Replace exercise in workouts and logs+ if obj_replaced:+ nr_settings = (+ Setting.objects.filter(exercise_base=obj+ ).update(exercise_base=obj_replaced)+ )+ nr_logs = (+ WorkoutLog.objects.filter(exercise_base=obj+ ).update(exercise_base=obj_replaced)+ )+obj.delete()print_fn(f'Deleted exercise base {uuid}')+ if nr_settings:+ print_fn(f'- replaced {nr_settings} time(s) in workouts by {replaced_by_uuid}')+ if nr_logs:+ print_fn(f'- replaced {nr_logs} time(s) in workout logs by {replaced_by_uuid}')except ExerciseBase.DoesNotExist:passdiff --git a/wger/exercises/tasks.py b/wger/exercises/tasks.pyindex 757bacb55..3a46af9cc 100644--- a/wger/exercises/tasks.py+++ b/wger/exercises/tasks.py@@ -25,9 +25,9 @@ from celery.schedules import crontab# wgerfrom wger.celery_configuration import appfrom wger.exercises.sync import (- delete_entries,download_exercise_images,download_exercise_videos,+ handle_deleted_entries,sync_categories,sync_equipment,sync_exercises,@@ -51,7 +51,7 @@ def sync_exercises_task():sync_muscles(logger.info)sync_equipment(logger.info)sync_exercises(logger.info)- delete_entries(logger.info)+ handle_deleted_entries(logger.info)@app.taskdiff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.pyindex 2f991f3cc..4591c1d7f 100644--- a/wger/exercises/tests/test_sync.py+++ b/wger/exercises/tests/test_sync.py@@ -29,7 +29,7 @@ from wger.exercises.models import (Muscle,)from wger.exercises.sync import (- delete_entries,+ handle_deleted_entries,sync_categories,sync_equipment,sync_exercises,@@ -37,6 +37,10 @@ from wger.exercises.sync import (sync_licenses,sync_muscles,)+from wger.manager.models import (+ Setting,+ WorkoutLog,+)from wger.utils.requests import wger_headers@@ -272,43 +276,50 @@ class MockDeletionLogResponse:"results": [{"model_type": "base",- "uuid": "acad394936fb44819a72be2ddae2bc05",+ "uuid": "acad3949-36fb-4481-9a72-be2ddae2bc05",+ "replaced_by": "ae3328ba-9a35-4731-bc23-5da50720c5aa","timestamp": "2023-01-30T19:32:56.761426+01:00","comment": "Exercise base with ID 1"},{"model_type": "base","uuid": "577ee012-70c6-4517-b0fe-dcf340926ae7",+ "replaced_by": None,"timestamp": "2023-01-30T19:32:56.761426+01:00","comment": "Unknown Exercise base"},{"model_type": "translation","uuid": "0ef4cec8-d9c9-464f-baf9-3dbdf20083cb",+ "replaced_by": None,"timestamp": "2023-01-30T19:32:56.764582+01:00","comment": "Translation that is not in this DB"},{"model_type": "translation",- "uuid": "946afe7b54a644a69c36c3e31e6b4c3b",+ "uuid": "946afe7b-54a6-44a6-9c36-c3e31e6b4c3b",+ "replaced_by": None,"timestamp": "2023-01-30T19:32:56.765350+01:00","comment": "Translation with ID 3"},{"model_type": "image","uuid": "c72b4463-48ae-4c7d-8093-2c347c38e05a",+ "replaced_by": None,"timestamp": "2023-01-30T19:32:56.765350+01:00","comment": "Unknown image"},{"model_type": "video","uuid": "e0d25624-348b-4a23-adcd-17190f96f005",+ "replaced_by": None,"timestamp": "2023-01-30T19:32:56.765350+01:00","comment": "Unknown video"},{"model_type": "foobar",- "uuid": "37b5813c76bd4658820a572d9dd93f31",+ "uuid": "37b5813c-76bd-4658-820a-572d9dd93f31",+ "replaced_by": None,"timestamp": "2023-01-30T19:32:56.765350+01:00","comment": "UUID of existing translation but other model type"},@@ -334,7 +345,9 @@ class MockExerciseResponse:{"id": 123,"uuid": "1b020b3a-3732-4c7e-92fd-a0cec90ed69b",- "creation_date": "2022-10-11",+ "created": "2022-10-11T19:45:01.914000+01:00",+ "last_update": "2023-02-05T19:45:01.914000+01:00",+ "last_update_global": "2023-02-05T19:45:01.914000+01:00","category": {"id": 2,"name": "Another category"@@ -380,7 +393,7 @@ class MockExerciseResponse:"name": "Zweihandiges Kettlebell","exercise_base": 123,"description": "Hier könnte Ihre Werbung stehen!",- "creation_date": "2015-08-03",+ "created": "2015-08-03","language": 1,"aliases": ["Kettlebell mit zwei Händen"@@ -406,7 +419,7 @@ class MockExerciseResponse:"name": "2 Handed Kettlebell Swing","exercise_base": 123,"description": "TBD",- "creation_date": "2023-08-03",+ "created": "2023-08-03","language": 2,"aliases": [],"notes": [],@@ -434,7 +447,9 @@ class MockExerciseResponse:{"id": 2,"uuid": "ae3328ba-9a35-4731-bc23-5da50720c5aa",- "creation_date": "2022-10-11",+ "created": "2022-10-11T13:11:01.779000+01:00",+ "last_update": "2023-02-05T19:45:01.779000+01:00",+ "last_update_global": "2023-08-15T23:33:11.779000+01:00","category": {"id": 3,"name": "Yet another category"@@ -480,7 +495,7 @@ class MockExerciseResponse:"name": "A new, better, updated name","exercise_base": 2,"description": "Two Handed Russian Style Kettlebell swing",- "creation_date": "2015-08-03",+ "created": "2015-08-03","language": 1,"aliases": ["A new alias here",@@ -505,7 +520,7 @@ class MockExerciseResponse:"name": "Balançoire Kettlebell à 2 mains","exercise_base": 2,"description": "Balançoire Kettlebell à deux mains de style russe",- "creation_date": "2015-08-03",+ "created": "2015-08-03","language": 3,"aliases": [],"notes": [],@@ -616,7 +631,16 @@ class TestSyncMethods(WgerTestCase):def test_deletion_log(self, mock_request):self.assertEqual(ExerciseBase.objects.count(), 8)self.assertEqual(Exercise.objects.count(), 11)- delete_entries(lambda x: x)++ base = ExerciseBase.objects.get(pk=1)+ base2 = ExerciseBase.objects.get(pk=3)+ self.assertFalse(Setting.objects.filter(exercise_base=base2).count())+ self.assertFalse(WorkoutLog.objects.filter(exercise_base=base2).count())++ settings = Setting.objects.filter(exercise_base=base)+ logs = WorkoutLog.objects.filter(exercise_base=base)++ handle_deleted_entries(print)mock_request.assert_called_with('https://wger.de/api/v2/deletion-log/?limit=100',@@ -627,6 +651,12 @@ class TestSyncMethods(WgerTestCase):self.assertRaises(Exercise.DoesNotExist, Exercise.objects.get, pk=3)self.assertRaises(ExerciseBase.DoesNotExist, ExerciseBase.objects.get, pk=1)+ # Workouts and logs have been moved+ for setting_pk in settings:+ self.assertEqual(Setting.objects.get(pk=setting_pk).exercise_base_id, 2)+ for setting_pk in logs:+ self.assertEqual(WorkoutLog.objects.get(pk=setting_pk).exercise_base_id, 2)+@patch('requests.get', return_value=MockExerciseResponse())def test_exercise_sync(self, mock_request):self.assertEqual(ExerciseBase.objects.count(), 8)