-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpermissions.py
More file actions
56 lines (43 loc) · 1.83 KB
/
permissions.py
File metadata and controls
56 lines (43 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from rest_framework.exceptions import PermissionDenied
from rest_framework.permissions import BasePermission, SAFE_METHODS
from users.models import Expert
class IsAchievementOwnerOrReadOnly(BasePermission):
"""
Allows access to update only to himself.
"""
def has_object_permission(self, request, view, obj):
if request.method in SAFE_METHODS or (obj.user == request.user):
return True
return False
class IsExpert(BasePermission):
"""
Allows access if user is EXPERT
"""
def has_permission(self, request, view):
user = request.user
if not getattr(user, "is_authenticated", False):
raise PermissionDenied("Authentication credentials were not provided.")
program_id = view.kwargs.get("program_id")
if not user.user_type == 3:
raise PermissionDenied("User is not an expert")
if not Expert.objects.filter(programs__id=program_id, user=user).exists():
raise PermissionDenied("You don't have permission to rate this program")
return True
class IsExpertPost(BasePermission):
"""
Allows access if user is EXPERT
"""
def has_permission(self, request, view):
user = request.user
if not getattr(user, "is_authenticated", False):
raise PermissionDenied("Authentication credentials were not provided.")
if getattr(user, "user_type", None) != 3:
raise PermissionDenied("User is not an expert")
return True
class CustomIsAuthenticated(BasePermission):
def has_permission(self, request, view):
if (
hasattr(view, "authentication_off") and view.authentication_off
): # Проверка наличия и значения атрибута
return True
return bool(request.user and request.user.is_authenticated)