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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
# Copyright (c) 2011, 2012 Free Software Foundation
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This project incorporates work covered by the following copyright and permission notice:
# Copyright (c) 2009, Julien Fache
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * Neither the name of the author nor the names of other
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
# Copyright (c) 2011, 2012 Free Software Foundation
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Feeds for Objectapp"""
from urlparse import urljoin
from BeautifulSoup import BeautifulSoup
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 tagging.models import Tag
from tagging.models import TaggedItem
from objectapp.models import Gbobject
from objectapp.settings import COPYRIGHT
from objectapp.settings import PROTOCOL
from objectapp.settings import FEEDS_FORMAT
from objectapp.settings import FEEDS_MAX_ITEMS
from objectapp.managers import gbobjects_published
from objectapp.views.objecttypes import get_Objecttype_or_404
from objectapp.templatetags.objectapp_tags import get_gravatar
class ObjectappFeed(Feed):
"""Base Feed for Objectapp"""
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
class GbobjectFeed(ObjectappFeed):
"""Base Gbobject Feed"""
title_template = 'feeds/gbobject_title.html'
description_template = 'feeds/gbobject_description.html'
def item_pubdate(self, item):
"""Publication date of an gbobject"""
return item.creation_date
def item_objecttypes(self, item):
"""Gbobject's objecttypes"""
return [Objecttype.title for Objecttype in item.objecttypes.all()]
def item_author_name(self, item):
"""Returns the first author of an gbobject"""
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('objectapp_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:
return item.image.url
img = BeautifulSoup(item.html_content).find('img')
if img:
return urljoin(self.site_url, img['src'])
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 LatestGbobjects(GbobjectFeed):
"""Feed for the latest gbobjects"""
def link(self):
"""URL of latest gbobjects"""
return reverse('objectapp_gbobject_archive_index')
def items(self):
"""Items are published gbobjects"""
return Gbobject.published.all()[:FEEDS_MAX_ITEMS]
def title(self):
"""Title of the feed"""
return '%s - %s' % (self.site.name, _('Latest gbobjects'))
def description(self):
"""Description of the feed"""
return _('The latest gbobjects for the site %s') % self.site.name
class ObjecttypeGbobjects(GbobjectFeed):
"""Feed filtered by a Objecttype"""
def get_object(self, request, path):
"""Retrieve the Objecttype by his path"""
return get_Objecttype_or_404(path)
def items(self, obj):
"""Items are the published gbobjects of the Objecttype"""
return obj.gbobjects_published()[:FEEDS_MAX_ITEMS]
def link(self, obj):
"""URL of the Objecttype"""
return obj.get_absolute_url()
def title(self, obj):
"""Title of the feed"""
return _('Gbobjects for the Objecttype %s') % obj.title
def description(self, obj):
"""Description of the feed"""
return _('The latest gbobjects for the Objecttype %s') % obj.title
class AuthorGbobjects(GbobjectFeed):
"""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 gbobjects of the author"""
return gbobjects_published(obj.gbobjects)[:FEEDS_MAX_ITEMS]
def link(self, obj):
"""URL of the author"""
return reverse('objectapp_author_detail', args=[obj.username])
def title(self, obj):
"""Title of the feed"""
return _('Gbobjects for author %s') % obj.username
def description(self, obj):
"""Description of the feed"""
return _('The latest gbobjects by %s') % obj.username
class TagGbobjects(GbobjectFeed):
"""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 gbobjects of the tag"""
return TaggedItem.objects.get_by_model(
Gbobject.published.all(), obj)[:FEEDS_MAX_ITEMS]
def link(self, obj):
"""URL of the tag"""
return reverse('objectapp_tag_detail', args=[obj.name])
def title(self, obj):
"""Title of the feed"""
return _('Gbobjects for the tag %s') % obj.name
def description(self, obj):
"""Description of the feed"""
return _('The latest gbobjects for the tag %s') % obj.name
class SearchGbobjects(GbobjectFeed):
"""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 gbobjects founds"""
return Gbobject.published.search(obj)[:FEEDS_MAX_ITEMS]
def link(self, obj):
"""URL of the search request"""
return '%s?pattern=%s' % (reverse('objectapp_gbobject_search'), obj)
def 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 gbobjects containing the pattern '%s'") % obj
class GbobjectDiscussions(ObjectappFeed):
"""Feed for discussions in an gbobject"""
title_template = 'feeds/discussion_title.html'
description_template = 'feeds/discussion_description.html'
def get_object(self, request, year, month, day, slug):
"""Retrieve the discussions by gbobject's slug"""
return get_object_or_404(Gbobject.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 gbobject"""
return obj.discussions[:FEEDS_MAX_ITEMS]
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 link(self, obj):
"""URL of the gbobject"""
return obj.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']
def 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 gbobject %s') % obj.title
class GbobjectComments(GbobjectDiscussions):
"""Feed for comments in an gbobject"""
title_template = 'feeds/comment_title.html'
description_template = 'feeds/comment_description.html'
def items(self, obj):
"""Items are the comments on the gbobject"""
return obj.comments[:FEEDS_MAX_ITEMS]
def item_link(self, item):
"""URL of the comment"""
return item.get_absolute_url('#comment_%(id)s')
def 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 gbobject %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 GbobjectPingbacks(GbobjectDiscussions):
"""Feed for pingbacks in an gbobject"""
title_template = 'feeds/pingback_title.html'
description_template = 'feeds/pingback_description.html'
def items(self, obj):
"""Items are the pingbacks on the gbobject"""
return obj.pingbacks[:FEEDS_MAX_ITEMS]
def item_link(self, item):
"""URL of the pingback"""
return item.get_absolute_url('#pingback_%(id)s')
def 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 gbobject %s') % obj.title
class GbobjectTrackbacks(GbobjectDiscussions):
"""Feed for trackbacks in an gbobject"""
title_template = 'feeds/trackback_title.html'
description_template = 'feeds/trackback_description.html'
def items(self, obj):
"""Items are the trackbacks on the gbobject"""
return obj.trackbacks[:FEEDS_MAX_ITEMS]
def item_link(self, item):
"""URL of the trackback"""
return item.get_absolute_url('#trackback_%(id)s')
def 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 gbobject %s') % obj.title
|