1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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/>.
19 from mediagoblin
import messages
20 from mediagoblin
.db
.util
import DESCENDING
, ObjectId
21 from mediagoblin
.util
import (
22 Pagination
, render_to_response
, redirect
, cleaned_markdown_conversion
)
23 from mediagoblin
.user_pages
import forms
as user_forms
25 from mediagoblin
.decorators
import uses_pagination
, get_user_media_entry
, \
28 from werkzeug
.contrib
.atom
import AtomFeed
32 def user_home(request
, page
):
33 """'Homepage' of a User()"""
34 user
= request
.db
.User
.find_one({
35 'username': request
.matchdict
['user'],
38 return exc
.HTTPNotFound()
40 cursor
= request
.db
.MediaEntry
.find(
41 {'uploader': user
['_id'],
42 'state': 'processed'}).sort('created', DESCENDING
)
44 pagination
= Pagination(page
, cursor
)
45 media_entries
= pagination()
47 #if no data is available, return NotFound
48 if media_entries
== None:
49 return exc
.HTTPNotFound()
51 user_gallery_url
= request
.urlgen(
52 'mediagoblin.user_pages.user_gallery',
53 user
=user
['username'])
55 return render_to_response(
57 'mediagoblin/user_pages/user.html',
59 'user_gallery_url': user_gallery_url
,
60 'media_entries': media_entries
,
61 'pagination': pagination
})
64 def user_gallery(request
, page
):
65 """'Gallery' of a User()"""
66 user
= request
.db
.User
.find_one({
67 'username': request
.matchdict
['user'],
70 return exc
.HTTPNotFound()
72 cursor
= request
.db
.MediaEntry
.find(
73 {'uploader': user
['_id'],
74 'state': 'processed'}).sort('created', DESCENDING
)
76 pagination
= Pagination(page
, cursor
)
77 media_entries
= pagination()
79 #if no data is available, return NotFound
80 if media_entries
== None:
81 return exc
.HTTPNotFound()
83 return render_to_response(
85 'mediagoblin/user_pages/gallery.html',
87 'media_entries': media_entries
,
88 'pagination': pagination
})
90 MEDIA_COMMENTS_PER_PAGE
= 50
94 def media_home(request
, media
, page
, **kwargs
):
96 'Homepage' of a MediaEntry()
99 pagination
= Pagination(page
, media
.get_comments(), MEDIA_COMMENTS_PER_PAGE
)
100 comments
= pagination()
102 comment_form
= user_forms
.MediaCommentForm(request
.POST
)
104 return render_to_response(
106 'mediagoblin/user_pages/media.html',
108 'comments': comments
,
109 'pagination': pagination
,
110 'comment_form': comment_form
})
113 @require_active_login
114 def media_post_comment(request
):
116 recieves POST from a MediaEntry() comment form, saves the comment.
118 comment
= request
.db
.MediaComment()
119 comment
['media_entry'] = ObjectId(request
.matchdict
['media'])
120 comment
['author'] = request
.user
['_id']
121 comment
['content'] = request
.POST
['comment']
123 comment
['content_html'] = cleaned_markdown_conversion(comment
['content'])
127 messages
.add_message(
128 request
, messages
.SUCCESS
,
131 return redirect(request
, 'mediagoblin.user_pages.media_home',
132 media
= request
.matchdict
['media'],
133 user
= request
.matchdict
['user'])
136 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
= 5
138 def atom_feed(request
):
140 generates the atom feed with the newest images
143 user
= request
.db
.User
.find_one({
144 'username': request
.matchdict
['user'],
147 return exc
.HTTPNotFound()
149 cursor
= request
.db
.MediaEntry
.find({
150 'uploader': user
['_id'],
151 'state': 'processed'}) \
152 .sort('created', DESCENDING
) \
153 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
155 feed
= AtomFeed(request
.matchdict
['user'],
156 feed_url
=request
.url
,
157 url
=request
.host_url
)
160 feed
.add(entry
.get('title'),
161 entry
.get('description_html'),
163 author
=request
.matchdict
['user'],
164 updated
=entry
.get('created'),
165 url
=entry
.url_for_self(request
.urlgen
))
167 return feed
.get_response()