Fix #5398 and #5395 - Fix tests failing due to problem creating connection for alembic
[larjonas-mediagoblin.git] / mediagoblin / listings / views.py
blobf640cc9521281ee748ca7215cd2e727bb5d99558
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
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
24 from werkzeug.contrib.atom import AtomFeed
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
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
43 @uses_pagination
44 def tag_listing(request, page):
45 """'Gallery'/listing for this tag slug"""
46 tag_slug = request.matchdict[u'tag']
48 cursor = media_entries_for_tag_slug(request.db, tag_slug)
49 cursor = cursor.order_by(MediaEntry.created.desc())
51 pagination = Pagination(page, cursor)
52 media_entries = pagination()
54 tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
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})
65 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
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"
84 atomlinks = [
85 {'href': link,
86 'rel': 'alternate',
87 'type': 'text/html'}]
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})
95 cursor = cursor.order_by(MediaEntry.created.desc())
96 cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
98 feed = AtomFeed(
99 feed_title,
100 feed_url=request.url,
101 id=link,
102 links=atomlinks)
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_actor.username,
110 'uri': request.urlgen(
111 'mediagoblin.user_pages.user_home',
112 qualified=True, user=entry.get_actor.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'}])
121 return feed.get_response()