]> git.0d.be Git - chloro.git/blob - chloro/phyll/views.py
b8598dc1180f2ead84dc9f40901924fd46ea6602
[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.core.exceptions import PermissionDenied
18 from django.http import Http404
19 from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView, TemplateView
20
21 from .models import Note
22
23
24 class NoteView(DetailView):
25     model = Note
26
27     def get(self, request, *args, **kwargs):
28         note = self.get_object()
29         if kwargs.get('year'):
30             # check date does match
31             creation = self.get_object().creation_timestamp
32             if (creation.year, creation.month, creation.day) != (
33                 int(kwargs['year']),
34                 int(kwargs['month']),
35                 int(kwargs['day']),
36             ):
37                 raise Http404()
38         if not note.published and not request.user.is_staff:
39             raise PermissionDenied()
40         return super(NoteView, self).get(request, *args, **kwargs)
41
42
43 class NoteEditView(UpdateView):
44     model = Note
45     fields = ['title', 'slug', 'text', 'tags', 'published']
46
47
48 class NoteAddView(CreateView):
49     model = Note
50     fields = ['title', 'slug', 'text', 'tags', 'published']
51
52
53 class NoteDeleteView(DeleteView):
54     model = Note
55
56     def get_success_url(self):
57         return '/'
58
59
60 class HomeView(TemplateView):
61     template_name = 'phyll/home.html'
62
63
64 class ListOnTagView(ListView):
65     model = Note
66
67     def get_queryset(self):
68         qs = Note.objects.filter(tags__name__in=[self.kwargs['tag']])
69         if not self.request.user.is_staff:
70             qs = qs.filter(published=True)
71         return qs