a03c93fe4beda075a88b04caa98d15bf30179d0b
[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 import urllib.parse
18
19 from django.conf import settings
20 from django.contrib.syndication.views import Feed
21 from django.core.exceptions import PermissionDenied
22 from django.http import HttpResponse, Http404
23 from django.utils.feedgenerator import Atom1Feed
24 from django.views import View
25 from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView, TemplateView
26
27 from .models import Note
28
29
30 class NoteView(DetailView):
31     model = Note
32
33     def get(self, request, *args, **kwargs):
34         note = self.get_object()
35         if kwargs.get('year'):
36             # check date does match
37             creation = self.get_object().creation_timestamp
38             if (creation.year, creation.month, creation.day) != (
39                 int(kwargs['year']),
40                 int(kwargs['month']),
41                 int(kwargs['day']),
42             ):
43                 raise Http404()
44         if not note.published and not request.user.is_staff:
45             raise PermissionDenied()
46         return super(NoteView, self).get(request, *args, **kwargs)
47
48
49 class NoteEditView(UpdateView):
50     model = Note
51     fields = ['title', 'slug', 'text', 'tags', 'published']
52
53
54 class NoteApiSaveView(View):
55     http_method_names = ['post']
56
57     def post(self, request, *args, **kwargs):
58         note = Note.objects.get(slug=kwargs['slug'])
59         note.text = request.POST['text']
60         note.save()
61         return HttpResponse('ok')
62
63
64 class NoteAddView(CreateView):
65     model = Note
66     fields = ['title', 'slug', 'text', 'tags', 'published']
67
68
69 class NoteDeleteView(DeleteView):
70     model = Note
71
72     def get_success_url(self):
73         return '/'
74
75
76 class HomeView(TemplateView):
77     template_name = 'phyll/home.html'
78
79     def get_context_data(self, **kwargs):
80         context = super(HomeView, self).get_context_data(**kwargs)
81         context['posts'] = Note.objects.filter(published=True).order_by('-creation_timestamp')[:5]
82         return context
83
84
85 class ArchivesView(ListView):
86     model = Note
87
88     def get_queryset(self):
89         qs = super(ArchivesView, self).get_queryset()
90         if not self.request.user.is_staff:
91             qs = qs.filter(published=True)
92         return qs
93
94
95 class ListOnTagView(ListView):
96     model = Note
97
98     def get_queryset(self):
99         qs = Note.objects.filter(tags__name__in=[self.kwargs['tag']])
100         if not self.request.user.is_staff:
101             qs = qs.filter(published=True)
102         return qs
103
104
105 class Atom1FeedWithBaseXml(Atom1Feed):
106     def root_attributes(self):
107         root_attributes = super(Atom1FeedWithBaseXml, self).root_attributes()
108         scheme, netloc, path, params, query, fragment = urllib.parse.urlparse(self.feed['feed_url'])
109         root_attributes['xml:base'] = urllib.parse.urlunparse((scheme, netloc, '/', params, query, fragment))
110         return root_attributes
111
112
113 class AtomFeed(Feed):
114     title = settings.SITE_TITLE
115     link = '/'
116     feed_type = Atom1FeedWithBaseXml
117     author_name = settings.SITE_AUTHOR
118
119     def get_object(self, request, *args, **kwargs):
120         self.sub = kwargs.get('sub', 'default')
121         return super(AtomFeed, self).get_object(request, *args, **kwargs)
122
123     def items(self):
124         qs = Note.objects.filter(published=True)
125         if self.sub == 'default':
126             pass
127         elif self.sub == 'gnome-en':
128             qs = qs.filter(tags__name__in=['gnome']).filter(tags__name__in=['lang-en'])
129         else:
130             qs = qs.filter(tags__name__in=[self.sub])
131         return qs.select_related()[:20]
132
133     def item_description(self, item):
134         return item.text
135
136     def item_guid(self, item):
137         for tag in item.tags.all():
138             if tag.name.startswith('old-post-id-'):
139                 return 'http://www.0d.be/posts/%s' % tag.name.split('-')[-1]
140         return 'https://www.0d.be' + item.get_absolute_url()
141
142     def item_title(self, item):
143         return item.title
144
145     def item_pubdate(self, item):
146         return item.creation_timestamp