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
.db
.models
import MediaEntry
18 from mediagoblin
.db
.util
import media_entries_for_tag_slug
19 from mediagoblin
.tools
.pagination
import Pagination
20 from mediagoblin
.tools
.response
import render_to_response
21 from mediagoblin
.decorators
import uses_pagination
23 from werkzeug
.contrib
.atom
import AtomFeed
26 def _get_tag_name_from_entries(media_entries
, tag_slug
):
28 Get a tag name from the first entry by looking it up via its slug.
30 # ... this is slightly hacky looking :\
33 for entry
in media_entries
:
34 for tag
in entry
.tags
:
35 if tag
['slug'] == tag_slug
:
36 tag_name
= tag
['name']
43 def tag_listing(request
, page
):
44 """'Gallery'/listing for this tag slug"""
45 tag_slug
= request
.matchdict
[u
'tag']
47 cursor
= media_entries_for_tag_slug(request
.db
, tag_slug
)
48 cursor
= cursor
.order_by(MediaEntry
.created
.desc())
50 pagination
= Pagination(page
, cursor
)
51 media_entries
= pagination()
53 tag_name
= _get_tag_name_from_entries(media_entries
, tag_slug
)
55 return render_to_response(
57 'mediagoblin/listings/tag.html',
58 {'tag_slug': tag_slug
,
60 'media_entries': media_entries
,
61 'pagination': pagination
})
64 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
= 15
67 def atom_feed(request
):
69 generates the atom feed with the tag images
71 tag_slug
= request
.matchdict
.get(u
'tag')
72 feed_title
= "MediaGoblin Feed"
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
)
77 feed_title
+= "for tag '%s'" % tag_slug
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"
83 cursor
= cursor
.order_by(MediaEntry
.created
.desc())
84 cursor
= cursor
.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
92 'type': 'text/html'}])
94 feed
.add(entry
.get('title'),
95 entry
.description_html
,
96 id=entry
.url_for_self(request
.urlgen
,qualified
=True),
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
)},
102 updated
=entry
.get('created'),
104 'href':entry
.url_for_self(
108 'type': 'text/html'}])
110 return feed
.get_response()