Commit | Line | Data |
---|---|---|
01c75c7e | 1 | # MediaGoblin -- federated, autonomous media hosting |
9a16e16f SS |
2 | # Copyright (C) 2011 Free Software Foundation, Inc |
3 | # | |
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. | |
8 | # | |
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. | |
13 | # | |
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/>. | |
16 | ||
1c63ad5d | 17 | from webob import exc |
52359e91 | 18 | |
3a8c3a38 | 19 | from mediagoblin import messages, mg_globals |
9074ee7c | 20 | from mediagoblin.db.util import DESCENDING, ObjectId |
95e6da02 | 21 | from mediagoblin.util import ( |
de12b4e7 | 22 | Pagination, render_to_response, redirect, cleaned_markdown_conversion, |
502073f2 | 23 | render_404, delete_media_files) |
7a4c0126 | 24 | from mediagoblin.util import pass_to_ugettext as _ |
9074ee7c | 25 | from mediagoblin.user_pages import forms as user_forms |
f6249408 | 26 | |
50854db0 | 27 | from mediagoblin.decorators import (uses_pagination, get_user_media_entry, |
502073f2 | 28 | require_active_login, user_may_delete_media) |
9a16e16f | 29 | |
00c39256 | 30 | from werkzeug.contrib.atom import AtomFeed |
1301a8ad | 31 | |
9074ee7c | 32 | |
3eb6fc4f | 33 | @uses_pagination |
1301a8ad | 34 | def user_home(request, page): |
9a16e16f | 35 | """'Homepage' of a User()""" |
7acdbfd3 | 36 | user = request.db.User.find_one({ |
990d3b69 | 37 | 'username': request.matchdict['user']}) |
7acdbfd3 | 38 | if not user: |
de12b4e7 | 39 | return render_404(request) |
990d3b69 CAW |
40 | elif user['status'] != u'active': |
41 | return render_to_response( | |
42 | request, | |
43 | 'mediagoblin/user_pages/user.html', | |
44 | {'user': user}) | |
9a16e16f | 45 | |
434b3221 | 46 | cursor = request.db.MediaEntry.find( |
16509be1 | 47 | {'uploader': user['_id'], |
434b3221 | 48 | 'state': 'processed'}).sort('created', DESCENDING) |
9a16e16f | 49 | |
1301a8ad | 50 | pagination = Pagination(page, cursor) |
ca3ca51c | 51 | media_entries = pagination() |
44e3e917 | 52 | |
ae85ed0f BK |
53 | #if no data is available, return NotFound |
54 | if media_entries == None: | |
de12b4e7 | 55 | return render_404(request) |
ae85ed0f | 56 | |
5949be9a CAW |
57 | user_gallery_url = request.urlgen( |
58 | 'mediagoblin.user_pages.user_gallery', | |
59 | user=user['username']) | |
60 | ||
9038c9f9 CAW |
61 | return render_to_response( |
62 | request, | |
c9c24934 E |
63 | 'mediagoblin/user_pages/user.html', |
64 | {'user': user, | |
5949be9a | 65 | 'user_gallery_url': user_gallery_url, |
c9c24934 E |
66 | 'media_entries': media_entries, |
67 | 'pagination': pagination}) | |
f6249408 | 68 | |
184f2240 | 69 | @uses_pagination |
70 | def user_gallery(request, page): | |
71 | """'Gallery' of a User()""" | |
72 | user = request.db.User.find_one({ | |
73 | 'username': request.matchdict['user'], | |
74 | 'status': 'active'}) | |
75 | if not user: | |
de12b4e7 | 76 | return render_404(request) |
184f2240 | 77 | |
78 | cursor = request.db.MediaEntry.find( | |
79 | {'uploader': user['_id'], | |
80 | 'state': 'processed'}).sort('created', DESCENDING) | |
81 | ||
82 | pagination = Pagination(page, cursor) | |
83 | media_entries = pagination() | |
84 | ||
85 | #if no data is available, return NotFound | |
86 | if media_entries == None: | |
de12b4e7 | 87 | return render_404(request) |
184f2240 | 88 | |
4b5f5a08 | 89 | return render_to_response( |
90 | request, | |
91 | 'mediagoblin/user_pages/gallery.html', | |
92 | {'user': user, | |
93 | 'media_entries': media_entries, | |
94 | 'pagination': pagination}) | |
184f2240 | 95 | |
6f59a3a3 | 96 | MEDIA_COMMENTS_PER_PAGE = 50 |
434b3221 | 97 | |
01674e10 | 98 | @get_user_media_entry |
9074ee7c | 99 | @uses_pagination |
6f59a3a3 | 100 | def media_home(request, media, page, **kwargs): |
9074ee7c JW |
101 | """ |
102 | 'Homepage' of a MediaEntry() | |
103 | """ | |
af2fcba5 JW |
104 | if ObjectId(request.matchdict.get('comment')): |
105 | pagination = Pagination( | |
106 | page, media.get_comments(), MEDIA_COMMENTS_PER_PAGE, | |
107 | ObjectId(request.matchdict.get('comment'))) | |
108 | else: | |
109 | pagination = Pagination( | |
110 | page, media.get_comments(), MEDIA_COMMENTS_PER_PAGE) | |
9074ee7c | 111 | |
6f59a3a3 | 112 | comments = pagination() |
9074ee7c | 113 | |
6f59a3a3 | 114 | comment_form = user_forms.MediaCommentForm(request.POST) |
9074ee7c | 115 | |
9038c9f9 CAW |
116 | return render_to_response( |
117 | request, | |
c9c24934 | 118 | 'mediagoblin/user_pages/media.html', |
9074ee7c JW |
119 | {'media': media, |
120 | 'comments': comments, | |
121 | 'pagination': pagination, | |
3a8c3a38 JW |
122 | 'comment_form': comment_form, |
123 | 'app_config': mg_globals.app_config}) | |
9074ee7c | 124 | |
95e6da02 | 125 | |
9074ee7c JW |
126 | @require_active_login |
127 | def media_post_comment(request): | |
128 | """ | |
129 | recieves POST from a MediaEntry() comment form, saves the comment. | |
130 | """ | |
131 | comment = request.db.MediaComment() | |
132 | comment['media_entry'] = ObjectId(request.matchdict['media']) | |
133 | comment['author'] = request.user['_id'] | |
08750772 | 134 | comment['content'] = unicode(request.POST['comment_content']) |
9074ee7c | 135 | |
95e6da02 | 136 | comment['content_html'] = cleaned_markdown_conversion(comment['content']) |
9074ee7c JW |
137 | |
138 | comment.save() | |
b5d3aec6 | 139 | |
52359e91 CAW |
140 | messages.add_message( |
141 | request, messages.SUCCESS, | |
142 | 'Comment posted!') | |
143 | ||
9074ee7c JW |
144 | return redirect(request, 'mediagoblin.user_pages.media_home', |
145 | media = request.matchdict['media'], | |
146 | user = request.matchdict['user']) | |
00c39256 | 147 | |
95e6da02 | 148 | |
502073f2 JW |
149 | @get_user_media_entry |
150 | @require_active_login | |
151 | @user_may_delete_media | |
152 | def media_confirm_delete(request, media): | |
153 | ||
154 | form = user_forms.ConfirmDeleteForm(request.POST) | |
155 | ||
156 | if request.method == 'POST' and form.validate(): | |
157 | if request.POST.get('confirm') == 'True': | |
158 | username = media.uploader()['username'] | |
159 | ||
160 | # Delete all files on the public storage | |
161 | delete_media_files(media) | |
162 | ||
163 | media.delete() | |
164 | ||
165 | return redirect(request, "mediagoblin.user_pages.user_home", | |
166 | user=username) | |
167 | else: | |
168 | return redirect(request, "mediagoblin.user_pages.media_home", | |
169 | user=media.uploader()['username'], | |
170 | media=media['slug']) | |
171 | ||
7a4c0126 CAW |
172 | if ((request.user[u'is_admin'] and |
173 | request.user[u'_id'] != media.uploader()[u'_id'])): | |
174 | messages.add_message( | |
175 | request, messages.WARNING, | |
176 | _("You are about to delete another user's media. " | |
177 | "Proceed with caution.")) | |
178 | ||
502073f2 JW |
179 | return render_to_response( |
180 | request, | |
181 | 'mediagoblin/user_pages/media_confirm_delete.html', | |
182 | {'media': media, | |
183 | 'form': form}) | |
184 | ||
185 | ||
a5303e47 | 186 | ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15 |
00c39256 BK |
187 | |
188 | def atom_feed(request): | |
189 | """ | |
190 | generates the atom feed with the newest images | |
191 | """ | |
192 | ||
193 | user = request.db.User.find_one({ | |
194 | 'username': request.matchdict['user'], | |
195 | 'status': 'active'}) | |
196 | if not user: | |
de12b4e7 | 197 | return render_404(request) |
00c39256 BK |
198 | |
199 | cursor = request.db.MediaEntry.find({ | |
200 | 'uploader': user['_id'], | |
201 | 'state': 'processed'}) \ | |
202 | .sort('created', DESCENDING) \ | |
203 | .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS) | |
204 | ||
205 | feed = AtomFeed(request.matchdict['user'], | |
206 | feed_url=request.url, | |
207 | url=request.host_url) | |
44e2da2f | 208 | |
00c39256 BK |
209 | for entry in cursor: |
210 | feed.add(entry.get('title'), | |
44e2da2f | 211 | entry.get('description_html'), |
00c39256 BK |
212 | content_type='html', |
213 | author=request.matchdict['user'], | |
214 | updated=entry.get('created'), | |
215 | url=entry.url_for_self(request.urlgen)) | |
216 | ||
9074ee7c | 217 | return feed.get_response() |
01c75c7e CAW |
218 | |
219 | ||
220 | @require_active_login | |
221 | def processing_panel(request): | |
222 | """ | |
223 | Show to the user what media is still in conversion/processing... | |
224 | and what failed, and why! | |
225 | """ | |
226 | # Get the user | |
227 | user = request.db.User.find_one( | |
228 | {'username': request.matchdict['user'], | |
229 | 'status': 'active'}) | |
230 | ||
231 | # Make sure the user exists and is active | |
232 | if not user: | |
de12b4e7 | 233 | return render_404(request) |
01c75c7e CAW |
234 | elif user['status'] != u'active': |
235 | return render_to_response( | |
236 | request, | |
237 | 'mediagoblin/user_pages/user.html', | |
238 | {'user': user}) | |
239 | ||
240 | # XXX: Should this be a decorator? | |
241 | # | |
242 | # Make sure we have permission to access this user's panel. Only | |
243 | # admins and this user herself should be able to do so. | |
244 | if not (user[u'_id'] == request.user[u'_id'] | |
245 | or request.user.is_admin): | |
246 | # No? Let's simply redirect to this user's homepage then. | |
247 | return redirect( | |
248 | request, 'mediagoblin.user_pages.user_home', | |
249 | user=request.matchdict['user']) | |
250 | ||
251 | # Get media entries which are in-processing | |
252 | processing_entries = request.db.MediaEntry.find( | |
253 | {'uploader': user['_id'], | |
254 | 'state': 'processing'}).sort('created', DESCENDING) | |
255 | ||
256 | # Get media entries which have failed to process | |
257 | failed_entries = request.db.MediaEntry.find( | |
258 | {'uploader': user['_id'], | |
259 | 'state': 'failed'}).sort('created', DESCENDING) | |
260 | ||
261 | # Render to response | |
262 | return render_to_response( | |
263 | request, | |
264 | 'mediagoblin/user_pages/processing_panel.html', | |
265 | {'user': user, | |
266 | 'processing_entries': processing_entries, | |
267 | 'failed_entries': failed_entries}) |