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
.decorators
import uses_pagination
21 from mediagoblin
.plugins
.api
.tools
import get_media_file_paths
22 from mediagoblin
.tools
.pagination
import Pagination
23 from mediagoblin
.tools
.response
import render_to_response
25 from werkzeug
.contrib
.atom
import AtomFeed
28 def _get_tag_name_from_entries(media_entries
, tag_slug
):
30 Get a tag name from the first entry by looking it up via its slug.
32 # ... this is slightly hacky looking :\
35 for entry
in media_entries
:
36 for tag
in entry
.tags
:
37 if tag
['slug'] == tag_slug
:
38 tag_name
= tag
['name']
45 def tag_listing(request
, page
):
46 """'Gallery'/listing for this tag slug"""
47 tag_slug
= request
.matchdict
[u
'tag']
49 cursor
= media_entries_for_tag_slug(request
.db
, tag_slug
)
50 cursor
= cursor
.order_by(MediaEntry
.created
.desc())
52 pagination
= Pagination(page
, cursor
)
53 media_entries
= pagination()
55 tag_name
= _get_tag_name_from_entries(media_entries
, tag_slug
)
57 return render_to_response(
59 'mediagoblin/listings/tag.html',
60 {'tag_slug': tag_slug
,
62 'media_entries': media_entries
,
63 'pagination': pagination
})
66 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
= 15
69 def atom_feed(request
):
71 generates the atom feed with the tag images
73 tag_slug
= request
.matchdict
.get(u
'tag')
74 feed_title
= "MediaGoblin Feed"
76 feed_title
+= " for tag '%s'" % tag_slug
77 link
= request
.urlgen('mediagoblin.listings.tags_listing',
78 qualified
=True, tag
=tag_slug
)
79 cursor
= media_entries_for_tag_slug(request
.db
, tag_slug
)
80 else: # all recent item feed
81 feed_title
+= " for all recent items"
82 link
= request
.urlgen('index', qualified
=True)
83 cursor
= MediaEntry
.query
.filter_by(state
=u
'processed')
84 cursor
= cursor
.order_by(MediaEntry
.created
.desc())
85 cursor
= cursor
.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
89 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
96 if mg_globals
.app_config
["push_urls"]:
97 for push_url
in mg_globals
.app_config
["push_urls"]:
104 feed_url
=request
.url
,
109 # Include a thumbnail image in content.
110 file_urls
= get_media_file_paths(entry
.media_files
, request
.urlgen
)
111 if 'thumb' in file_urls
:
112 content
= '<img src="{thumb}" alt='' /> {desc}'.format(
113 thumb
=file_urls
['thumb'], desc
=entry
.description_html
)
115 content
= entry
.description_html
120 id=entry
.url_for_self(request
.urlgen
, qualified
=True),
123 'name': entry
.get_actor
.username
,
124 'uri': request
.urlgen(
125 'mediagoblin.user_pages.user_home',
127 user
=entry
.get_actor
.username
)},
128 updated
=entry
.get('created'),
130 'href': entry
.url_for_self(
134 'type': 'text/html'}])
136 return feed
.get_response()