Merge branch 'master' of gitorious.org:mediagoblin/mediagoblin
[mediagoblin.git] / mediagoblin / listings / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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 DESCENDING
18
19 from mediagoblin.util import Pagination, render_to_response
20 from mediagoblin.decorators import uses_pagination
21
22
23 @uses_pagination
24 def tag_listing(request, page):
25 """'Gallery'/listing for this tag slug"""
26 tag_slug = request.matchdict[u'tag']
27
28 cursor = request.db.MediaEntry.find(
29 {u'state': u'processed',
30 u'tags.slug': tag_slug})
31 cursor = cursor.sort('created', DESCENDING)
32
33 pagination = Pagination(page, cursor)
34 media_entries = pagination()
35
36 # Take the tag "name" from the first MediaEntry's non-normalized
37 # tag naming.
38 # ... this is slightly hacky looking :\
39 tag_name = tag_slug
40 if media_entries.count():
41 for tag in media_entries[0]['tags']:
42 if tag['slug'] == tag_slug:
43 tag_name == tag['name']
44 break
45 else:
46 tag_name = tag_slug
47
48 return render_to_response(
49 request,
50 'mediagoblin/listings/tag.html',
51 {'tag_name': tag_name,
52 'media_entries': media_entries,
53 'pagination': pagination})