Django-NotesApp/project/notes/views.py

25 lines
774 B
Python
Raw Permalink Normal View History

2023-03-01 22:58:22 +08:00
from rest_framework.permissions import IsAuthenticated
2023-03-29 19:37:42 +08:00
from rest_framework import viewsets, generics
2023-02-24 00:13:44 +08:00
from .serializers import NoteSerializer
from .models import Note
2023-05-20 08:20:20 +08:00
from permissions.permissions import IsOwner
2023-02-24 00:13:44 +08:00
class NoteViewSet(viewsets.ModelViewSet):
2023-05-20 08:20:20 +08:00
permission_classes = [IsOwner]
2023-02-24 00:13:44 +08:00
serializer_class = NoteSerializer
2023-03-01 22:58:22 +08:00
queryset = Note.objects.all()
def get_queryset(self):
user = self.request.user
queryset = Note.objects.filter(owner=user).order_by('date_created')
return queryset
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
2023-03-29 19:37:42 +08:00
class PublicNoteViewSet(generics.ListAPIView):
serializer_class = NoteSerializer
queryset = Note.objects.filter(public=True)