Self hosted FLOSS fitness/workout, nutrition and weight tracker
Also migrate the data stored in the json field for context data
Roland Geider4f7cfb62e8ad
diff --git a/wger/manager/models/slot_entry.py b/wger/manager/models/slot_entry.pyindex dd222368c..3eda33714 100644--- a/wger/manager/models/slot_entry.py+++ b/wger/manager/models/slot_entry.py@@ -306,7 +306,11 @@ class SlotEntry(models.Model):if config.replace:out = config.valueelse:- step = config.value if config.step == StepChoices.ABSOLUTE else out * config.value / 100+ step = (+ config.value+ if config.step == StepChoices.ABSOLUTE+ else out * config.value / 100+ )if config.operation == OperationChoices.PLUS:out += stepdiff --git a/wger/trophies/checkers/personal_record.py b/wger/trophies/checkers/personal_record.pyindex ea2b00aa1..83b264454 100644--- a/wger/trophies/checkers/personal_record.py+++ b/wger/trophies/checkers/personal_record.py@@ -129,9 +129,9 @@ class PersonalRecordChecker(BaseTrophyChecker):one_rm_estimate = Nonereturn {- 'log_id': log.id,+ 'log_id': str(log.id),'date': log.date.isoformat(),- 'session_id': session.id if session else None,+ 'session_id': str(session.id) if session else None,'exercise_id': exercise.id if exercise else None,'repetitions_unit_id': repetitions_unit.id if repetitions_unit else None,'repetitions': float(repetitions) if repetitions else None,diff --git a/wger/trophies/migrations/0003_migrate_context_data_uuids.py b/wger/trophies/migrations/0003_migrate_context_data_uuids.pynew file mode 100644index 000000000..b078d4940--- /dev/null+++ b/wger/trophies/migrations/0003_migrate_context_data_uuids.py@@ -0,0 +1,64 @@+# Generated manually for PowerSync UUID PK migration.++from django.db import migrations+from django.db.migrations.state import StateApps+++def migrate_context_data_uuids(apps: StateApps, schema_editor):+ """+ Migrate the int PKs of Log and Session objects to UUIDs (strings)+ """+ UserTrophy = apps.get_model('trophies', 'UserTrophy')+ WorkoutLog = apps.get_model('manager', 'WorkoutLog')+ WorkoutSession = apps.get_model('manager', 'WorkoutSession')++ log_uuid_cache = {}+ session_uuid_cache = {}++ def resolve(cache: dict, model, old_id: int):+ if old_id not in cache:+ cache[old_id] = model.objects.filter(id=old_id).values_list('uuid', flat=True).first()+ return cache[old_id]++ for user_trophy in UserTrophy.objects.exclude(context_data__isnull=True).iterator():+ context = user_trophy.context_data+ if not isinstance(context, dict):+ continue++ changed = False++ log_id = context.get('log_id')+ if isinstance(log_id, int):+ uuid = resolve(log_uuid_cache, WorkoutLog, log_id)+ if uuid is not None:+ context['log_id'] = str(uuid)+ changed = True++ session_id = context.get('session_id')+ if isinstance(session_id, int):+ uuid = resolve(session_uuid_cache, WorkoutSession, session_id)+ if uuid is not None:+ context['session_id'] = str(uuid)+ changed = True++ if changed:+ user_trophy.context_data = context+ user_trophy.save(update_fields=['context_data'])+++class Migration(migrations.Migration):+ dependencies = [+ ('trophies', '0002_load_initial_trophies'),+ ('manager', '0025_change_pk_to_uuid'),+ ]++ run_before = [+ ('manager', '0026_change_pk_to_uuid_swap'),+ ]++ operations = [+ migrations.RunPython(+ migrate_context_data_uuids,+ reverse_code=migrations.RunPython.noop,+ ),+ ]diff --git a/wger/trophies/signals.py b/wger/trophies/signals.pyindex ec0ea52e4..61429c346 100644--- a/wger/trophies/signals.py+++ b/wger/trophies/signals.py@@ -99,7 +99,7 @@ def workout_log_saved(sender, instance: WorkoutLog, created: bool, **kwargs):checker = CheckerRegistry.create_checker(instance.user, trophy)checker.params = {'log': instance}existing = UserTrophy.objects.filter(- user=instance.user, trophy=trophy, context_data__log_id=instance.id+ user=instance.user, trophy=trophy, context_data__log_id=str(instance.id)).exists()if not existing and checker and checker.check():context = checker.get_context_data()diff --git a/wger/trophies/tests/test_services.py b/wger/trophies/tests/test_services.pyindex 13d7e9814..1a5defb8e 100644--- a/wger/trophies/tests/test_services.py+++ b/wger/trophies/tests/test_services.py@@ -319,10 +319,16 @@ class TrophyServiceTestCase(WgerTestCase):)ut1 = TrophyService.award_trophy(- self.user, pr_trophy, progress=100.0, context_data={'log_id': 1}+ self.user,+ pr_trophy,+ progress=100.0,+ context_data={'log_id': '0194c3f0-0000-7000-8000-000000000001'},)ut2 = TrophyService.award_trophy(- self.user, pr_trophy, progress=100.0, context_data={'log_id': 2}+ self.user,+ pr_trophy,+ progress=100.0,+ context_data={'log_id': '0194c3f0-0000-7000-8000-000000000002'},)self.assertEqual(UserTrophy.objects.filter(user=self.user, trophy=pr_trophy).count(), 2)