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