It's 2012 all up in here
[mediagoblin.git] / mediagoblin / listings / views.py
CommitLineData
71454fd3 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
71454fd3
CAW
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
17from mediagoblin.db.util import DESCENDING
18
152a3bfa
AW
19from mediagoblin.tools.pagination import Pagination
20from mediagoblin.tools.response import render_to_response
71454fd3
CAW
21from mediagoblin.decorators import uses_pagination
22
1a897068
CAW
23from werkzeug.contrib.atom import AtomFeed
24
25
26def _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 if media_entries.count():
33 for tag in media_entries[0]['tags']:
34 if tag['slug'] == tag_slug:
35 tag_name == tag['name']
36 break
37
38 return tag_name
39
71454fd3
CAW
40
41@uses_pagination
42def tag_listing(request, page):
43 """'Gallery'/listing for this tag slug"""
44 tag_slug = request.matchdict[u'tag']
45
46 cursor = request.db.MediaEntry.find(
47 {u'state': u'processed',
48 u'tags.slug': tag_slug})
49 cursor = cursor.sort('created', DESCENDING)
243c3843 50
71454fd3
CAW
51 pagination = Pagination(page, cursor)
52 media_entries = pagination()
53
1a897068 54 tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
71454fd3
CAW
55
56 return render_to_response(
57 request,
58 'mediagoblin/listings/tag.html',
1a897068
CAW
59 {'tag_slug': tag_slug,
60 'tag_name': tag_name,
71454fd3
CAW
61 'media_entries': media_entries,
62 'pagination': pagination})
1a897068
CAW
63
64
65ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
66
243c3843 67
1a897068
CAW
68def tag_atom_feed(request):
69 """
70 generates the atom feed with the tag images
71 """
72 tag_slug = request.matchdict[u'tag']
73
74 cursor = request.db.MediaEntry.find(
75 {u'state': u'processed',
76 u'tags.slug': tag_slug})
77 cursor = cursor.sort('created', DESCENDING)
78 cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
79
1df68a35
MA
80 """
81 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
82 """
1a897068
CAW
83 feed = AtomFeed(
84 "MediaGoblin: Feed for tag '%s'" % tag_slug,
85 feed_url=request.url,
1df68a35
MA
86 id='tag:'+request.host+',2011:gallery.tag-%s' % tag_slug,
87 links=[{'href': request.urlgen(
88 'mediagoblin.listings.tags_listing',
89 qualified=True, tag=tag_slug ),
90 'rel': 'alternate',
91 'type': 'text/html'}])
1a897068
CAW
92 for entry in cursor:
93 feed.add(entry.get('title'),
94 entry.get('description_html'),
1df68a35 95 id=entry.url_for_self(request.urlgen,qualified=True),
1a897068 96 content_type='html',
1df68a35
MA
97 author={'name': entry.get_uploader.username,
98 'uri': request.urlgen(
99 'mediagoblin.user_pages.user_home',
100 qualified=True, user=entry.get_uploader.username)},
1a897068 101 updated=entry.get('created'),
1df68a35
MA
102 links=[{
103 'href':entry.url_for_self(
104 request.urlgen,
105 qualified=True),
106 'rel': 'alternate',
107 'type': 'text/html'}])
1a897068
CAW
108
109 return feed.get_response()