Merge branch 'auth_docs'
[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 import mg_globals
18 from mediagoblin.db.models import MediaEntry
19 from mediagoblin.db.util import media_entries_for_tag_slug
20 from mediagoblin.tools.pagination import Pagination
21 from mediagoblin.tools.response import render_to_response
22 from mediagoblin.decorators import uses_pagination
23
24 from werkzeug.contrib.atom import AtomFeed
25
26
27 def _get_tag_name_from_entries(media_entries, tag_slug):
28 """
29 Get a tag name from the first entry by looking it up via its slug.
30 """
31 # ... this is slightly hacky looking :\
32 tag_name = tag_slug
33
34 for entry in media_entries:
35 for tag in entry.tags:
36 if tag['slug'] == tag_slug:
37 tag_name = tag['name']
38 break
39 break
40 return tag_name
41
42
43 @uses_pagination
44 def tag_listing(request, page):
45 """'Gallery'/listing for this tag slug"""
46 tag_slug = request.matchdict[u'tag']
47
48 cursor = media_entries_for_tag_slug(request.db, tag_slug)
49 cursor = cursor.order_by(MediaEntry.created.desc())
50
51 pagination = Pagination(page, cursor)
52 media_entries = pagination()
53
54 tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
55
56 return render_to_response(
57 request,
58 'mediagoblin/listings/tag.html',
59 {'tag_slug': tag_slug,
60 'tag_name': tag_name,
61 'media_entries': media_entries,
62 'pagination': pagination})
63
64
65 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
66
67
68 def atom_feed(request):
69 """
70 generates the atom feed with the tag images
71 """
72 tag_slug = request.matchdict.get(u'tag')
73 feed_title = "MediaGoblin Feed"
74 if tag_slug:
75 cursor = media_entries_for_tag_slug(request.db, tag_slug)
76 link = request.urlgen('mediagoblin.listings.tags_listing',
77 qualified=True, tag=tag_slug )
78 feed_title += "for tag '%s'" % tag_slug
79 else: # all recent item feed
80 cursor = MediaEntry.query.filter_by(state=u'processed')
81 link = request.urlgen('index', qualified=True)
82 feed_title += "for all recent items"
83
84 atomlinks = [
85 {'href': link,
86 'rel': 'alternate',
87 'type': 'text/html'}]
88
89 if mg_globals.app_config["push_urls"]:
90 for push_url in mg_globals.app_config["push_urls"]:
91 atomlinks.append({
92 'rel': 'hub',
93 'href': push_url})
94
95 cursor = cursor.order_by(MediaEntry.created.desc())
96 cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
97
98 feed = AtomFeed(
99 feed_title,
100 feed_url=request.url,
101 id=link,
102 links=atomlinks)
103
104 for entry in cursor:
105 feed.add(entry.get('title'),
106 entry.description_html,
107 id=entry.url_for_self(request.urlgen,qualified=True),
108 content_type='html',
109 author={'name': entry.get_uploader.username,
110 'uri': request.urlgen(
111 'mediagoblin.user_pages.user_home',
112 qualified=True, user=entry.get_uploader.username)},
113 updated=entry.get('created'),
114 links=[{
115 'href':entry.url_for_self(
116 request.urlgen,
117 qualified=True),
118 'rel': 'alternate',
119 'type': 'text/html'}])
120
121 return feed.get_response()