Fundraising in PDF.
[wolnelektury.git] / src / catalogue / feeds.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from django.contrib.sites.models import Site
5 from django.contrib.syndication.views import Feed
6 from django.urls import reverse
7
8 from catalogue import models
9
10
11 def absolute_url(url):
12     return "http://%s%s" % (Site.objects.get_current().domain, url)
13
14
15 class AudiobookFeed(Feed):
16     description = "Audiobooki ostatnio dodane do serwisu Wolne Lektury."
17
18     mime_types = {
19         'mp3': 'audio/mpeg',
20         'ogg': 'audio/ogg',
21         'daisy': 'application/zip',
22     }
23
24     titles = {
25         'all': 'WolneLektury.pl - audiobooki we wszystkich formatach',
26         'mp3': 'WolneLektury.pl - audiobooki w formacie MP3',
27         'ogg': 'WolneLektury.pl - audiobooki w formacie Ogg Vorbis',
28         'daisy': 'WolneLektury.pl - audiobooki w formacie DAISY',
29     }
30
31     def get_object(self, request, type):
32         return {'type': type, 'all': 'all' in request.GET}
33
34     def title(self, args):
35         return self.titles[args['type']]
36
37     def link(self, args):
38         return reverse('audiobook_feed', args=(args['type'],))
39
40     def items(self, args):
41         objects = models.BookMedia.objects.order_by('-uploaded_at')
42         objects = objects.filter(book__findable=True)
43         if type == 'all':
44             objects = objects.filter(type__in=('mp3', 'ogg', 'daisy'))
45         else:
46             objects = objects.filter(type=args['type'])
47         if not args['all']:
48             objects = objects[:50]
49         return objects
50
51     def item_title(self, item):
52         return item.name
53
54     def item_categories(self, item):
55         return sorted(item.book.authors().values_list('name', flat=True))
56
57     def item_description(self, item):
58         lines = []
59         extra_info = item.get_extra_info_json()
60         artist = extra_info.get('artist_name', None)
61         if artist is not None:
62             lines.append('Czyta: %s' % artist)
63         director = extra_info.get('director_name', None)
64         if director is not None:
65             lines.append('Reżyseria: %s' % director)
66         return '<br/>\n'.join(lines)
67
68     def item_link(self, item):
69         return item.book.get_absolute_url()
70
71     def item_guid(self, item):
72         return absolute_url(item.file.url)
73
74     def item_enclosure_url(self, item):
75         return absolute_url(item.file.url)
76
77     def item_enclosure_length(self, item):
78         return item.file.size
79
80     def item_enclosure_mime_type(self, item):
81         return self.mime_types[item.type]
82
83     def item_pubdate(self, item):
84         return item.uploaded_at