Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add custom permissions and viewsets for exercises and related methods

Roland Geider2f18485184fc
diff --git a/wger/core/api/viewsets.py b/wger/core/api/viewsets.py
new file mode 100644
index 000000000..36d4259fb
--- /dev/null
+++ b/wger/core/api/viewsets.py
@@ -0,0 +1,29 @@
+# This file is part of wger Workout Manager <https://github.com/wger-project>.
+# Copyright (C) 2013 - 2021 wger Team
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# 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/>.
+
+from rest_framework import mixins
+from rest_framework.viewsets import GenericViewSet
+
+
+class CreateUpdateModelViewSet(mixins.CreateModelMixin,
+ mixins.RetrieveModelMixin,
+ mixins.UpdateModelMixin,
+ mixins.ListModelMixin,
+ GenericViewSet):
+ """
+ This is basically a copy of ModelViewSet, but without support for DELETE
+ """
+ pass
diff --git a/wger/core/models.py b/wger/core/models.py
index 1f81ce31d..a9b95bf35 100644
--- a/wger/core/models.py
+++ b/wger/core/models.py
@@ -231,16 +231,12 @@ by the US Department of Agriculture. It is extremely complete, with around
days_since_joined = datetime.date.today() - self.user.date_joined.date()
days_in_three_weeks = 21
- if days_since_joined.days > days_in_three_weeks:
- # perms will be updated only once, after they hit three week mark
- image_perm = Permission.objects.get(codename='add_exerciseimage')
- self.user.user_permissions.add(image_perm)
-
return days_since_joined.days > days_in_three_weeks
@property
def has_exercise_permission(self):
"""Returns true if user has all the exercise permissions and not just one."""
+ # TODO: check if this can be removed / refactored
if self.user.groups.filter(name='admin').exists() or self.user.groups.filter(
name='exercises_editor').exists():
return True
diff --git a/wger/exercises/api/permissions.py b/wger/exercises/api/permissions.py
new file mode 100644
index 000000000..9b570679c
--- /dev/null
+++ b/wger/exercises/api/permissions.py
@@ -0,0 +1,33 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
+
+# Third Party
+from rest_framework.permissions import BasePermission
+
+
+class CanEditExercises(BasePermission):
+ """
+ Checks that users are allowed to create or edit an exercise.
+
+ Regular users that are "trustworthy" (see Userprofile model for details) and
+ administrator users with the appropriate exercise permission are allowed.
+ """
+
+ def has_permission(self, request, view):
+ if request.user.is_anonymous:
+ return False
+
+ return request.user.userprofile.is_trustworthy or\
+ request.user.has_perm('exercises.add_exercise')
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index 185bd8cbf..5315d6e64 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -36,7 +36,9 @@ from rest_framework.permissions import (
from rest_framework.response import Response
# wger
+from wger.core.api.viewsets import CreateUpdateModelViewSet
from wger.config.models import LanguageConfig
+from wger.exercises.api.permissions import CanEditExercises
from wger.exercises.api.serializers import (
EquipmentSerializer,
ExerciseBaseSerializer,
@@ -79,14 +81,14 @@ class ExerciseBaseViewSet(viewsets.ReadOnlyModelViewSet):
'equipment')
-class ExerciseViewSet(viewsets.ModelViewSet):
+class ExerciseViewSet(CreateUpdateModelViewSet):
"""
API endpoint for exercise objects. For a read-only endpoint with all
the information of an exercise, see /api/v2/exerciseinfo/
"""
queryset = Exercise.objects.accepted()
serializer_class = ExerciseSerializer
- permission_classes = (AllowAny, ) # TODO: use trustworthiness attribute / permission
+ permission_classes = (CanEditExercises, )
ordering_fields = '__all__'
filterset_fields = ('uuid',
'creation_date',
@@ -278,13 +280,14 @@ class ExerciseCategoryViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = ('name',)
-class ExerciseImageViewSet(viewsets.ModelViewSet):
+class ExerciseImageViewSet(CreateUpdateModelViewSet):
"""
API endpoint for exercise image objects
"""
+
queryset = ExerciseImage.objects.all()
serializer_class = ExerciseImageSerializer
- permission_classes = (IsAuthenticatedOrReadOnly, CreateOnlyPermission)
+ permission_classes = (CanEditExercises, )
ordering_fields = '__all__'
filterset_fields = ('is_main',
'status',
@@ -322,11 +325,12 @@ class ExerciseImageViewSet(viewsets.ModelViewSet):
obj.save()
-class ExerciseCommentViewSet(viewsets.ReadOnlyModelViewSet):
+class ExerciseCommentViewSet(CreateUpdateModelViewSet):
"""
API endpoint for exercise comment objects
"""
serializer_class = ExerciseCommentSerializer
+ permission_classes = (CanEditExercises,)
ordering_fields = '__all__'
filterset_fields = ('comment',
'exercise')
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index 91538bba0..455ecc57a 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -578,7 +578,6 @@ class ExerciseComment(models.Model):
"""
exercise = models.ForeignKey(Exercise,
verbose_name=_('Exercise'),
- editable=False,
on_delete=models.CASCADE)
comment = models.CharField(max_length=200,
verbose_name=_('Comment'),
@@ -593,7 +592,7 @@ class ExerciseComment(models.Model):
"""
return self.comment
- def save(self, *args, **kwargs):
+ def save22(self, *args, **kwargs):
"""
Reset cached workouts
"""
diff --git a/wger/exercises/templates/exercise/view.html b/wger/exercises/templates/exercise/view.html
index d1ea6ee2f..338dd6c8a 100644
--- a/wger/exercises/templates/exercise/view.html
+++ b/wger/exercises/templates/exercise/view.html
@@ -92,7 +92,7 @@ $(document).ready(function() {
{# Images #}
{% with images=exercise.images.accepted %}
-{% if images or perms.exercises.change_exerciseimage or user.userprofile.is_trustworthy %}
+{% if images %}
<h5 class="mt-4">{% translate "Images" %}:</h5>
<div class="row row-cols-2 row-cols-md-3">
{% with other_images=images %}