from django.views.generic import CreateView, ListView, UpdateView, DeleteView from django.views.generic.detail import DetailView from django.utils.decorators import method_decorator from django.contrib.auth.decorators import login_required from rest_framework import viewsets from .serializers import NoteSerializer, FolderSerializer from .models import Note from .forms import NoteForm @method_decorator(login_required, name='dispatch') class LoggedInCreateViewWithUser(CreateView): def get_form_kwargs(self, **kwargs): kwargs = super().get_form_kwargs(**kwargs) kwargs.update({'user': self.request.user}) return kwargs class NoteListView(ListView): model = Note def get_queryset(self): return Note.objects.filter(created_by=self.request.user) class NoteCreateView(LoggedInCreateViewWithUser): model = Note form_class = NoteForm template_name = "notes/create.html" class NoteViewSet(viewsets.ModelViewSet): """ API endpoint that allows users to be viewed or edited. """ serializer_class = NoteSerializer def get_queryset(self): return Note.objects.filter(created_by=self.request.user).order_by('-date_created')