mirror of
https://github.com/lemeow125/Django-NotesApp.git
synced 2024-11-16 22:19:24 +08:00
Added permissions to views
This commit is contained in:
parent
5b296ac367
commit
7ad2654b00
5 changed files with 18 additions and 1 deletions
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"[python]": {
|
||||
"editor.defaultFormatter": "ms-python.autopep8"
|
||||
},
|
||||
"python.formatting.provider": "none"
|
||||
}
|
|
@ -47,6 +47,7 @@ INSTALLED_APPS = [
|
|||
'corsheaders',
|
||||
'djoser',
|
||||
'accounts',
|
||||
'permissions',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
|
|
@ -2,10 +2,11 @@ from rest_framework.permissions import IsAuthenticated
|
|||
from rest_framework import viewsets, generics
|
||||
from .serializers import NoteSerializer
|
||||
from .models import Note
|
||||
from permissions.permissions import IsOwner
|
||||
|
||||
|
||||
class NoteViewSet(viewsets.ModelViewSet):
|
||||
permission_classes = [IsAuthenticated]
|
||||
permission_classes = [IsOwner]
|
||||
serializer_class = NoteSerializer
|
||||
queryset = Note.objects.all()
|
||||
|
||||
|
|
0
project/permissions/__init__.py
Normal file
0
project/permissions/__init__.py
Normal file
9
project/permissions/permissions.py
Normal file
9
project/permissions/permissions.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
from rest_framework.permissions import BasePermission
|
||||
|
||||
class IsOwner(BasePermission):
|
||||
"""
|
||||
Custom permission to only allow the creator of an object to view and manipulate it.
|
||||
"""
|
||||
def has_object_permission(self, request, view, obj):
|
||||
# Only allow the creator of the object to view and manipulate it.
|
||||
return obj.creator == request.user
|
Loading…
Reference in a new issue