Skip to content

Commit

Permalink
Add UUID field to comments and aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandgeider committed Jan 6, 2024
1 parent 7701367 commit 60199ff
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 7 deletions.
14 changes: 12 additions & 2 deletions wger/exercises/api/serializers.py
Expand Up @@ -184,6 +184,7 @@ class Meta:
model = ExerciseComment
fields = [
'id',
'uuid',
'exercise',
'comment',
]
Expand All @@ -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):
Expand All @@ -218,7 +224,11 @@ class ExerciseInfoAliasSerializer(serializers.ModelSerializer):

class Meta:
model = Alias
fields = ['id', 'alias']
fields = [
'id',
'uuid',
'alias',
]


class ExerciseCategorySerializer(serializers.ModelSerializer):
Expand Down
4 changes: 4 additions & 0 deletions wger/exercises/fixtures/test-exercises.json
Expand Up @@ -286,6 +286,7 @@
"pk": 1,
"model": "exercises.exercisecomment",
"fields": {
"uuid": "2275a647-4185-431c-87a8-2992c2d30cc6",
"comment": "test 123",
"exercise": 1
}
Expand All @@ -294,6 +295,7 @@
"pk": 2,
"model": "exercises.exercisecomment",
"fields": {
"uuid": "a0271147-faca-4a08-af5e-0c02e8ae1310",
"comment": "Foobar",
"exercise": 2
}
Expand All @@ -302,6 +304,7 @@
"pk": 1,
"model": "exercises.alias",
"fields": {
"uuid": "48694b73-6cb8-4c34-a602-f0566c1ccb7c",
"alias": "a different name",
"exercise": 1
}
Expand All @@ -310,6 +313,7 @@
"pk": 2,
"model": "exercises.alias",
"fields": {
"uuid": "cdee2830-a0b6-4a23-ab8c-46c53e0c9722",
"alias": "yet another name",
"exercise": 2
}
Expand Down
81 changes: 81 additions & 0 deletions 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",
),
)
]
12 changes: 12 additions & 0 deletions wger/exercises/models/comment.py
Expand Up @@ -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

# Django
from django.db import models
from django.utils.translation import gettext_lazy as _
Expand All @@ -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'),
Expand Down
11 changes: 11 additions & 0 deletions wger/exercises/models/exercise_alias.py
Expand Up @@ -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

# Django
from django.db import models
from django.utils.translation import gettext_lazy as _
Expand All @@ -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'),
Expand Down
29 changes: 24 additions & 5 deletions wger/exercises/sync.py
Expand Up @@ -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('')

Expand Down
5 changes: 5 additions & 0 deletions wger/exercises/tests/test_sync.py
Expand Up @@ -398,12 +398,14 @@ def json():
"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"
},
Expand Down Expand Up @@ -503,16 +505,19 @@ def json():
"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"
},
Expand Down

0 comments on commit 60199ff

Please sign in to comment.