Moved common, translation, template, and url code out of util.py and into tools/...
[mediagoblin.git] / mediagoblin / listings / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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 DESCENDING
18
19 from mediagoblin.util import Pagination, render_to_response
20 from mediagoblin.decorators import uses_pagination
21
22 from werkzeug.contrib.atom import AtomFeed
23
24
25 def _get_tag_name_from_entries(media_entries, tag_slug):
26 """
27 Get a tag name from the first entry by looking it up via its slug.
28 """
29 # ... this is slightly hacky looking :\
30 tag_name = tag_slug
31 if media_entries.count():
32 for tag in media_entries[0]['tags']:
33 if tag['slug'] == tag_slug:
34 tag_name == tag['name']
35 break
36
37 return tag_name
38
39
40 @uses_pagination
41 def tag_listing(request, page):
42 """'Gallery'/listing for this tag slug"""
43 tag_slug = request.matchdict[u'tag']
44
45 cursor = request.db.MediaEntry.find(
46 {u'state': u'processed',
47 u'tags.slug': tag_slug})
48 cursor = cursor.sort('created', DESCENDING)
49
50 pagination = Pagination(page, cursor)
51 media_entries = pagination()
52
53 tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
54
55 return render_to_response(
56 request,
57 'mediagoblin/listings/tag.html',
58 {'tag_slug': tag_slug,
59 'tag_name': tag_name,
60 'media_entries': media_entries,
61 'pagination': pagination})
62
63
64 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
65
66 def tag_atom_feed(request):
67 """
68 generates the atom feed with the tag images
69 """
70 tag_slug = request.matchdict[u'tag']
71
72 cursor = request.db.MediaEntry.find(
73 {u'state': u'processed',
74 u'tags.slug': tag_slug})
75 cursor = cursor.sort('created', DESCENDING)
76 cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
77
78 feed = AtomFeed(
79 "MediaGoblin: Feed for tag '%s'" % tag_slug,
80 feed_url=request.url,
81 url=request.host_url)
82
83 for entry in cursor:
84 feed.add(entry.get('title'),
85 entry.get('description_html'),
86 content_type='html',
87 author=entry.uploader()['username'],
88 updated=entry.get('created'),
89 url=entry.url_for_self(request.urlgen))
90
91 return feed.get_response()