Fix #5500 UnicodeEncodeError in atom feed
[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
a4dcb1f4 17from mediagoblin import mg_globals
b0c8328e 18from mediagoblin.db.models import MediaEntry
0efe9e27 19from mediagoblin.db.util import media_entries_for_tag_slug
65d8ca70
BS
20from mediagoblin.decorators import uses_pagination
21from mediagoblin.plugins.api.tools import get_media_file_paths
152a3bfa
AW
22from mediagoblin.tools.pagination import Pagination
23from mediagoblin.tools.response import render_to_response
71454fd3 24
1a897068
CAW
25from werkzeug.contrib.atom import AtomFeed
26
27
28def _get_tag_name_from_entries(media_entries, tag_slug):
29 """
30 Get a tag name from the first entry by looking it up via its slug.
31 """
32 # ... this is slightly hacky looking :\
33 tag_name = tag_slug
d8409c26 34
1eff10fa
E
35 for entry in media_entries:
36 for tag in entry.tags:
1a897068 37 if tag['slug'] == tag_slug:
1b4d9eda 38 tag_name = tag['name']
1a897068 39 break
1eff10fa 40 break
1a897068
CAW
41 return tag_name
42
71454fd3
CAW
43
44@uses_pagination
45def tag_listing(request, page):
46 """'Gallery'/listing for this tag slug"""
47 tag_slug = request.matchdict[u'tag']
48
07163593 49 cursor = media_entries_for_tag_slug(request.db, tag_slug)
0efe9e27 50 cursor = cursor.order_by(MediaEntry.created.desc())
243c3843 51
71454fd3
CAW
52 pagination = Pagination(page, cursor)
53 media_entries = pagination()
54
1a897068 55 tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
71454fd3
CAW
56
57 return render_to_response(
58 request,
59 'mediagoblin/listings/tag.html',
1a897068
CAW
60 {'tag_slug': tag_slug,
61 'tag_name': tag_name,
71454fd3
CAW
62 'media_entries': media_entries,
63 'pagination': pagination})
1a897068
CAW
64
65
66ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
67
243c3843 68
ec3f1012 69def atom_feed(request):
1a897068
CAW
70 """
71 generates the atom feed with the tag images
72 """
b624ca0f
SS
73 tag_slug = request.matchdict.get(u'tag')
74 feed_title = "MediaGoblin Feed"
75 if tag_slug:
2a01c5e7 76 feed_title += " for tag '%s'" % tag_slug
b624ca0f
SS
77 link = request.urlgen('mediagoblin.listings.tags_listing',
78 qualified=True, tag=tag_slug )
2a01c5e7 79 cursor = media_entries_for_tag_slug(request.db, tag_slug)
b624ca0f 80 else: # all recent item feed
2a01c5e7 81 feed_title += " for all recent items"
b624ca0f 82 link = request.urlgen('index', qualified=True)
2a01c5e7
AB
83 cursor = MediaEntry.query.filter_by(state=u'processed')
84 cursor = cursor.order_by(MediaEntry.created.desc())
85 cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
1a897068 86
2a01c5e7
AB
87
88 """
89 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
90 """
91 atomlinks = [{
92 'href': link,
93 'rel': 'alternate',
94 'type': 'text/html'}]
a4dcb1f4
RE
95
96 if mg_globals.app_config["push_urls"]:
97 for push_url in mg_globals.app_config["push_urls"]:
98 atomlinks.append({
99 'rel': 'hub',
100 'href': push_url})
101
1a897068 102 feed = AtomFeed(
b624ca0f 103 feed_title,
1a897068 104 feed_url=request.url,
b624ca0f 105 id=link,
a4dcb1f4
RE
106 links=atomlinks)
107
1a897068 108 for entry in cursor:
65d8ca70
BS
109 # Include a thumbnail image in content.
110 file_urls = get_media_file_paths(entry.media_files, request.urlgen)
111 if 'thumb' in file_urls:
741c25fd 112 content = u'<img src="{thumb}" alt='' /> {desc}'.format(
65d8ca70
BS
113 thumb=file_urls['thumb'], desc=entry.description_html)
114 else:
115 content = entry.description_html
116
2a01c5e7
AB
117 feed.add(
118 entry.get('title'),
65d8ca70 119 content,
2a01c5e7 120 id=entry.url_for_self(request.urlgen, qualified=True),
1a897068 121 content_type='html',
2a01c5e7
AB
122 author={
123 'name': entry.get_actor.username,
1df68a35
MA
124 'uri': request.urlgen(
125 'mediagoblin.user_pages.user_home',
2a01c5e7
AB
126 qualified=True,
127 user=entry.get_actor.username)},
1a897068 128 updated=entry.get('created'),
1df68a35 129 links=[{
2a01c5e7
AB
130 'href': entry.url_for_self(
131 request.urlgen,
132 qualified=True),
1df68a35
MA
133 'rel': 'alternate',
134 'type': 'text/html'}])
1a897068
CAW
135
136 return feed.get_response()