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
):
29 Get a tag name from the first entry by looking it up via its slug.
31 # ... this is slightly hacky looking :\
34 for entry
in media_entries
:
35 for tag
in entry
.tags
:
36 if tag
['slug'] == tag_slug
:
37 tag_name
= tag
['name']
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(
58 'mediagoblin/listings/tag.html',
59 {'tag_slug': tag_slug
,
61 'media_entries': media_entries
,
62 'pagination': pagination
})
65 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
= 15
68 def atom_feed(request
):
70 generates the atom feed with the tag images
72 tag_slug
= request
.matchdict
.get(u
'tag')
73 feed_title
= "MediaGoblin Feed"
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"
89 if mg_globals
.app_config
["push_urls"]:
90 for push_url
in mg_globals
.app_config
["push_urls"]:
95 cursor
= cursor
.order_by(MediaEntry
.created
.desc())
96 cursor
= cursor
.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
100 feed_url
=request
.url
,
105 feed
.add(entry
.get('title'),
106 entry
.description_html
,
107 id=entry
.url_for_self(request
.urlgen
,qualified
=True),
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'),
115 'href':entry
.url_for_self(
119 'type': 'text/html'}])
121 return feed
.get_response()