aboutsummaryrefslogtreecommitdiff
path: root/apps/notes/views.py
blob: ddb72ed1f7028bfda71e42ba9710bf7a0a77525c (plain)
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
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')