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