Apply consistency & PEP8 to atom feeds
[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.decorators import uses_pagination
21 from mediagoblin.plugins.api.tools import get_media_file_paths
22 from mediagoblin.tools.pagination import Pagination
23 from mediagoblin.tools.response import render_to_response
24
25 from werkzeug.contrib.atom import AtomFeed
26
27
28 def _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
34
35 for entry in media_entries:
36 for tag in entry.tags:
37 if tag['slug'] == tag_slug:
38 tag_name = tag['name']
39 break
40 break
41 return tag_name
42
43
44 @uses_pagination
45 def tag_listing(request, page):
46 """'Gallery'/listing for this tag slug"""
47 tag_slug = request.matchdict[u'tag']
48
49 cursor = media_entries_for_tag_slug(request.db, tag_slug)
50 cursor = cursor.order_by(MediaEntry.created.desc())
51
52 pagination = Pagination(page, cursor)
53 media_entries = pagination()
54
55 tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
56
57 return render_to_response(
58 request,
59 'mediagoblin/listings/tag.html',
60 {'tag_slug': tag_slug,
61 'tag_name': tag_name,
62 'media_entries': media_entries,
63 'pagination': pagination})
64
65
66 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
67
68
69 def atom_feed(request):
70 """
71 generates the atom feed with the tag images
72 """
73 tag_slug = request.matchdict.get(u'tag')
74 feed_title = "MediaGoblin Feed"
75 if tag_slug:
76 feed_title += " for tag '%s'" % tag_slug
77 link = request.urlgen('mediagoblin.listings.tags_listing',
78 qualified=True, tag=tag_slug )
79 cursor = media_entries_for_tag_slug(request.db, tag_slug)
80 else: # all recent item feed
81 feed_title += " for all recent items"
82 link = request.urlgen('index', qualified=True)
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)
86
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'}]
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
102 feed = AtomFeed(
103 feed_title,
104 feed_url=request.url,
105 id=link,
106 links=atomlinks)
107
108 for entry in cursor:
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:
112 content = '<img src="{thumb}" alt='' /> {desc}'.format(
113 thumb=file_urls['thumb'], desc=entry.description_html)
114 else:
115 content = entry.description_html
116
117 feed.add(
118 entry.get('title'),
119 content,
120 id=entry.url_for_self(request.urlgen, qualified=True),
121 content_type='html',
122 author={
123 'name': entry.get_actor.username,
124 'uri': request.urlgen(
125 'mediagoblin.user_pages.user_home',
126 qualified=True,
127 user=entry.get_actor.username)},
128 updated=entry.get('created'),
129 links=[{
130 'href': entry.url_for_self(
131 request.urlgen,
132 qualified=True),
133 'rel': 'alternate',
134 'type': 'text/html'}])
135
136 return feed.get_response()