Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Don't allow changing the exercise base of a translation

Roland Geiderff71a76841be
diff --git a/wger/core/fixtures/test-user-data.json b/wger/core/fixtures/test-user-data.json
index 022388dd4..a7c4e2ed3 100644
--- a/wger/core/fixtures/test-user-data.json
+++ b/wger/core/fixtures/test-user-data.json
@@ -480,6 +480,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": true,
"show_comments": false,
"show_english_ingredients": false,
"user": 1,
@@ -511,6 +512,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": true,
"show_comments": false,
"show_english_ingredients": false,
"user": 2,
@@ -542,6 +544,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": true,
+ "email_verified": false,
"show_comments": false,
"show_english_ingredients": false,
"user": 3,
@@ -573,6 +576,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": true,
"show_comments": false,
"show_english_ingredients": false,
"user": 4,
@@ -604,6 +608,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": false,
"show_comments": false,
"show_english_ingredients": false,
"user": 5,
@@ -635,6 +640,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": false,
"show_comments": false,
"show_english_ingredients": false,
"user": 6,
@@ -666,6 +672,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": true,
"show_comments": false,
"show_english_ingredients": false,
"user": 7,
@@ -697,6 +704,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": true,
"show_comments": false,
"show_english_ingredients": false,
"user": 8,
@@ -728,6 +736,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": true,
"show_comments": false,
"show_english_ingredients": false,
"user": 9,
@@ -759,6 +768,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": true,
"show_comments": false,
"show_english_ingredients": false,
"user": 10,
@@ -790,6 +800,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": true,
"show_comments": false,
"show_english_ingredients": false,
"user": 11,
@@ -821,6 +832,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": true,
"show_comments": false,
"show_english_ingredients": false,
"user": 12,
@@ -852,6 +864,7 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
+ "email_verified": true,
"show_comments": false,
"show_english_ingredients": false,
"user": 13,
@@ -882,6 +895,7 @@
"pk": 14,
"model": "core.userprofile",
"fields": {
+ "email_verified": true,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
@@ -913,6 +927,7 @@
"pk": 15,
"model": "core.userprofile",
"fields": {
+ "email_verified": true,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
@@ -944,6 +959,7 @@
"pk": 16,
"model": "core.userprofile",
"fields": {
+ "email_verified": false,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
@@ -975,6 +991,7 @@
"pk": 17,
"model": "core.userprofile",
"fields": {
+ "email_verified": false,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
@@ -1006,6 +1023,7 @@
"pk": 18,
"model": "core.userprofile",
"fields": {
+ "email_verified": false,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
@@ -1037,6 +1055,7 @@
"pk": 19,
"model": "core.userprofile",
"fields": {
+ "email_verified": false,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
@@ -1068,6 +1087,7 @@
"pk": 20,
"model": "core.userprofile",
"fields": {
+ "email_verified": true,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
@@ -1099,6 +1119,7 @@
"pk": 21,
"model": "core.userprofile",
"fields": {
+ "email_verified": true,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
@@ -1130,6 +1151,7 @@
"pk": 22,
"model": "core.userprofile",
"fields": {
+ "email_verified": true,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
@@ -1161,6 +1183,7 @@
"pk": 23,
"model": "core.userprofile",
"fields": {
+ "email_verified": true,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
@@ -1192,6 +1215,7 @@
"pk": 24,
"model": "core.userprofile",
"fields": {
+ "email_verified": true,
"workout_reminder": 20,
"height": 180,
"show_english_ingredients": false,
diff --git a/wger/core/tests/api_base_test.py b/wger/core/tests/api_base_test.py
index 2a0a35759..5d0d2f12a 100644
--- a/wger/core/tests/api_base_test.py
+++ b/wger/core/tests/api_base_test.py
@@ -109,7 +109,7 @@ class ApiBaseTestCase(APITestCase):
self.client.force_authenticate(user=user_obj)
-class ApiGetTestCase(object):
+class ApiGetTestCase:
"""
Base test case for testing GET access to the API
"""
@@ -202,7 +202,7 @@ class ApiGetTestCase(object):
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
-class ApiPostTestCase(object):
+class ApiPostTestCase:
"""
Base test case for testing POST access to the API
"""
@@ -325,7 +325,7 @@ class ApiPostTestCase(object):
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
-class ApiPatchTestCase(object):
+class ApiPatchTestCase:
"""
Base test case for testing PATCH access to the API
"""
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index 95b7a5021..2025fe19a 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -242,6 +242,9 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):
"author_history",
)
+ def get_description(self, obj):
+ return "foo"
+
class ExerciseInfoSerializer(serializers.ModelSerializer):
"""
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index 79a079ee7..e2e9cb8ee 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -77,7 +77,7 @@ logger = logging.getLogger(__name__)
class ExerciseBaseViewSet(CreateUpdateModelViewSet):
"""
API endpoint for exercise base objects. For a read-only endpoint with all
- the information of an exercise, see /api/v2/exerciseinfo/
+ the information of an exercise, see /api/v2/exercisebaseinfo/
"""
queryset = ExerciseBase.objects.all()
serializer_class = ExerciseBaseSerializer
@@ -96,7 +96,9 @@ class ExerciseBaseViewSet(CreateUpdateModelViewSet):
"""
super().perform_create(serializer)
actstream_action.send(
- self.request.user, verb=StreamVerbs.CREATED.value, action_object=serializer.instance
+ self.request.user,
+ verb=StreamVerbs.CREATED.value,
+ action_object=serializer.instance,
)
def perform_update(self, serializer):
@@ -105,7 +107,9 @@ class ExerciseBaseViewSet(CreateUpdateModelViewSet):
"""
super().perform_create(serializer)
actstream_action.send(
- self.request.user, verb=StreamVerbs.UPDATED.value, action_object=serializer.instance
+ self.request.user,
+ verb=StreamVerbs.UPDATED.value,
+ action_object=serializer.instance,
)
@@ -140,6 +144,11 @@ class ExerciseTranslationViewSet(CreateUpdateModelViewSet):
"""
Save entry to activity stream
"""
+
+ # Don't allow to change the exercise base over the API
+ if serializer.validated_data.get('exercise_base'):
+ del serializer.validated_data['exercise_base']
+
super().perform_create(serializer)
actstream_action.send(
self.request.user,
diff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.json
index 9ac234333..3390da90c 100644
--- a/wger/exercises/fixtures/test-exercises.json
+++ b/wger/exercises/fixtures/test-exercises.json
@@ -175,7 +175,7 @@
"uuid": "9838235ce38f4ca6921e9d237d8e0813",
"language": 1,
"exercise_base": 1,
- "description": "",
+ "description": "Lorem ipsum dolor sit amet",
"name": "An exercise",
"creation_date": "2002-08-15",
"update_date": "2008-08-08T08:08:08+01:00"
diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.py
index 626d8f823..9f6d01f5e 100644
--- a/wger/exercises/tests/test_exercise.py
+++ b/wger/exercises/tests/test_exercise.py
@@ -26,7 +26,10 @@ from django.template import (
from django.urls import reverse
# wger
+from rest_framework import status
+
from wger.core.tests import api_base_test
+from wger.core.tests.api_base_test import ApiBaseTestCase
from wger.core.tests.base_testcase import (
STATUS_CODES_FAIL,
WgerDeleteTestCase,
@@ -558,3 +561,49 @@ class ExerciseBaseInfoApiTestCase(
def get_resource_name(self):
return 'exercisebaseinfo'
+
+
+class ExerciseCustomApiTestCase(api_base_test.BaseTestCase, ApiBaseTestCase):
+
+ pk = 1
+
+ def get_resource_name(self):
+ return 'exercise-translation'
+
+ def test_change_fields(self):
+ """
+ Test that it is possible to edit the fields of an exercise translation
+ """
+ exercise = Exercise.objects.get(pk=self.pk)
+ self.assertEqual(exercise.name, 'An exercise')
+ self.assertEqual(exercise.description, 'Lorem ipsum dolor sit amet')
+
+ data = {
+ 'name': 'A new name',
+ 'description': 'The wild boar is a suid native to much of Eurasia and North Africa'
+ }
+ self.get_credentials('test')
+ response = self.client.patch(self.url_detail, data=data)
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+ exercise = Exercise.objects.get(pk=self.pk)
+ self.assertEqual(exercise.name, 'A new name')
+ self.assertEqual(
+ exercise.description,
+ 'The wild boar is a suid native to much of Eurasia and North Africa'
+ )
+
+ def test_cant_change_base_id(self):
+ """
+ Test that it is not possible to change the base id of an existing
+ exercise resource.
+ """
+ exercise = Exercise.objects.get(pk=self.pk)
+ self.assertEqual(exercise.exercise_base_id, 1)
+
+ self.get_credentials('test')
+ response = self.client.patch(self.url_detail, data={'exercise_base': 2})
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+ exercise = Exercise.objects.get(pk=self.pk)
+ self.assertEqual(exercise.exercise_base_id, 1)