Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Enforce translation uniqueness

Roland Geider12136823c507
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index b0a96bc2f..fe418d8e5 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -14,6 +14,9 @@
#
# You should have received a copy of the GNU Affero General Public License
+# Django
+from django.db.models import Q
+
# Third Party
from rest_framework import serializers
@@ -238,6 +241,34 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):
"language",
)
+ def validate(self, value):
+ """
+ Check that there is only one language per exercise
+ """
+ if value.get('language'):
+ # Editing an existing object
+ # -> Check if the language already exists, excluding the current object
+ if self.instance:
+ if self.instance.exercise_base.exercises.filter(
+ ~Q(id=self.instance.pk),
+ language=value['language']
+ ).exists():
+ raise serializers.ValidationError(
+ f"There is already a translation for this exercise in {value['language']}"
+ )
+ # Creating a new object
+ # -> Check if the language already exists
+ else:
+ if Exercise.objects.filter(
+ exercise_base=value['exercise_base'],
+ language=value['language']
+ ).exists():
+ raise serializers.ValidationError(
+ f"There is already a translation for this exercise in {value['language']}"
+ )
+
+ return super().validate(value)
+
class ExerciseInfoSerializer(serializers.ModelSerializer):
"""
diff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.json
index 297b0ae9e..65bc78802 100644
--- a/wger/exercises/fixtures/test-exercises.json
+++ b/wger/exercises/fixtures/test-exercises.json
@@ -1,295 +1,294 @@
[
-
- {
- "pk": 1,
- "model": "exercises.variation",
- "fields": {
- }
- },
- {
- "pk": 2,
- "model": "exercises.variation",
- "fields": {
- }
- },
- {
- "pk": 3,
- "model": "exercises.variation",
- "fields": {
- }
- },
- {
- "pk": 4,
- "model": "exercises.variation",
- "fields": {
- }
- },
- {
- "pk": 1,
- "model": "exercises.exercisebase",
- "fields": {
- "uuid": "acad394936fb44819a72be2ddae2bc05",
- "category": 2,
- "creation_date": "2020-01-10",
- "update_date": "2020-11-01T22:10:00+01:00",
- "muscles": [
- 1,
- 2
- ],
- "muscles_secondary": [
- 3
- ],
- "variations": 1
- }
- },
- {
- "pk": 2,
- "model": "exercises.exercisebase",
- "fields": {
- "uuid": "029a6eac67f242d9b71d9d692b0a35b0",
- "category": 2,
- "creation_date": "2014-11-01",
- "update_date": "2018-11-01T00:00:00+01:00",
- "muscles": [
- 2
- ],
- "muscles_secondary": [
- 1,
- 3
- ],
- "variations": 1
- }
- },
- {
- "pk": 3,
- "model": "exercises.exercisebase",
- "fields": {
- "uuid": "c2dc11e9b14a49cb9d11d1b9441f16f4",
- "category": 3,
- "creation_date": "2020-01-10",
- "update_date": "2020-11-01T22:10:00+01:00",
- "muscles": [
- 1
- ],
- "variations": 2
- }
- },
- {
- "pk": 4,
- "model": "exercises.exercisebase",
- "fields": {
- "uuid": "9838235ce38f4ca6921e9d237d8e0813",
- "category": 3,
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00",
- "muscles": [
- 1
- ],
- "muscles_secondary":[],
- "variations": 2
- }
- },
- {
- "pk": 5,
- "model": "exercises.exercisebase",
- "fields": {
- "uuid": "e9e35f0c45b14d64b00db707a94c6fb8",
- "category": 1,
- "creation_date": "2002-08-15",
- "update_date": "2008-08-08T08:08:08+01:00",
- "muscles": [],
- "variations": null
- }
- },
- {
- "pk": 1,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "9838235ce38f4ca6921e9d237d8e0813",
- "language": 1,
- "exercise_base": 1,
- "description": "Lorem ipsum dolor sit amet",
- "name": "An exercise",
- "creation_date": "2002-08-15",
- "update_date": "2008-08-08T08:08:08+01:00"
- }
- },
- {
- "pk": 2,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "3275c4e6e7ba4368964b6b1a9d80ca7a",
- "language": 2,
- "exercise_base": 2,
- "description": "",
- "name": "Very cool exercise",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 3,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "946afe7b54a644a69c36c3e31e6b4c3b",
- "language": 1,
- "exercise_base": 3,
- "description": "",
- "name": "Boring exercise",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 4,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "37b5813c76bd4658820a572d9dd93f31",
- "language": 1,
- "description": "",
- "exercise_base": 4,
- "license_author": "test",
- "name": "Pending exercise",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 5,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "37b5813c76bd4658820a572d9dd93f31",
- "language": 3,
- "description": "",
- "exercise_base": 1,
- "license_author": "test",
- "name": "Test exercise 123",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 6,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "37b5813c76bd4658820a572d9dd93f31",
- "language": 3,
- "description": "",
- "exercise_base": 3,
- "license_author": "test",
- "name": "Some other exercise",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 35,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "150f17eb8674450d976f4c5538479bfb",
- "language": 1,
- "exercise_base": 4,
- "description": "",
- "name": "Needed for demo user",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 81,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "885c1110aa324235af361164347a4465",
- "language": 1,
- "exercise_base": 5,
- "description": "",
- "name": "Needed for demo user",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 84,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "a7c662b36b474affb313a872a104dd2f",
- "language": 2,
- "exercise_base": 4,
- "description": "",
- "name": "Needed for demo user",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 91,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "f2417bb4fa314e51bd5c425af21be468",
- "language": 2,
- "exercise_base": 4,
- "description": "",
- "name": "Needed for demo user",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 111,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "cc3dbb66889f4deda2f0142c942c464f",
- "language": 2,
- "exercise_base": 4,
- "description": "",
- "name": "Needed for demo user",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 126,
- "model": "exercises.exercise",
- "fields": {
- "uuid": "283ad85ca8984a719abbc9cadd62b353",
- "language": 2,
- "exercise_base": 4,
- "description": "",
- "name": "Needed for demo user",
- "creation_date": "2022-01-10",
- "update_date": "2022-02-02T06:45:11+01:00"
- }
- },
- {
- "pk": 1,
- "model": "exercises.exercisecomment",
- "fields": {
- "comment": "test 123",
- "exercise": 1
- }
- },
- {
- "pk": 2,
- "model": "exercises.exercisecomment",
- "fields": {
- "comment": "Foobar",
- "exercise": 2
- }
- },
- {
- "pk": 1,
- "model": "exercises.alias",
- "fields": {
- "alias": "a different name",
- "exercise": 1
- }
- },
- {
- "pk": 2,
- "model": "exercises.alias",
- "fields": {
- "alias": "yet another name",
- "exercise": 2
- }
+ {
+ "pk": 1,
+ "model": "exercises.variation",
+ "fields": {
}
+ },
+ {
+ "pk": 2,
+ "model": "exercises.variation",
+ "fields": {
+ }
+ },
+ {
+ "pk": 3,
+ "model": "exercises.variation",
+ "fields": {
+ }
+ },
+ {
+ "pk": 4,
+ "model": "exercises.variation",
+ "fields": {
+ }
+ },
+ {
+ "pk": 1,
+ "model": "exercises.exercisebase",
+ "fields": {
+ "uuid": "acad394936fb44819a72be2ddae2bc05",
+ "category": 2,
+ "creation_date": "2020-01-10",
+ "update_date": "2020-11-01T22:10:00+01:00",
+ "muscles": [
+ 1,
+ 2
+ ],
+ "muscles_secondary": [
+ 3
+ ],
+ "variations": 1
+ }
+ },
+ {
+ "pk": 2,
+ "model": "exercises.exercisebase",
+ "fields": {
+ "uuid": "029a6eac67f242d9b71d9d692b0a35b0",
+ "category": 2,
+ "creation_date": "2014-11-01",
+ "update_date": "2018-11-01T00:00:00+01:00",
+ "muscles": [
+ 2
+ ],
+ "muscles_secondary": [
+ 1,
+ 3
+ ],
+ "variations": 1
+ }
+ },
+ {
+ "pk": 3,
+ "model": "exercises.exercisebase",
+ "fields": {
+ "uuid": "c2dc11e9b14a49cb9d11d1b9441f16f4",
+ "category": 3,
+ "creation_date": "2020-01-10",
+ "update_date": "2020-11-01T22:10:00+01:00",
+ "muscles": [
+ 1
+ ],
+ "variations": 2
+ }
+ },
+ {
+ "pk": 4,
+ "model": "exercises.exercisebase",
+ "fields": {
+ "uuid": "9838235ce38f4ca6921e9d237d8e0813",
+ "category": 3,
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00",
+ "muscles": [
+ 1
+ ],
+ "muscles_secondary": [],
+ "variations": 2
+ }
+ },
+ {
+ "pk": 5,
+ "model": "exercises.exercisebase",
+ "fields": {
+ "uuid": "e9e35f0c45b14d64b00db707a94c6fb8",
+ "category": 1,
+ "creation_date": "2002-08-15",
+ "update_date": "2008-08-08T08:08:08+01:00",
+ "muscles": [],
+ "variations": null
+ }
+ },
+ {
+ "pk": 1,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "9838235ce38f4ca6921e9d237d8e0813",
+ "language": 1,
+ "exercise_base": 1,
+ "description": "Lorem ipsum dolor sit amet",
+ "name": "An exercise",
+ "creation_date": "2002-08-15",
+ "update_date": "2008-08-08T08:08:08+01:00"
+ }
+ },
+ {
+ "pk": 2,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "3275c4e6e7ba4368964b6b1a9d80ca7a",
+ "language": 2,
+ "exercise_base": 2,
+ "description": "",
+ "name": "Very cool exercise",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 3,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "946afe7b54a644a69c36c3e31e6b4c3b",
+ "language": 1,
+ "exercise_base": 3,
+ "description": "",
+ "name": "Boring exercise",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 4,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "37b5813c76bd4658820a572d9dd93f31",
+ "language": 1,
+ "description": "",
+ "exercise_base": 4,
+ "license_author": "test",
+ "name": "Pending exercise",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 5,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "37b5813c76bd4658820a572d9dd93f31",
+ "language": 3,
+ "description": "",
+ "exercise_base": 1,
+ "license_author": "test",
+ "name": "Test exercise 123",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 6,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "37b5813c76bd4658820a572d9dd93f31",
+ "language": 3,
+ "description": "",
+ "exercise_base": 3,
+ "license_author": "test",
+ "name": "Some other exercise",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 35,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "150f17eb8674450d976f4c5538479bfb",
+ "language": 1,
+ "exercise_base": 4,
+ "description": "",
+ "name": "Needed for demo user",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 81,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "885c1110aa324235af361164347a4465",
+ "language": 1,
+ "exercise_base": 5,
+ "description": "",
+ "name": "Needed for demo user",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 84,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "a7c662b36b474affb313a872a104dd2f",
+ "language": 2,
+ "exercise_base": 4,
+ "description": "",
+ "name": "Needed for demo user",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 91,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "f2417bb4fa314e51bd5c425af21be468",
+ "language": 2,
+ "exercise_base": 4,
+ "description": "",
+ "name": "Needed for demo user",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 111,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "cc3dbb66889f4deda2f0142c942c464f",
+ "language": 2,
+ "exercise_base": 4,
+ "description": "",
+ "name": "Needed for demo user",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 126,
+ "model": "exercises.exercise",
+ "fields": {
+ "uuid": "283ad85ca8984a719abbc9cadd62b353",
+ "language": 2,
+ "exercise_base": 4,
+ "description": "",
+ "name": "Needed for demo user",
+ "creation_date": "2022-01-10",
+ "update_date": "2022-02-02T06:45:11+01:00"
+ }
+ },
+ {
+ "pk": 1,
+ "model": "exercises.exercisecomment",
+ "fields": {
+ "comment": "test 123",
+ "exercise": 1
+ }
+ },
+ {
+ "pk": 2,
+ "model": "exercises.exercisecomment",
+ "fields": {
+ "comment": "Foobar",
+ "exercise": 2
+ }
+ },
+ {
+ "pk": 1,
+ "model": "exercises.alias",
+ "fields": {
+ "alias": "a different name",
+ "exercise": 1
+ }
+ },
+ {
+ "pk": 2,
+ "model": "exercises.alias",
+ "fields": {
+ "alias": "yet another name",
+ "exercise": 2
+ }
+ }
]
diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise_translation.py
similarity index 94%
rename from wger/exercises/tests/test_exercise.py
rename to wger/exercises/tests/test_exercise_translation.py
index fc86a3e13..73d1b9fe2 100644
--- a/wger/exercises/tests/test_exercise.py
+++ b/wger/exercises/tests/test_exercise_translation.py
@@ -570,7 +570,7 @@ class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):
data = {
'name': 'A new name',
'description': 'The wild boar is a suid native to much of Eurasia and North Africa',
- 'language': 3,
+ 'language': 1,
'exercise_base': 2
}
@@ -647,3 +647,34 @@ class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):
exercise = Exercise.objects.get(pk=self.pk)
self.assertEqual(exercise.description, 'alert(); The wild boar is a suid native...')
+
+ def test_post_only_one_language_per_base(self):
+ """
+ Test that it's not possible to add a second translation for the same
+ base in the same language.
+ """
+ self.get_credentials('trainer1')
+ response = self.client.post(self.url, data=self.data)
+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+
+ response = self.client.post(self.url, data=self.data)
+ self.assertTrue(response.data['non_field_errors'])
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+
+ def test_set_existing_language(self):
+ """
+ Test that it is possible to set the language if it doesn't duplicate a translation
+ """
+ self.get_credentials('trainer1')
+ response = self.client.patch(self.url_detail, data={'language': 1, 'name': '123456'})
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertFalse(response.data.get('non_field_errors'))
+
+ def test_edit_only_one_language_per_base(self):
+ """
+ Test that it's not possible to edit a translation to a second language for the same base
+ """
+ self.get_credentials('trainer1')
+ response = self.client.patch(self.url_detail, data={'language': 3})
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertTrue(response.data['non_field_errors'])
diff --git a/wger/manager/api/serializers.py b/wger/manager/api/serializers.py
index a9faffa76..17b81cc7c 100644
--- a/wger/manager/api/serializers.py
+++ b/wger/manager/api/serializers.py
@@ -20,6 +20,7 @@ from rest_framework import serializers
# wger
from wger.core.api.serializers import DaysOfWeekSerializer
+from wger.core.models import DaysOfWeek
from wger.exercises.api.serializers import (
ExerciseBaseInfoSerializer,
ExerciseSerializer,
@@ -114,6 +115,8 @@ class DaySerializer(serializers.ModelSerializer):
"""
Workout day serializer
"""
+ training = serializers.PrimaryKeyRelatedField(queryset=Workout.objects.all())
+ day = serializers.PrimaryKeyRelatedField(queryset=DaysOfWeek.objects.all(), many=True)
class Meta:
model = Day
@@ -124,6 +127,7 @@ class SetSerializer(serializers.ModelSerializer):
"""
Workout setting serializer
"""
+ exerciseday = serializers.PrimaryKeyRelatedField(queryset=Day.objects.all())
class Meta:
model = Set
diff --git a/wger/utils/viewsets.py b/wger/utils/viewsets.py
index 81df19f15..2325080e7 100644
--- a/wger/utils/viewsets.py
+++ b/wger/utils/viewsets.py
@@ -38,7 +38,7 @@ class WgerOwnerObjectModelViewSet(viewsets.ModelViewSet):
if obj.get_owner_object().user != request.user:
raise exceptions.PermissionDenied('You are not allowed to do this')
else:
- return super(WgerOwnerObjectModelViewSet, self).create(request, *args, **kwargs)
+ return super().create(request, *args, **kwargs)
def update(self, request, *args, **kwargs):
"""
@@ -51,4 +51,4 @@ class WgerOwnerObjectModelViewSet(viewsets.ModelViewSet):
if obj.get_owner_object().user != request.user:
raise exceptions.PermissionDenied('You are not allowed to do this')
else:
- return super(WgerOwnerObjectModelViewSet, self).update(request, *args, **kwargs)
+ return super().update(request, *args, **kwargs)