Hide keyboard shortcuts

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 

5 

6 

7class IsOwner(permissions.BasePermission): 

8 """Checks whether the requesting user is also the owner of the existing object""" 

9 

10 def has_object_permission(self, request, view, obj): 

11 return obj.owner == request.user 

12 

13 

14class IsOwnerOfWorkout(permissions.BasePermission): 

15 """Checks whether the requesting user is also the owner of the new or existing object""" 

16 

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 

25 

26 return True 

27 

28 def has_object_permission(self, request, view, obj): 

29 return obj.workout.owner == request.user 

30 

31 

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 """ 

36 

37 def has_object_permission(self, request, view, obj): 

38 return obj.owner.coach == request.user 

39 

40 

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 """ 

45 

46 def has_object_permission(self, request, view, obj): 

47 return obj.workout.owner.coach == request.user 

48 

49 

50class IsPublic(permissions.BasePermission): 

51 """Checks whether the object (workout) has visibility of Public.""" 

52 

53 def has_object_permission(self, request, view, obj): 

54 return obj.visibility == "PU" 

55 

56 

57class IsWorkoutPublic(permissions.BasePermission): 

58 """Checks whether the object's workout has visibility of Public.""" 

59 

60 def has_object_permission(self, request, view, obj): 

61 return obj.workout.visibility == "PU" 

62 

63 

64class IsReadOnly(permissions.BasePermission): 

65 """Checks whether the HTTP request verb is only for retrieving data (GET, HEAD, OPTIONS)""" 

66 

67 def has_object_permission(self, request, view, obj): 

68 return request.method in permissions.SAFE_METHODS