forked from Fantomas42/django-blog-zinnia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feeds.py
371 lines (278 loc) · 11.5 KB
/
feeds.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""Feeds for Zinnia"""
from urlparse import urljoin
from BeautifulSoup import BeautifulSoup
from django.contrib import comments
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404
from django.utils.feedgenerator import Atom1Feed
from django.utils.translation import ugettext as _
from django.contrib.syndication.views import Feed
from django.core.urlresolvers import NoReverseMatch
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.contenttypes.models import ContentType
from tagging.models import Tag
from tagging.models import TaggedItem
from zinnia.models import Entry
from zinnia.settings import COPYRIGHT
from zinnia.settings import PROTOCOL
from zinnia.settings import FEEDS_FORMAT
from zinnia.settings import FEEDS_MAX_ITEMS
from zinnia.managers import entries_published
from zinnia.views.categories import get_category_or_404
from zinnia.templatetags.zinnia_tags import get_gravatar
class ZinniaFeed(Feed):
"""Base Feed class for the Zinnia application,
enriched for a more convenient usage."""
feed_copyright = COPYRIGHT
def __init__(self):
self.site = Site.objects.get_current()
self.site_url = '%s://%s' % (PROTOCOL, self.site.domain)
if FEEDS_FORMAT == 'atom':
self.feed_type = Atom1Feed
self.subtitle = self.description
def title(self, obj=None):
"""Title of the feed prefixed with the site name """
return '%s - %s' % (self.site.name, self.get_title(obj))
def get_title(self, obj):
raise NotImplementedError
class EntryFeed(ZinniaFeed):
"""Base Entry Feed"""
title_template = 'feeds/entry_title.html'
description_template = 'feeds/entry_description.html'
def item_pubdate(self, item):
"""Publication date of an entry"""
return item.creation_date
def item_categories(self, item):
"""Entry's categories"""
return [category.title for category in item.categories.all()]
def item_author_name(self, item):
"""Returns the first author of an entry"""
if item.authors.count():
self.item_author = item.authors.all()[0]
return self.item_author.username
def item_author_email(self, item):
"""Returns the first author's email"""
return self.item_author.email
def item_author_link(self, item):
"""Returns the author's URL"""
try:
author_url = reverse('zinnia_author_detail',
args=[self.item_author.username])
return self.site_url + author_url
except NoReverseMatch:
return self.site_url
def item_enclosure_url(self, item):
"""Returns an image for enclosure"""
if item.image:
url = item.image.url
else:
img = BeautifulSoup(item.html_content).find('img')
url = img.get('src') if img else None
return urljoin(self.site_url, url) if url else None
def item_enclosure_length(self, item):
"""Hardcoded enclosure length"""
return '100000'
def item_enclosure_mime_type(self, item):
"""Hardcoded enclosure mimetype"""
return 'image/jpeg'
class LatestEntries(EntryFeed):
"""Feed for the latest entries"""
def link(self):
"""URL of latest entries"""
return reverse('zinnia_entry_archive_index')
def items(self):
"""Items are published entries"""
return Entry.published.all()[:FEEDS_MAX_ITEMS]
def get_title(self, obj):
"""Title of the feed"""
return _('Latest entries')
def description(self):
"""Description of the feed"""
return _('The latest entries for the site %s') % self.site.name
class CategoryEntries(EntryFeed):
"""Feed filtered by a category"""
def get_object(self, request, path):
"""Retrieve the category by his path"""
return get_category_or_404(path)
def items(self, obj):
"""Items are the published entries of the category"""
return obj.entries_published()[:FEEDS_MAX_ITEMS]
def link(self, obj):
"""URL of the category"""
return obj.get_absolute_url()
def get_title(self, obj):
"""Title of the feed"""
return _('Entries for the category %s') % obj.title
def description(self, obj):
"""Description of the feed"""
return _('The latest entries for the category %s') % obj.title
class AuthorEntries(EntryFeed):
"""Feed filtered by an author"""
def get_object(self, request, username):
"""Retrieve the author by his username"""
return get_object_or_404(User, username=username)
def items(self, obj):
"""Items are the published entries of the author"""
return entries_published(obj.entries)[:FEEDS_MAX_ITEMS]
def link(self, obj):
"""URL of the author"""
return reverse('zinnia_author_detail', args=[obj.username])
def get_title(self, obj):
"""Title of the feed"""
return _('Entries for author %s') % obj.username
def description(self, obj):
"""Description of the feed"""
return _('The latest entries by %s') % obj.username
class TagEntries(EntryFeed):
"""Feed filtered by a tag"""
def get_object(self, request, slug):
"""Retrieve the tag by his name"""
return get_object_or_404(Tag, name=slug)
def items(self, obj):
"""Items are the published entries of the tag"""
return TaggedItem.objects.get_by_model(
Entry.published.all(), obj)[:FEEDS_MAX_ITEMS]
def link(self, obj):
"""URL of the tag"""
return reverse('zinnia_tag_detail', args=[obj.name])
def get_title(self, obj):
"""Title of the feed"""
return _('Entries for the tag %s') % obj.name
def description(self, obj):
"""Description of the feed"""
return _('The latest entries for the tag %s') % obj.name
class SearchEntries(EntryFeed):
"""Feed filtered by a search pattern"""
def get_object(self, request):
"""The GET parameter 'pattern' is the object"""
pattern = request.GET.get('pattern', '')
if len(pattern) < 3:
raise ObjectDoesNotExist
return pattern
def items(self, obj):
"""Items are the published entries founds"""
return Entry.published.search(obj)[:FEEDS_MAX_ITEMS]
def link(self, obj):
"""URL of the search request"""
return '%s?pattern=%s' % (reverse('zinnia_entry_search'), obj)
def get_title(self, obj):
"""Title of the feed"""
return _("Results of the search for '%s'") % obj
def description(self, obj):
"""Description of the feed"""
return _("The entries containing the pattern '%s'") % obj
class DiscussionFeed(ZinniaFeed):
"""Base class for Discussion Feed"""
title_template = 'feeds/discussion_title.html'
description_template = 'feeds/discussion_description.html'
def item_pubdate(self, item):
"""Publication date of a discussion"""
return item.submit_date
def item_link(self, item):
"""URL of the discussion"""
return item.get_absolute_url()
def item_author_name(self, item):
"""Author of the discussion"""
return item.userinfo['name']
def item_author_email(self, item):
"""Author's email of the discussion"""
return item.userinfo['email']
def item_author_link(self, item):
"""Author's URL of the discussion"""
return item.userinfo['url']
class LatestDiscussions(DiscussionFeed):
"""Feed for the latest discussions"""
def items(self):
"""Items are the discussions on the entries"""
content_type = ContentType.objects.get_for_model(Entry)
return comments.get_model().objects.filter(
content_type=content_type, is_public=True).order_by(
'-submit_date')[:FEEDS_MAX_ITEMS]
def link(self):
"""URL of latest discussions"""
return reverse('zinnia_entry_archive_index')
def get_title(self, obj):
"""Title of the feed"""
return _('Latest discussions')
def description(self):
"""Description of the feed"""
return _('The latest discussions for the site %s') % self.site.name
class EntryDiscussions(DiscussionFeed):
"""Feed for discussions on an entry"""
def get_object(self, request, year, month, day, slug):
"""Retrieve the discussions by entry's slug"""
return get_object_or_404(Entry.published, slug=slug,
creation_date__year=year,
creation_date__month=month,
creation_date__day=day)
def items(self, obj):
"""Items are the discussions on the entry"""
return obj.discussions[:FEEDS_MAX_ITEMS]
def link(self, obj):
"""URL of the entry"""
return obj.get_absolute_url()
def get_title(self, obj):
"""Title of the feed"""
return _('Discussions on %s') % obj.title
def description(self, obj):
"""Description of the feed"""
return _('The latest discussions for the entry %s') % obj.title
class EntryComments(EntryDiscussions):
"""Feed for comments in an entry"""
title_template = 'feeds/comment_title.html'
description_template = 'feeds/comment_description.html'
def items(self, obj):
"""Items are the comments on the entry"""
return obj.comments[:FEEDS_MAX_ITEMS]
def item_link(self, item):
"""URL of the comment"""
return item.get_absolute_url('#comment_%(id)s')
def get_title(self, obj):
"""Title of the feed"""
return _('Comments on %s') % obj.title
def description(self, obj):
"""Description of the feed"""
return _('The latest comments for the entry %s') % obj.title
def item_enclosure_url(self, item):
"""Returns a gravatar image for enclosure"""
return get_gravatar(item.userinfo['email'])
def item_enclosure_length(self, item):
"""Hardcoded enclosure length"""
return '100000'
def item_enclosure_mime_type(self, item):
"""Hardcoded enclosure mimetype"""
return 'image/jpeg'
class EntryPingbacks(EntryDiscussions):
"""Feed for pingbacks in an entry"""
title_template = 'feeds/pingback_title.html'
description_template = 'feeds/pingback_description.html'
def items(self, obj):
"""Items are the pingbacks on the entry"""
return obj.pingbacks[:FEEDS_MAX_ITEMS]
def item_link(self, item):
"""URL of the pingback"""
return item.get_absolute_url('#pingback_%(id)s')
def get_title(self, obj):
"""Title of the feed"""
return _('Pingbacks on %s') % obj.title
def description(self, obj):
"""Description of the feed"""
return _('The latest pingbacks for the entry %s') % obj.title
class EntryTrackbacks(EntryDiscussions):
"""Feed for trackbacks in an entry"""
title_template = 'feeds/trackback_title.html'
description_template = 'feeds/trackback_description.html'
def items(self, obj):
"""Items are the trackbacks on the entry"""
return obj.trackbacks[:FEEDS_MAX_ITEMS]
def item_link(self, item):
"""URL of the trackback"""
return item.get_absolute_url('#trackback_%(id)s')
def get_title(self, obj):
"""Title of the feed"""
return _('Trackbacks on %s') % obj.title
def description(self, obj):
"""Description of the feed"""
return _('The latest trackbacks for the entry %s') % obj.title