Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add UUID field to comments and aliases
Roland Geider60199ffdaced
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.pyindex f250a73c7..fe8d73f45 100644--- a/wger/exercises/api/serializers.py+++ b/wger/exercises/api/serializers.py@@ -184,6 +184,7 @@ class ExerciseCommentSerializer(serializers.ModelSerializer):model = ExerciseCommentfields = ['id',+ 'uuid','exercise','comment',]@@ -196,7 +197,12 @@ class ExerciseAliasSerializer(serializers.ModelSerializer):class Meta:model = Alias- fields = ['id', 'exercise', 'alias']+ fields = [+ 'id',+ 'uuid',+ 'exercise',+ 'alias',+ ]class ExerciseVariationSerializer(serializers.ModelSerializer):@@ -218,7 +224,11 @@ class ExerciseInfoAliasSerializer(serializers.ModelSerializer):class Meta:model = Alias- fields = ['id', 'alias']+ fields = [+ 'id',+ 'uuid',+ 'alias',+ ]class ExerciseCategorySerializer(serializers.ModelSerializer):diff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.jsonindex 5146c16e9..179cfd154 100644--- a/wger/exercises/fixtures/test-exercises.json+++ b/wger/exercises/fixtures/test-exercises.json@@ -286,6 +286,7 @@"pk": 1,"model": "exercises.exercisecomment","fields": {+ "uuid": "2275a647-4185-431c-87a8-2992c2d30cc6","comment": "test 123","exercise": 1}@@ -294,6 +295,7 @@"pk": 2,"model": "exercises.exercisecomment","fields": {+ "uuid": "a0271147-faca-4a08-af5e-0c02e8ae1310","comment": "Foobar","exercise": 2}@@ -302,6 +304,7 @@"pk": 1,"model": "exercises.alias","fields": {+ "uuid": "48694b73-6cb8-4c34-a602-f0566c1ccb7c","alias": "a different name","exercise": 1}@@ -310,6 +313,7 @@"pk": 2,"model": "exercises.alias","fields": {+ "uuid": "cdee2830-a0b6-4a23-ab8c-46c53e0c9722","alias": "yet another name","exercise": 2}diff --git a/wger/exercises/migrations/0028_add_uuid_alias_and_comments.py b/wger/exercises/migrations/0028_add_uuid_alias_and_comments.pynew file mode 100644index 000000000..200df04ac--- /dev/null+++ b/wger/exercises/migrations/0028_add_uuid_alias_and_comments.py@@ -0,0 +1,81 @@+# Generated by Django 4.2.6 on 2024-01-06 14:01++from django.db import migrations, models+import uuid+++def generate_aliases_uuids(apps, schema_editor):+ """Generate new UUIDs for each alias"""++ Alias = apps.get_model("exercises", "Alias")+ for alias in Alias.objects.all():+ alias.uuid = uuid.uuid4()+ alias.save(update_fields=["uuid"])+++def generate_comments_uuids(apps, schema_editor):+ """Generate new UUIDs for each comment"""++ Comment = apps.get_model("exercises", "ExerciseComment")+ for comment in Comment.objects.all():+ comment.uuid = uuid.uuid4()+ comment.save(update_fields=["uuid"])+++class Migration(migrations.Migration):+ dependencies = [+ ("exercises", "0027_alter_deletionlog_replaced_by_and_more"),+ ]++ operations = [+ migrations.AddField(+ model_name="exercisecomment",+ name="uuid",+ field=models.UUIDField(+ default=uuid.uuid4, editable=False, unique=False, verbose_name="UUID",+ ),+ ),+ migrations.AddField(+ model_name="historicalexercisecomment",+ name="uuid",+ field=models.UUIDField(+ db_index=True, default=uuid.uuid4, editable=False, verbose_name="UUID",+ ),+ ),+ migrations.AddField(+ model_name="alias",+ name="uuid",+ field=models.UUIDField(+ default=uuid.uuid4, editable=False, unique=False, verbose_name="UUID",+ ),+ ),+ migrations.AddField(+ model_name="historicalalias",+ name="uuid",+ field=models.UUIDField(+ db_index=True, default=uuid.uuid4, editable=False, verbose_name="UUID",+ ),+ ),+ migrations.RunPython(+ generate_aliases_uuids,+ reverse_code=migrations.RunPython.noop,+ ),+ migrations.RunPython(+ generate_comments_uuids,+ reverse_code=migrations.RunPython.noop,+ ),+ migrations.AlterField(+ model_name="exercisecomment",+ name="uuid",+ field=models.UUIDField(+ default=uuid.uuid4, editable=False, unique=True, verbose_name="UUID",+ ),+ ),+ migrations.AlterField(+ model_name="alias",+ name="uuid",+ field=models.UUIDField(+ default=uuid.uuid4, editable=False, unique=True, verbose_name="UUID",+ ),+ )+ ]diff --git a/wger/exercises/models/comment.py b/wger/exercises/models/comment.pyindex 65c3dd1c8..b3749e309 100644--- a/wger/exercises/models/comment.py+++ b/wger/exercises/models/comment.py@@ -14,6 +14,9 @@# 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+# Djangofrom django.db import modelsfrom django.utils.translation import gettext_lazy as _@@ -32,11 +35,20 @@ class ExerciseComment(models.Model):"""Model for an exercise comment"""+ uuid = models.UUIDField(+ default=uuid.uuid4,+ editable=False,+ unique=True,+ verbose_name='UUID',+ )+ """Globally unique ID, to identify the comment across installations"""+exercise = models.ForeignKey(Exercise,verbose_name=_('Exercise'),on_delete=models.CASCADE,)+comment = models.CharField(max_length=200,verbose_name=_('Comment'),diff --git a/wger/exercises/models/exercise_alias.py b/wger/exercises/models/exercise_alias.pyindex b2fdad9a8..0ccff1940 100644--- a/wger/exercises/models/exercise_alias.py+++ b/wger/exercises/models/exercise_alias.py@@ -14,6 +14,9 @@# 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+# Djangofrom django.db import modelsfrom django.utils.translation import gettext_lazy as _@@ -32,6 +35,14 @@ class Alias(models.Model):"""Model for an exercise (name)alias"""+ uuid = models.UUIDField(+ default=uuid.uuid4,+ editable=False,+ unique=True,+ verbose_name='UUID',+ )+ """Globally unique ID, to identify the alias across installations"""+exercise = models.ForeignKey(Exercise,verbose_name=_('Exercise'),diff --git a/wger/exercises/sync.py b/wger/exercises/sync.pyindex d501b511b..15372cc15 100644--- a/wger/exercises/sync.py+++ b/wger/exercises/sync.py@@ -121,17 +121,36 @@ def sync_exercises(f"{translation.language.short_name} {trans_uuid} - {name}"print_fn(out)+ # TODO: currently (2024-01-06) we always delete all the comments and the aliases+ # when synchronizing the data, even though we could identify them via the+ # UUID. However, the UUID created when running the database migrations will+ # be unique as well, so we will never update. We need to wait a while till+ # most local instances have run the sync script so that the UUID is the same+ # locally as well.+ #+ # -> remove the `.delete()` after the 2024-06-01+ExerciseComment.objects.filter(exercise=translation).delete()for note in translation_data['notes']:- ExerciseComment.objects.get_or_create(- exercise=translation,- comment=note['comment'],+ ExerciseComment.objects.update_or_create(+ uuid=note['uuid'],+ defaults={+ 'uuid': note['uuid'],+ 'exercise': translation,+ 'comment': note['comment'],+ })Alias.objects.filter(exercise=translation).delete()for alias in translation_data['aliases']:- print(alias)- Alias.objects.get_or_create(exercise=translation, alias=alias['alias'])+ Alias.objects.update_or_create(+ uuid=alias['uuid'],+ defaults={+ 'uuid': alias['uuid'],+ 'exercise': translation,+ 'alias': alias['alias'],+ }+ )print_fn('')diff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.pyindex f950470d6..f81707aec 100644--- a/wger/exercises/tests/test_sync.py+++ b/wger/exercises/tests/test_sync.py@@ -398,12 +398,14 @@ class MockExerciseResponse:"aliases": [{"id": 1,+ "uuid": "9a9ab323-5f47-431f-9289-cc21ad1de171","alias": "Kettlebell mit zwei Händen"}],"notes": [{"id": 1,+ "uuid": "f46e1610-b729-4948-b80a-c5ae52672c6a","exercise": 100,"comment": "Wichtig die Übung richtig zu machen"},@@ -503,16 +505,19 @@ class MockExerciseResponse:"aliases": [{"id": 2,+ "uuid": "65250c7c-d02e-4f3d-be01-beb39ff5ef0f","alias": "A new alias here"},{"id": 500,+ "uuid": "6544ce61-69a8-413e-9662-189d3aadd1b9","alias": "yet another name"},],"notes": [{"id": 147,+ "uuid": "53906cd1-61f1-4d56-ac60-e4fcc5824861","exercise": 123,"comment": "Foobar"},