Merge remote-tracking branch 'is_derek/bug405_email_notifications_for_comments' into...
[mediagoblin.git] / mediagoblin / listings / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # 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 mediagoblin.db.util import media_entries_for_tag_slug, DESCENDING
18
19 from mediagoblin.tools.pagination import Pagination
20 from mediagoblin.tools.response import render_to_response
21 from mediagoblin.decorators import uses_pagination
22
23 from werkzeug.contrib.atom import AtomFeed
24
25
26 def _get_tag_name_from_entries(media_entries, tag_slug):
27 """
28 Get a tag name from the first entry by looking it up via its slug.
29 """
30 # ... this is slightly hacky looking :\
31 tag_name = tag_slug
32
33 for entry in media_entries:
34 for tag in entry.tags:
35 if tag['slug'] == tag_slug:
36 tag_name = tag['name']
37 break
38 break
39 # TODO: Remove after SQL-switch, it's mongo specific
40 if hasattr(media_entries, "rewind"):
41 media_entries.rewind()
42
43 return tag_name
44
45
46 @uses_pagination
47 def tag_listing(request, page):
48 """'Gallery'/listing for this tag slug"""
49 tag_slug = request.matchdict[u'tag']
50
51 cursor = media_entries_for_tag_slug(request.db, tag_slug)
52 cursor = cursor.sort('created', DESCENDING)
53
54 pagination = Pagination(page, cursor)
55 media_entries = pagination()
56
57 tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
58
59 return render_to_response(
60 request,
61 'mediagoblin/listings/tag.html',
62 {'tag_slug': tag_slug,
63 'tag_name': tag_name,
64 'media_entries': media_entries,
65 'pagination': pagination})
66
67
68 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
69
70
71 def tag_atom_feed(request):
72 """
73 generates the atom feed with the tag images
74 """
75 tag_slug = request.matchdict[u'tag']
76
77 cursor = media_entries_for_tag_slug(request.db, tag_slug)
78 cursor = cursor.sort('created', DESCENDING)
79 cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
80
81 """
82 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
83 """
84 feed = AtomFeed(
85 "MediaGoblin: Feed for tag '%s'" % tag_slug,
86 feed_url=request.url,
87 id='tag:'+request.host+',2011:gallery.tag-%s' % tag_slug,
88 links=[{'href': request.urlgen(
89 'mediagoblin.listings.tags_listing',
90 qualified=True, tag=tag_slug ),
91 'rel': 'alternate',
92 'type': 'text/html'}])
93 for entry in cursor:
94 feed.add(entry.get('title'),
95 entry.description_html,
96 id=entry.url_for_self(request.urlgen,qualified=True),
97 content_type='html',
98 author={'name': entry.get_uploader.username,
99 'uri': request.urlgen(
100 'mediagoblin.user_pages.user_home',
101 qualified=True, user=entry.get_uploader.username)},
102 updated=entry.get('created'),
103 links=[{
104 'href':entry.url_for_self(
105 request.urlgen,
106 qualified=True),
107 'rel': 'alternate',
108 'type': 'text/html'}])
109
110 return feed.get_response()