-
Notifications
You must be signed in to change notification settings - Fork 8
/
permissions.py
73 lines (49 loc) · 1.97 KB
/
permissions.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from rest_framework.permissions import BasePermission
class IsAnonymoused(BasePermission):
"""
Allows access only to not authenticated users.
"""
message = 'permission denied, at first you must logout'
def has_permission(self, request, view):
return bool(request.user.is_anonymous)
class IsOwnerOfTicket(BasePermission):
"""
Allow access only user that owner of ticket
"""
message = 'permission denied, you are not owner of this ticket'
def has_permission(self, request, view):
return request.user.is_authenticated and request.user
def has_object_permission(self, request, view, obj):
return bool(obj.owner == request.user)
class IsSeller(BasePermission):
"""
Allow access only user that is seller
"""
message = 'permission denied, you are not seller user'
def has_permission(self, request, view):
return request.user.is_authenticated and request.user
def has_object_permission(self, request, view, obj):
return bool(obj.is_seller)
class IsSellerAndHasStore(BasePermission):
"""
Allow access only user that is seller and have store
"""
message = "permission denied, you are not seller user or don't have store"
def has_permission(self, request, view):
return request.user.is_authenticated and request.user
def has_object_permission(self, request, view, obj):
has_store = True
try:
obj.store
except:
has_store = False
return bool(obj.is_seller and has_store)
class IsSellerOfProduct(BasePermission):
"""
Allow access only user that seller of product
"""
message = 'permission denied, you are not seller of this product'
def has_permission(self, request, view):
return request.user.is_authenticated and request.user
def has_object_permission(self, request, view, obj):
return bool(obj.seller.founder == request.user and request.user.is_seller)