1 # 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
, mg_globals
20 from mediagoblin
.db
.util
import DESCENDING
, ObjectId
21 from mediagoblin
.util
import (
22 Pagination
, render_to_response
, redirect
, cleaned_markdown_conversion
,
24 from mediagoblin
.user_pages
import forms
as user_forms
26 from mediagoblin
.decorators
import (uses_pagination
, get_user_media_entry
,
29 from werkzeug
.contrib
.atom
import AtomFeed
33 def user_home(request
, page
):
34 """'Homepage' of a User()"""
35 user
= request
.db
.User
.find_one({
36 'username': request
.matchdict
['user']})
38 return render_404(request
)
39 elif user
['status'] != u
'active':
40 return render_to_response(
42 'mediagoblin/user_pages/user.html',
45 cursor
= request
.db
.MediaEntry
.find(
46 {'uploader': user
['_id'],
47 'state': 'processed'}).sort('created', DESCENDING
)
49 pagination
= Pagination(page
, cursor
)
50 media_entries
= pagination()
52 #if no data is available, return NotFound
53 if media_entries
== None:
54 return render_404(request
)
56 user_gallery_url
= request
.urlgen(
57 'mediagoblin.user_pages.user_gallery',
58 user
=user
['username'])
60 return render_to_response(
62 'mediagoblin/user_pages/user.html',
64 'user_gallery_url': user_gallery_url
,
65 'media_entries': media_entries
,
66 'pagination': pagination
})
69 def user_gallery(request
, page
):
70 """'Gallery' of a User()"""
71 user
= request
.db
.User
.find_one({
72 'username': request
.matchdict
['user'],
75 return render_404(request
)
77 cursor
= request
.db
.MediaEntry
.find(
78 {'uploader': user
['_id'],
79 'state': 'processed'}).sort('created', DESCENDING
)
81 pagination
= Pagination(page
, cursor
)
82 media_entries
= pagination()
84 #if no data is available, return NotFound
85 if media_entries
== None:
86 return render_404(request
)
88 return render_to_response(
90 'mediagoblin/user_pages/gallery.html',
92 'media_entries': media_entries
,
93 'pagination': pagination
})
95 MEDIA_COMMENTS_PER_PAGE
= 50
99 def media_home(request
, media
, page
, **kwargs
):
101 'Homepage' of a MediaEntry()
103 if ObjectId(request
.matchdict
.get('comment')):
104 pagination
= Pagination(
105 page
, media
.get_comments(), MEDIA_COMMENTS_PER_PAGE
,
106 ObjectId(request
.matchdict
.get('comment')))
108 pagination
= Pagination(
109 page
, media
.get_comments(), MEDIA_COMMENTS_PER_PAGE
)
111 comments
= pagination()
113 comment_form
= user_forms
.MediaCommentForm(request
.POST
)
115 return render_to_response(
117 'mediagoblin/user_pages/media.html',
119 'comments': comments
,
120 'pagination': pagination
,
121 'comment_form': comment_form
,
122 'app_config': mg_globals
.app_config
})
125 @require_active_login
126 def media_post_comment(request
):
128 recieves POST from a MediaEntry() comment form, saves the comment.
130 comment
= request
.db
.MediaComment()
131 comment
['media_entry'] = ObjectId(request
.matchdict
['media'])
132 comment
['author'] = request
.user
['_id']
133 comment
['content'] = request
.POST
['comment_content']
135 comment
['content_html'] = cleaned_markdown_conversion(comment
['content'])
139 messages
.add_message(
140 request
, messages
.SUCCESS
,
143 return redirect(request
, 'mediagoblin.user_pages.media_home',
144 media
= request
.matchdict
['media'],
145 user
= request
.matchdict
['user'])
148 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
= 15
150 def atom_feed(request
):
152 generates the atom feed with the newest images
155 user
= request
.db
.User
.find_one({
156 'username': request
.matchdict
['user'],
159 return render_404(request
)
161 cursor
= request
.db
.MediaEntry
.find({
162 'uploader': user
['_id'],
163 'state': 'processed'}) \
164 .sort('created', DESCENDING
) \
165 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
167 feed
= AtomFeed(request
.matchdict
['user'],
168 feed_url
=request
.url
,
169 url
=request
.host_url
)
172 feed
.add(entry
.get('title'),
173 entry
.get('description_html'),
175 author
=request
.matchdict
['user'],
176 updated
=entry
.get('created'),
177 url
=entry
.url_for_self(request
.urlgen
))
179 return feed
.get_response()
182 @require_active_login
183 def processing_panel(request
):
185 Show to the user what media is still in conversion/processing...
186 and what failed, and why!
189 user
= request
.db
.User
.find_one(
190 {'username': request
.matchdict
['user'],
193 # Make sure the user exists and is active
195 return render_404(request
)
196 elif user
['status'] != u
'active':
197 return render_to_response(
199 'mediagoblin/user_pages/user.html',
202 # XXX: Should this be a decorator?
204 # Make sure we have permission to access this user's panel. Only
205 # admins and this user herself should be able to do so.
206 if not (user
[u
'_id'] == request
.user
[u
'_id']
207 or request
.user
.is_admin
):
208 # No? Let's simply redirect to this user's homepage then.
210 request
, 'mediagoblin.user_pages.user_home',
211 user
=request
.matchdict
['user'])
213 # Get media entries which are in-processing
214 processing_entries
= request
.db
.MediaEntry
.find(
215 {'uploader': user
['_id'],
216 'state': 'processing'}).sort('created', DESCENDING
)
218 # Get media entries which have failed to process
219 failed_entries
= request
.db
.MediaEntry
.find(
220 {'uploader': user
['_id'],
221 'state': 'failed'}).sort('created', DESCENDING
)
224 return render_to_response(
226 'mediagoblin/user_pages/processing_panel.html',
228 'processing_entries': processing_entries
,
229 'failed_entries': failed_entries
})