Fix #5408 - ignore non-int offset in api 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
152a3bfa
AW
20from mediagoblin.tools.pagination import Pagination
21from mediagoblin.tools.response import render_to_response
71454fd3
CAW
22from mediagoblin.decorators import uses_pagination
23
1a897068
CAW
24from werkzeug.contrib.atom import AtomFeed
25
26
27def _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
d8409c26 33
1eff10fa
E
34 for entry in media_entries:
35 for tag in entry.tags:
1a897068 36 if tag['slug'] == tag_slug:
1b4d9eda 37 tag_name = tag['name']
1a897068 38 break
1eff10fa 39 break
1a897068
CAW
40 return tag_name
41
71454fd3
CAW
42
43@uses_pagination
44def tag_listing(request, page):
45 """'Gallery'/listing for this tag slug"""
46 tag_slug = request.matchdict[u'tag']
47
07163593 48 cursor = media_entries_for_tag_slug(request.db, tag_slug)
0efe9e27 49 cursor = cursor.order_by(MediaEntry.created.desc())
243c3843 50
71454fd3
CAW
51 pagination = Pagination(page, cursor)
52 media_entries = pagination()
53
1a897068 54 tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
71454fd3
CAW
55
56 return render_to_response(
57 request,
58 'mediagoblin/listings/tag.html',
1a897068
CAW
59 {'tag_slug': tag_slug,
60 'tag_name': tag_name,
71454fd3
CAW
61 'media_entries': media_entries,
62 'pagination': pagination})
1a897068
CAW
63
64
65ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
66
243c3843 67
ec3f1012 68def atom_feed(request):
1a897068
CAW
69 """
70 generates the atom feed with the tag images
71 """
b624ca0f
SS
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 )
6e2e5b36 78 feed_title += "for tag '%s'" % tag_slug
b624ca0f
SS
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"
1a897068 83
a4dcb1f4
RE
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
0efe9e27 95 cursor = cursor.order_by(MediaEntry.created.desc())
1a897068
CAW
96 cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
97
98 feed = AtomFeed(
b624ca0f 99 feed_title,
1a897068 100 feed_url=request.url,
b624ca0f 101 id=link,
a4dcb1f4
RE
102 links=atomlinks)
103
1a897068
CAW
104 for entry in cursor:
105 feed.add(entry.get('title'),
1e72e075 106 entry.description_html,
1df68a35 107 id=entry.url_for_self(request.urlgen,qualified=True),
1a897068 108 content_type='html',
0f3bf8d4 109 author={'name': entry.get_actor.username,
1df68a35
MA
110 'uri': request.urlgen(
111 'mediagoblin.user_pages.user_home',
0f3bf8d4 112 qualified=True, user=entry.get_actor.username)},
1a897068 113 updated=entry.get('created'),
1df68a35
MA
114 links=[{
115 'href':entry.url_for_self(
116 request.urlgen,
117 qualified=True),
118 'rel': 'alternate',
119 'type': 'text/html'}])
1a897068
CAW
120
121 return feed.get_response()