| 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
| 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
| 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 | |
| 17 | import logging |
| 18 | import datetime |
| 19 | |
| 20 | from mediagoblin import messages, mg_globals |
| 21 | from mediagoblin.db.models import (MediaEntry, MediaTag, Collection, |
| 22 | CollectionItem, User, MediaComment, |
| 23 | CommentReport, MediaReport) |
| 24 | from mediagoblin.tools.response import render_to_response, render_404, \ |
| 25 | redirect, redirect_obj |
| 26 | from mediagoblin.tools.translate import pass_to_ugettext as _ |
| 27 | from mediagoblin.tools.pagination import Pagination |
| 28 | from mediagoblin.user_pages import forms as user_forms |
| 29 | from mediagoblin.user_pages.lib import (send_comment_email, |
| 30 | add_media_to_collection, build_report_object) |
| 31 | from mediagoblin.notifications import trigger_notification, \ |
| 32 | add_comment_subscription, mark_comment_notification_seen |
| 33 | |
| 34 | from mediagoblin.decorators import (uses_pagination, get_user_media_entry, |
| 35 | get_media_entry_by_id, user_has_privilege, |
| 36 | require_active_login, user_may_delete_media, user_may_alter_collection, |
| 37 | get_user_collection, get_user_collection_item, active_user_from_url, |
| 38 | get_media_comment_by_id, user_not_banned) |
| 39 | |
| 40 | from werkzeug.contrib.atom import AtomFeed |
| 41 | from werkzeug.exceptions import MethodNotAllowed |
| 42 | |
| 43 | |
| 44 | _log = logging.getLogger(__name__) |
| 45 | _log.setLevel(logging.DEBUG) |
| 46 | |
| 47 | @user_not_banned |
| 48 | @uses_pagination |
| 49 | def user_home(request, page): |
| 50 | """'Homepage' of a User()""" |
| 51 | # TODO: decide if we only want homepages for active users, we can |
| 52 | # then use the @get_active_user decorator and also simplify the |
| 53 | # template html. |
| 54 | user = User.query.filter_by(username=request.matchdict['user']).first() |
| 55 | if not user: |
| 56 | return render_404(request) |
| 57 | elif user.status != u'active': |
| 58 | return render_to_response( |
| 59 | request, |
| 60 | 'mediagoblin/user_pages/user.html', |
| 61 | {'user': user}) |
| 62 | |
| 63 | cursor = MediaEntry.query.\ |
| 64 | filter_by(uploader = user.id, |
| 65 | state = u'processed').order_by(MediaEntry.created.desc()) |
| 66 | |
| 67 | pagination = Pagination(page, cursor) |
| 68 | media_entries = pagination() |
| 69 | |
| 70 | #if no data is available, return NotFound |
| 71 | if media_entries == None: |
| 72 | return render_404(request) |
| 73 | |
| 74 | user_gallery_url = request.urlgen( |
| 75 | 'mediagoblin.user_pages.user_gallery', |
| 76 | user=user.username) |
| 77 | |
| 78 | return render_to_response( |
| 79 | request, |
| 80 | 'mediagoblin/user_pages/user.html', |
| 81 | {'user': user, |
| 82 | 'user_gallery_url': user_gallery_url, |
| 83 | 'media_entries': media_entries, |
| 84 | 'pagination': pagination}) |
| 85 | |
| 86 | @user_not_banned |
| 87 | @active_user_from_url |
| 88 | @uses_pagination |
| 89 | def user_gallery(request, page, url_user=None): |
| 90 | """'Gallery' of a User()""" |
| 91 | tag = request.matchdict.get('tag', None) |
| 92 | cursor = MediaEntry.query.filter_by( |
| 93 | uploader=url_user.id, |
| 94 | state=u'processed').order_by(MediaEntry.created.desc()) |
| 95 | |
| 96 | # Filter potentially by tag too: |
| 97 | if tag: |
| 98 | cursor = cursor.filter( |
| 99 | MediaEntry.tags_helper.any( |
| 100 | MediaTag.slug == request.matchdict['tag'])) |
| 101 | |
| 102 | # Paginate gallery |
| 103 | pagination = Pagination(page, cursor) |
| 104 | media_entries = pagination() |
| 105 | |
| 106 | #if no data is available, return NotFound |
| 107 | # TODO: Should we really also return 404 for empty galleries? |
| 108 | if media_entries == None: |
| 109 | return render_404(request) |
| 110 | |
| 111 | return render_to_response( |
| 112 | request, |
| 113 | 'mediagoblin/user_pages/gallery.html', |
| 114 | {'user': url_user, 'tag': tag, |
| 115 | 'media_entries': media_entries, |
| 116 | 'pagination': pagination}) |
| 117 | |
| 118 | |
| 119 | MEDIA_COMMENTS_PER_PAGE = 50 |
| 120 | |
| 121 | @user_not_banned |
| 122 | @get_user_media_entry |
| 123 | @uses_pagination |
| 124 | def media_home(request, media, page, **kwargs): |
| 125 | """ |
| 126 | 'Homepage' of a MediaEntry() |
| 127 | """ |
| 128 | comment_id = request.matchdict.get('comment', None) |
| 129 | if comment_id: |
| 130 | if request.user: |
| 131 | mark_comment_notification_seen(comment_id, request.user) |
| 132 | |
| 133 | pagination = Pagination( |
| 134 | page, media.get_comments( |
| 135 | mg_globals.app_config['comments_ascending']), |
| 136 | MEDIA_COMMENTS_PER_PAGE, |
| 137 | comment_id) |
| 138 | else: |
| 139 | pagination = Pagination( |
| 140 | page, media.get_comments( |
| 141 | mg_globals.app_config['comments_ascending']), |
| 142 | MEDIA_COMMENTS_PER_PAGE) |
| 143 | |
| 144 | comments = pagination() |
| 145 | |
| 146 | comment_form = user_forms.MediaCommentForm(request.form) |
| 147 | |
| 148 | media_template_name = media.media_manager.display_template |
| 149 | |
| 150 | return render_to_response( |
| 151 | request, |
| 152 | media_template_name, |
| 153 | {'media': media, |
| 154 | 'comments': comments, |
| 155 | 'pagination': pagination, |
| 156 | 'comment_form': comment_form, |
| 157 | 'app_config': mg_globals.app_config}) |
| 158 | |
| 159 | |
| 160 | @get_media_entry_by_id |
| 161 | @require_active_login |
| 162 | @user_has_privilege(u'commenter') |
| 163 | def media_post_comment(request, media): |
| 164 | """ |
| 165 | recieves POST from a MediaEntry() comment form, saves the comment. |
| 166 | """ |
| 167 | if not request.method == 'POST': |
| 168 | raise MethodNotAllowed() |
| 169 | |
| 170 | comment = request.db.MediaComment() |
| 171 | comment.media_entry = media.id |
| 172 | comment.author = request.user.id |
| 173 | comment.content = unicode(request.form['comment_content']) |
| 174 | |
| 175 | # Show error message if commenting is disabled. |
| 176 | if not mg_globals.app_config['allow_comments']: |
| 177 | messages.add_message( |
| 178 | request, |
| 179 | messages.ERROR, |
| 180 | _("Sorry, comments are disabled.")) |
| 181 | elif not comment.content.strip(): |
| 182 | messages.add_message( |
| 183 | request, |
| 184 | messages.ERROR, |
| 185 | _("Oops, your comment was empty.")) |
| 186 | else: |
| 187 | comment.save() |
| 188 | |
| 189 | messages.add_message( |
| 190 | request, messages.SUCCESS, |
| 191 | _('Your comment has been posted!')) |
| 192 | |
| 193 | trigger_notification(comment, media, request) |
| 194 | |
| 195 | add_comment_subscription(request.user, media) |
| 196 | |
| 197 | return redirect_obj(request, media) |
| 198 | |
| 199 | @user_not_banned |
| 200 | @get_media_entry_by_id |
| 201 | @require_active_login |
| 202 | def media_collect(request, media): |
| 203 | """Add media to collection submission""" |
| 204 | |
| 205 | form = user_forms.MediaCollectForm(request.form) |
| 206 | # A user's own collections: |
| 207 | form.collection.query = Collection.query.filter_by( |
| 208 | creator = request.user.id).order_by(Collection.title) |
| 209 | |
| 210 | if request.method != 'POST' or not form.validate(): |
| 211 | # No POST submission, or invalid form |
| 212 | if not form.validate(): |
| 213 | messages.add_message(request, messages.ERROR, |
| 214 | _('Please check your entries and try again.')) |
| 215 | |
| 216 | return render_to_response( |
| 217 | request, |
| 218 | 'mediagoblin/user_pages/media_collect.html', |
| 219 | {'media': media, |
| 220 | 'form': form}) |
| 221 | |
| 222 | # If we are here, method=POST and the form is valid, submit things. |
| 223 | # If the user is adding a new collection, use that: |
| 224 | if form.collection_title.data: |
| 225 | # Make sure this user isn't duplicating an existing collection |
| 226 | existing_collection = Collection.query.filter_by( |
| 227 | creator=request.user.id, |
| 228 | title=form.collection_title.data).first() |
| 229 | if existing_collection: |
| 230 | messages.add_message(request, messages.ERROR, |
| 231 | _('You already have a collection called "%s"!') |
| 232 | % existing_collection.title) |
| 233 | return redirect(request, "mediagoblin.user_pages.media_home", |
| 234 | user=media.get_uploader.username, |
| 235 | media=media.slug_or_id) |
| 236 | |
| 237 | collection = Collection() |
| 238 | collection.title = form.collection_title.data |
| 239 | collection.description = form.collection_description.data |
| 240 | collection.creator = request.user.id |
| 241 | collection.generate_slug() |
| 242 | collection.save() |
| 243 | |
| 244 | # Otherwise, use the collection selected from the drop-down |
| 245 | else: |
| 246 | collection = form.collection.data |
| 247 | if collection and collection.creator != request.user.id: |
| 248 | collection = None |
| 249 | |
| 250 | # Make sure the user actually selected a collection |
| 251 | if not collection: |
| 252 | messages.add_message( |
| 253 | request, messages.ERROR, |
| 254 | _('You have to select or add a collection')) |
| 255 | return redirect(request, "mediagoblin.user_pages.media_collect", |
| 256 | user=media.get_uploader.username, |
| 257 | media_id=media.id) |
| 258 | |
| 259 | |
| 260 | # Check whether media already exists in collection |
| 261 | elif CollectionItem.query.filter_by( |
| 262 | media_entry=media.id, |
| 263 | collection=collection.id).first(): |
| 264 | messages.add_message(request, messages.ERROR, |
| 265 | _('"%s" already in collection "%s"') |
| 266 | % (media.title, collection.title)) |
| 267 | else: # Add item to collection |
| 268 | add_media_to_collection(collection, media, form.note.data) |
| 269 | |
| 270 | messages.add_message(request, messages.SUCCESS, |
| 271 | _('"%s" added to collection "%s"') |
| 272 | % (media.title, collection.title)) |
| 273 | |
| 274 | return redirect_obj(request, media) |
| 275 | |
| 276 | |
| 277 | #TODO: Why does @user_may_delete_media not implicate @require_active_login? |
| 278 | @user_not_banned |
| 279 | @get_media_entry_by_id |
| 280 | @require_active_login |
| 281 | @user_may_delete_media |
| 282 | def media_confirm_delete(request, media): |
| 283 | |
| 284 | form = user_forms.ConfirmDeleteForm(request.form) |
| 285 | |
| 286 | if request.method == 'POST' and form.validate(): |
| 287 | if form.confirm.data is True: |
| 288 | username = media.get_uploader.username |
| 289 | # Delete MediaEntry and all related files, comments etc. |
| 290 | media.delete() |
| 291 | messages.add_message( |
| 292 | request, messages.SUCCESS, _('You deleted the media.')) |
| 293 | |
| 294 | return redirect(request, "mediagoblin.user_pages.user_home", |
| 295 | user=username) |
| 296 | else: |
| 297 | messages.add_message( |
| 298 | request, messages.ERROR, |
| 299 | _("The media was not deleted because you didn't check that you were sure.")) |
| 300 | return redirect_obj(request, media) |
| 301 | |
| 302 | if ((request.user.is_admin and |
| 303 | request.user.id != media.uploader)): |
| 304 | messages.add_message( |
| 305 | request, messages.WARNING, |
| 306 | _("You are about to delete another user's media. " |
| 307 | "Proceed with caution.")) |
| 308 | |
| 309 | return render_to_response( |
| 310 | request, |
| 311 | 'mediagoblin/user_pages/media_confirm_delete.html', |
| 312 | {'media': media, |
| 313 | 'form': form}) |
| 314 | |
| 315 | @user_not_banned |
| 316 | @active_user_from_url |
| 317 | @uses_pagination |
| 318 | def user_collection(request, page, url_user=None): |
| 319 | """A User-defined Collection""" |
| 320 | collection = Collection.query.filter_by( |
| 321 | get_creator=url_user, |
| 322 | slug=request.matchdict['collection']).first() |
| 323 | |
| 324 | if not collection: |
| 325 | return render_404(request) |
| 326 | |
| 327 | cursor = collection.get_collection_items() |
| 328 | |
| 329 | pagination = Pagination(page, cursor) |
| 330 | collection_items = pagination() |
| 331 | |
| 332 | # if no data is available, return NotFound |
| 333 | # TODO: Should an empty collection really also return 404? |
| 334 | if collection_items == None: |
| 335 | return render_404(request) |
| 336 | |
| 337 | return render_to_response( |
| 338 | request, |
| 339 | 'mediagoblin/user_pages/collection.html', |
| 340 | {'user': url_user, |
| 341 | 'collection': collection, |
| 342 | 'collection_items': collection_items, |
| 343 | 'pagination': pagination}) |
| 344 | |
| 345 | @user_not_banned |
| 346 | @active_user_from_url |
| 347 | def collection_list(request, url_user=None): |
| 348 | """A User-defined Collection""" |
| 349 | collections = Collection.query.filter_by( |
| 350 | get_creator=url_user) |
| 351 | |
| 352 | return render_to_response( |
| 353 | request, |
| 354 | 'mediagoblin/user_pages/collection_list.html', |
| 355 | {'user': url_user, |
| 356 | 'collections': collections}) |
| 357 | |
| 358 | |
| 359 | @get_user_collection_item |
| 360 | @require_active_login |
| 361 | @user_may_alter_collection |
| 362 | def collection_item_confirm_remove(request, collection_item): |
| 363 | |
| 364 | form = user_forms.ConfirmCollectionItemRemoveForm(request.form) |
| 365 | |
| 366 | if request.method == 'POST' and form.validate(): |
| 367 | username = collection_item.in_collection.get_creator.username |
| 368 | collection = collection_item.in_collection |
| 369 | |
| 370 | if form.confirm.data is True: |
| 371 | entry = collection_item.get_media_entry |
| 372 | entry.collected = entry.collected - 1 |
| 373 | entry.save() |
| 374 | |
| 375 | collection_item.delete() |
| 376 | collection.items = collection.items - 1 |
| 377 | collection.save() |
| 378 | |
| 379 | messages.add_message( |
| 380 | request, messages.SUCCESS, _('You deleted the item from the collection.')) |
| 381 | else: |
| 382 | messages.add_message( |
| 383 | request, messages.ERROR, |
| 384 | _("The item was not removed because you didn't check that you were sure.")) |
| 385 | |
| 386 | return redirect_obj(request, collection) |
| 387 | |
| 388 | if ((request.user.is_admin and |
| 389 | request.user.id != collection_item.in_collection.creator)): |
| 390 | messages.add_message( |
| 391 | request, messages.WARNING, |
| 392 | _("You are about to delete an item from another user's collection. " |
| 393 | "Proceed with caution.")) |
| 394 | |
| 395 | return render_to_response( |
| 396 | request, |
| 397 | 'mediagoblin/user_pages/collection_item_confirm_remove.html', |
| 398 | {'collection_item': collection_item, |
| 399 | 'form': form}) |
| 400 | |
| 401 | @user_not_banned |
| 402 | @get_user_collection |
| 403 | @require_active_login |
| 404 | @user_may_alter_collection |
| 405 | def collection_confirm_delete(request, collection): |
| 406 | |
| 407 | form = user_forms.ConfirmDeleteForm(request.form) |
| 408 | |
| 409 | if request.method == 'POST' and form.validate(): |
| 410 | |
| 411 | username = collection.get_creator.username |
| 412 | |
| 413 | if form.confirm.data is True: |
| 414 | collection_title = collection.title |
| 415 | |
| 416 | # Delete all the associated collection items |
| 417 | for item in collection.get_collection_items(): |
| 418 | entry = item.get_media_entry |
| 419 | entry.collected = entry.collected - 1 |
| 420 | entry.save() |
| 421 | item.delete() |
| 422 | |
| 423 | collection.delete() |
| 424 | messages.add_message(request, messages.SUCCESS, |
| 425 | _('You deleted the collection "%s"') % collection_title) |
| 426 | |
| 427 | return redirect(request, "mediagoblin.user_pages.user_home", |
| 428 | user=username) |
| 429 | else: |
| 430 | messages.add_message( |
| 431 | request, messages.ERROR, |
| 432 | _("The collection was not deleted because you didn't check that you were sure.")) |
| 433 | |
| 434 | return redirect_obj(request, collection) |
| 435 | |
| 436 | if ((request.user.is_admin and |
| 437 | request.user.id != collection.creator)): |
| 438 | messages.add_message( |
| 439 | request, messages.WARNING, |
| 440 | _("You are about to delete another user's collection. " |
| 441 | "Proceed with caution.")) |
| 442 | |
| 443 | return render_to_response( |
| 444 | request, |
| 445 | 'mediagoblin/user_pages/collection_confirm_delete.html', |
| 446 | {'collection': collection, |
| 447 | 'form': form}) |
| 448 | |
| 449 | |
| 450 | ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15 |
| 451 | |
| 452 | |
| 453 | def atom_feed(request): |
| 454 | """ |
| 455 | generates the atom feed with the newest images |
| 456 | """ |
| 457 | user = User.query.filter_by( |
| 458 | username = request.matchdict['user'], |
| 459 | status = u'active').first() |
| 460 | if not user: |
| 461 | return render_404(request) |
| 462 | |
| 463 | cursor = MediaEntry.query.filter_by( |
| 464 | uploader = user.id, |
| 465 | state = u'processed').\ |
| 466 | order_by(MediaEntry.created.desc()).\ |
| 467 | limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS) |
| 468 | |
| 469 | """ |
| 470 | ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI) |
| 471 | """ |
| 472 | atomlinks = [{ |
| 473 | 'href': request.urlgen( |
| 474 | 'mediagoblin.user_pages.user_home', |
| 475 | qualified=True, user=request.matchdict['user']), |
| 476 | 'rel': 'alternate', |
| 477 | 'type': 'text/html' |
| 478 | }] |
| 479 | |
| 480 | if mg_globals.app_config["push_urls"]: |
| 481 | for push_url in mg_globals.app_config["push_urls"]: |
| 482 | atomlinks.append({ |
| 483 | 'rel': 'hub', |
| 484 | 'href': push_url}) |
| 485 | |
| 486 | feed = AtomFeed( |
| 487 | "MediaGoblin: Feed for user '%s'" % request.matchdict['user'], |
| 488 | feed_url=request.url, |
| 489 | id='tag:{host},{year}:gallery.user-{user}'.format( |
| 490 | host=request.host, |
| 491 | year=datetime.datetime.today().strftime('%Y'), |
| 492 | user=request.matchdict['user']), |
| 493 | links=atomlinks) |
| 494 | |
| 495 | for entry in cursor: |
| 496 | feed.add(entry.get('title'), |
| 497 | entry.description_html, |
| 498 | id=entry.url_for_self(request.urlgen, qualified=True), |
| 499 | content_type='html', |
| 500 | author={ |
| 501 | 'name': entry.get_uploader.username, |
| 502 | 'uri': request.urlgen( |
| 503 | 'mediagoblin.user_pages.user_home', |
| 504 | qualified=True, user=entry.get_uploader.username)}, |
| 505 | updated=entry.get('created'), |
| 506 | links=[{ |
| 507 | 'href': entry.url_for_self( |
| 508 | request.urlgen, |
| 509 | qualified=True), |
| 510 | 'rel': 'alternate', |
| 511 | 'type': 'text/html'}]) |
| 512 | |
| 513 | return feed.get_response() |
| 514 | |
| 515 | |
| 516 | def collection_atom_feed(request): |
| 517 | """ |
| 518 | generates the atom feed with the newest images from a collection |
| 519 | """ |
| 520 | user = User.query.filter_by( |
| 521 | username = request.matchdict['user'], |
| 522 | status = u'active').first() |
| 523 | if not user: |
| 524 | return render_404(request) |
| 525 | |
| 526 | collection = Collection.query.filter_by( |
| 527 | creator=user.id, |
| 528 | slug=request.matchdict['collection']).first() |
| 529 | if not collection: |
| 530 | return render_404(request) |
| 531 | |
| 532 | cursor = CollectionItem.query.filter_by( |
| 533 | collection=collection.id) \ |
| 534 | .order_by(CollectionItem.added.desc()) \ |
| 535 | .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS) |
| 536 | |
| 537 | """ |
| 538 | ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI) |
| 539 | """ |
| 540 | atomlinks = [{ |
| 541 | 'href': collection.url_for_self(request.urlgen, qualified=True), |
| 542 | 'rel': 'alternate', |
| 543 | 'type': 'text/html' |
| 544 | }] |
| 545 | |
| 546 | if mg_globals.app_config["push_urls"]: |
| 547 | for push_url in mg_globals.app_config["push_urls"]: |
| 548 | atomlinks.append({ |
| 549 | 'rel': 'hub', |
| 550 | 'href': push_url}) |
| 551 | |
| 552 | feed = AtomFeed( |
| 553 | "MediaGoblin: Feed for %s's collection %s" % |
| 554 | (request.matchdict['user'], collection.title), |
| 555 | feed_url=request.url, |
| 556 | id=u'tag:{host},{year}:gnu-mediagoblin.{user}.collection.{slug}'\ |
| 557 | .format( |
| 558 | host=request.host, |
| 559 | year=collection.created.strftime('%Y'), |
| 560 | user=request.matchdict['user'], |
| 561 | slug=collection.slug), |
| 562 | links=atomlinks) |
| 563 | |
| 564 | for item in cursor: |
| 565 | entry = item.get_media_entry |
| 566 | feed.add(entry.get('title'), |
| 567 | item.note_html, |
| 568 | id=entry.url_for_self(request.urlgen, qualified=True), |
| 569 | content_type='html', |
| 570 | author={ |
| 571 | 'name': entry.get_uploader.username, |
| 572 | 'uri': request.urlgen( |
| 573 | 'mediagoblin.user_pages.user_home', |
| 574 | qualified=True, user=entry.get_uploader.username)}, |
| 575 | updated=item.get('added'), |
| 576 | links=[{ |
| 577 | 'href': entry.url_for_self( |
| 578 | request.urlgen, |
| 579 | qualified=True), |
| 580 | 'rel': 'alternate', |
| 581 | 'type': 'text/html'}]) |
| 582 | |
| 583 | return feed.get_response() |
| 584 | |
| 585 | @user_not_banned |
| 586 | @require_active_login |
| 587 | def processing_panel(request): |
| 588 | """ |
| 589 | Show to the user what media is still in conversion/processing... |
| 590 | and what failed, and why! |
| 591 | """ |
| 592 | user = User.query.filter_by(username=request.matchdict['user']).first() |
| 593 | # TODO: XXX: Should this be a decorator? |
| 594 | # |
| 595 | # Make sure we have permission to access this user's panel. Only |
| 596 | # admins and this user herself should be able to do so. |
| 597 | if not (user.id == request.user.id or request.user.is_admin): |
| 598 | # No? Simply redirect to this user's homepage. |
| 599 | return redirect( |
| 600 | request, 'mediagoblin.user_pages.user_home', |
| 601 | user=user.username) |
| 602 | |
| 603 | # Get media entries which are in-processing |
| 604 | processing_entries = MediaEntry.query.\ |
| 605 | filter_by(uploader = user.id, |
| 606 | state = u'processing').\ |
| 607 | order_by(MediaEntry.created.desc()) |
| 608 | |
| 609 | # Get media entries which have failed to process |
| 610 | failed_entries = MediaEntry.query.\ |
| 611 | filter_by(uploader = user.id, |
| 612 | state = u'failed').\ |
| 613 | order_by(MediaEntry.created.desc()) |
| 614 | |
| 615 | processed_entries = MediaEntry.query.\ |
| 616 | filter_by(uploader = user.id, |
| 617 | state = u'processed').\ |
| 618 | order_by(MediaEntry.created.desc()).\ |
| 619 | limit(10) |
| 620 | |
| 621 | # Render to response |
| 622 | return render_to_response( |
| 623 | request, |
| 624 | 'mediagoblin/user_pages/processing_panel.html', |
| 625 | {'user': user, |
| 626 | 'processing_entries': processing_entries, |
| 627 | 'failed_entries': failed_entries, |
| 628 | 'processed_entries': processed_entries}) |
| 629 | |
| 630 | @require_active_login |
| 631 | @get_user_media_entry |
| 632 | @user_has_privilege(u'reporter') |
| 633 | def file_a_report(request, media, comment=None): |
| 634 | if comment is not None: |
| 635 | form = user_forms.CommentReportForm(request.form) |
| 636 | form.reporter_id.data = request.user.id |
| 637 | context = {'media': media, |
| 638 | 'comment':comment, |
| 639 | 'form':form} |
| 640 | else: |
| 641 | form = user_forms.MediaReportForm(request.form) |
| 642 | form.reporter_id.data = request.user.id |
| 643 | context = {'media': media, |
| 644 | 'form':form} |
| 645 | |
| 646 | if request.method == "POST": |
| 647 | report_table = build_report_object(form, |
| 648 | media_entry=media, |
| 649 | comment=comment) |
| 650 | |
| 651 | # if the object was built successfully, report_table will not be None |
| 652 | if report_table: |
| 653 | report_table.save() |
| 654 | return redirect( |
| 655 | request, |
| 656 | 'index') |
| 657 | |
| 658 | |
| 659 | return render_to_response( |
| 660 | request, |
| 661 | 'mediagoblin/user_pages/report.html', |
| 662 | context) |
| 663 | |
| 664 | @require_active_login |
| 665 | @get_user_media_entry |
| 666 | @get_media_comment_by_id |
| 667 | def file_a_comment_report(request, media, comment): |
| 668 | return file_a_report(request, comment=comment) |