summaryrefslogtreecommitdiff
path: root/app/notes/views.py
blob: 1b8ec1d7256d8d7049900a90b29c185392b5fc6d (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from django.views.generic import UpdateView, DetailView, ListView 
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse

from .models import Note
from .forms import NoteCreateForm, NoteEditForm


class NoteCreateView(LoginRequiredMixin, CreateView):
    model = Note
    form_class = NoteCreateForm

    def get_form_kwargs(self):
       kwargs = super(NoteCreateView, self).get_form_kwargs()
       kwargs.update({'user': self.request.user})
       return kwargs

    def get_success_url(self):
        if 'add_new' in self.request.POST:
            return reverse('notes:create')
        else:
            if self.object.post:
                return reverse('posts:detail', kwargs={"pk": self.object.post.pk})
            else:
                return reverse('notes:create')


class NoteUpdateView(LoginRequiredMixin, UpdateView):
    model = Note
    form_class = NoteEditForm
    
    def get_form_kwargs(self):
       kwargs = super(NoteUpdateView, self).get_form_kwargs()
       kwargs.update({'user': self.request.user})
       return kwargs

    def get_context_data(self, **kwargs):
        context = super(NoteUpdateView, self).get_context_data(**kwargs)
        context['is_update'] = True
        return context

    def get_success_url(self):
        return reverse('posts:detail', kwargs={"pk": self.object.post.pk})


class NoteListView(LoginRequiredMixin, ListView):
    model = Note

    def get_queryset(self):
        if self.kwargs['status']:
            status_reverse = dict((v, k) for k, v in Note.STATUS)
            status = status_reverse[self.kwargs['status'].title()]
            return Note.objects.filter(user=self.request.user, status=status)
        return Note.objects.filter(user=self.request.user)
    
    def get_context_data(self, **kwargs):
        context = super(NoteListView, self).get_context_data(**kwargs)
        context['note_statuses'] = Note.STATUS
        return context