]> git.0d.be Git - chloro.git/blob - chloro/phyll/views.py
views: add page to list notes by tag
[chloro.git] / chloro / phyll / views.py
1 # chloro - personal space
2 # Copyright (C) 2019  Frederic Peters
3 #
4 # This program is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 from django.http import Http404
18 from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView, TemplateView
19
20 from .models import Note
21
22
23 class NoteView(DetailView):
24     model = Note
25
26     def get(self, request, *args, **kwargs):
27         if kwargs.get('year'):
28             # check date does match
29             note = self.get_object()
30             creation = self.get_object().creation_timestamp
31             if (creation.year, creation.month, creation.day) != (
32                 int(kwargs['year']),
33                 int(kwargs['month']),
34                 int(kwargs['day']),
35             ):
36                 raise Http404()
37         return super(NoteView, self).get(request, *args, **kwargs)
38
39
40 class NoteEditView(UpdateView):
41     model = Note
42     fields = ['title', 'slug', 'text', 'tags']
43
44
45 class NoteAddView(CreateView):
46     model = Note
47     fields = ['title', 'slug', 'text', 'tags']
48
49
50 class NoteDeleteView(DeleteView):
51     model = Note
52
53     def get_success_url(self):
54         return '/'
55
56
57 class HomeView(TemplateView):
58     template_name = 'phyll/home.html'
59
60
61 class ListOnTagView(ListView):
62     model = Note
63
64     def get_queryset(self):
65         return Note.objects.filter(tags__name__in=[self.kwargs['tag']])