Merge remote-tracking branch 'joar/audio+sniffing'
[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
07163593 17from mediagoblin.db.util import media_entries_for_tag_slug, DESCENDING
71454fd3 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
d8409c26 32
1eff10fa
E
33 for entry in media_entries:
34 for tag in entry.tags:
1a897068 35 if tag['slug'] == tag_slug:
1b4d9eda 36 tag_name = tag['name']
1a897068 37 break
1eff10fa 38 break
d8409c26
E
39 # TODO: Remove after SQL-switch, it's mongo specific
40 if hasattr(media_entries, "rewind"):
41 media_entries.rewind()
1a897068
CAW
42
43 return tag_name
44
71454fd3
CAW
45
46@uses_pagination
47def tag_listing(request, page):
48 """'Gallery'/listing for this tag slug"""
49 tag_slug = request.matchdict[u'tag']
50
07163593 51 cursor = media_entries_for_tag_slug(request.db, tag_slug)
71454fd3 52 cursor = cursor.sort('created', DESCENDING)
243c3843 53
71454fd3
CAW
54 pagination = Pagination(page, cursor)
55 media_entries = pagination()
56
1a897068 57 tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
71454fd3
CAW
58
59 return render_to_response(
60 request,
61 'mediagoblin/listings/tag.html',
1a897068
CAW
62 {'tag_slug': tag_slug,
63 'tag_name': tag_name,
71454fd3
CAW
64 'media_entries': media_entries,
65 'pagination': pagination})
1a897068
CAW
66
67
68ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
69
243c3843 70
1a897068
CAW
71def 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 = request.db.MediaEntry.find(
78 {u'state': u'processed',
79 u'tags.slug': tag_slug})
80 cursor = cursor.sort('created', DESCENDING)
81 cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
82
1df68a35
MA
83 """
84 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
85 """
1a897068
CAW
86 feed = AtomFeed(
87 "MediaGoblin: Feed for tag '%s'" % tag_slug,
88 feed_url=request.url,
1df68a35
MA
89 id='tag:'+request.host+',2011:gallery.tag-%s' % tag_slug,
90 links=[{'href': request.urlgen(
91 'mediagoblin.listings.tags_listing',
92 qualified=True, tag=tag_slug ),
93 'rel': 'alternate',
94 'type': 'text/html'}])
1a897068
CAW
95 for entry in cursor:
96 feed.add(entry.get('title'),
1e72e075 97 entry.description_html,
1df68a35 98 id=entry.url_for_self(request.urlgen,qualified=True),
1a897068 99 content_type='html',
1df68a35
MA
100 author={'name': entry.get_uploader.username,
101 'uri': request.urlgen(
102 'mediagoblin.user_pages.user_home',
103 qualified=True, user=entry.get_uploader.username)},
1a897068 104 updated=entry.get('created'),
1df68a35
MA
105 links=[{
106 'href':entry.url_for_self(
107 request.urlgen,
108 qualified=True),
109 'rel': 'alternate',
110 'type': 'text/html'}])
1a897068
CAW
111
112 return feed.get_response()