Coverage for workouts/permissions.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Contains custom DRF permissions classes for the workouts app
2"""
3from rest_framework import permissions
4from workouts.models import Workout
7class IsOwner(permissions.BasePermission):
8 """Checks whether the requesting user is also the owner of the existing object"""
10 def has_object_permission(self, request, view, obj):
11 return obj.owner == request.user
14class IsOwnerOfWorkout(permissions.BasePermission):
15 """Checks whether the requesting user is also the owner of the new or existing object"""
17 def has_permission(self, request, view):
18 if request.method == "POST":
19 if request.data.get("workout"):
20 workout_id = request.data["workout"].split("/")[-2]
21 workout = Workout.objects.get(pk=workout_id)
22 if workout:
23 return workout.owner == request.user
24 return False
26 return True
28 def has_object_permission(self, request, view, obj):
29 return obj.workout.owner == request.user
32class IsCoachAndVisibleToCoach(permissions.BasePermission):
33 """Checks whether the requesting user is the existing object's owner's coach
34 and whether the object (workout) has a visibility of Public or Coach.
35 """
37 def has_object_permission(self, request, view, obj):
38 return obj.owner.coach == request.user
41class IsCoachOfWorkoutAndVisibleToCoach(permissions.BasePermission):
42 """Checks whether the requesting user is the existing workout's owner's coach
43 and whether the object has a visibility of Public or Coach.
44 """
46 def has_object_permission(self, request, view, obj):
47 return obj.workout.owner.coach == request.user
50class IsPublic(permissions.BasePermission):
51 """Checks whether the object (workout) has visibility of Public."""
53 def has_object_permission(self, request, view, obj):
54 return obj.visibility == "PU"
57class IsWorkoutPublic(permissions.BasePermission):
58 """Checks whether the object's workout has visibility of Public."""
60 def has_object_permission(self, request, view, obj):
61 return obj.workout.visibility == "PU"
64class IsReadOnly(permissions.BasePermission):
65 """Checks whether the HTTP request verb is only for retrieving data (GET, HEAD, OPTIONS)"""
67 def has_object_permission(self, request, view, obj):
68 return request.method in permissions.SAFE_METHODS