+ comment.media_entry = media.id
+ comment.author = request.user.id
+ comment.content = unicode(request.form['comment_content'])
+
+ # Show error message if commenting is disabled.
+ if not mg_globals.app_config['allow_comments']:
+ messages.add_message(
+ request,
+ messages.ERROR,
+ _("Sorry, comments are disabled."))
+ elif not comment.content.strip():
+ messages.add_message(
+ request,
+ messages.ERROR,
+ _("Oops, your comment was empty."))
+ else:
+ comment.save()
+
+ messages.add_message(
+ request, messages.SUCCESS,
+ _('Your comment has been posted!'))
+
+ trigger_notification(comment, media, request)
+
+ add_comment_subscription(request.user, media)
+
+ return redirect_obj(request, media)
+
+
+@get_media_entry_by_id
+@require_active_login
+def media_collect(request, media):
+ """Add media to collection submission"""
+
+ form = user_forms.MediaCollectForm(request.form)
+ # A user's own collections:
+ form.collection.query = Collection.query.filter_by(
+ creator = request.user.id).order_by(Collection.title)
+
+ if request.method != 'POST' or not form.validate():
+ # No POST submission, or invalid form
+ if not form.validate():
+ messages.add_message(request, messages.ERROR,
+ _('Please check your entries and try again.'))
+
+ return render_to_response(
+ request,
+ 'mediagoblin/user_pages/media_collect.html',
+ {'media': media,
+ 'form': form})
+
+ # If we are here, method=POST and the form is valid, submit things.
+ # If the user is adding a new collection, use that:
+ if form.collection_title.data:
+ # Make sure this user isn't duplicating an existing collection
+ existing_collection = Collection.query.filter_by(
+ creator=request.user.id,
+ title=form.collection_title.data).first()
+ if existing_collection:
+ messages.add_message(request, messages.ERROR,
+ _('You already have a collection called "%s"!')
+ % existing_collection.title)
+ return redirect(request, "mediagoblin.user_pages.media_home",
+ user=media.get_uploader.username,
+ media=media.slug_or_id)
+
+ collection = Collection()
+ collection.title = form.collection_title.data
+ collection.description = form.collection_description.data
+ collection.creator = request.user.id
+ collection.generate_slug()
+ collection.save()
+
+ # Otherwise, use the collection selected from the drop-down
+ else:
+ collection = form.collection.data
+ if collection and collection.creator != request.user.id:
+ collection = None
+
+ # Make sure the user actually selected a collection
+ if not collection:
+ messages.add_message(
+ request, messages.ERROR,
+ _('You have to select or add a collection'))
+ return redirect(request, "mediagoblin.user_pages.media_collect",
+ user=media.get_uploader.username,
+ media_id=media.id)
+
+
+ # Check whether media already exists in collection
+ elif CollectionItem.query.filter_by(
+ media_entry=media.id,
+ collection=collection.id).first():
+ messages.add_message(request, messages.ERROR,
+ _('"%s" already in collection "%s"')
+ % (media.title, collection.title))
+ else: # Add item to collection
+ add_media_to_collection(collection, media, form.note.data)
+
+ messages.add_message(request, messages.SUCCESS,
+ _('"%s" added to collection "%s"')
+ % (media.title, collection.title))
+
+ return redirect_obj(request, media)
+
+
+#TODO: Why does @user_may_delete_media not implicate @require_active_login?
+@get_media_entry_by_id
+@require_active_login
+@user_may_delete_media
+def media_confirm_delete(request, media):
+
+ form = user_forms.ConfirmDeleteForm(request.form)
+
+ if request.method == 'POST' and form.validate():
+ if form.confirm.data is True:
+ username = media.get_uploader.username
+ # Delete MediaEntry and all related files, comments etc.
+ media.delete()
+ messages.add_message(
+ request, messages.SUCCESS, _('You deleted the media.'))
+
+ return redirect(request, "mediagoblin.user_pages.user_home",
+ user=username)
+ else:
+ messages.add_message(
+ request, messages.ERROR,
+ _("The media was not deleted because you didn't check that you were sure."))
+ return redirect_obj(request, media)
+
+ if ((request.user.is_admin and
+ request.user.id != media.uploader)):
+ messages.add_message(
+ request, messages.WARNING,
+ _("You are about to delete another user's media. "
+ "Proceed with caution."))
+
+ return render_to_response(
+ request,
+ 'mediagoblin/user_pages/media_confirm_delete.html',
+ {'media': media,
+ 'form': form})
+
+
+@active_user_from_url
+@uses_pagination
+def user_collection(request, page, url_user=None):
+ """A User-defined Collection"""
+ collection = Collection.query.filter_by(
+ get_creator=url_user,
+ slug=request.matchdict['collection']).first()
+
+ if not collection:
+ return render_404(request)
+
+ cursor = collection.get_collection_items()
+
+ pagination = Pagination(page, cursor)
+ collection_items = pagination()
+
+ # if no data is available, return NotFound
+ # TODO: Should an empty collection really also return 404?
+ if collection_items == None:
+ return render_404(request)
+
+ return render_to_response(
+ request,
+ 'mediagoblin/user_pages/collection.html',
+ {'user': url_user,
+ 'collection': collection,
+ 'collection_items': collection_items,
+ 'pagination': pagination})
+
+
+@active_user_from_url
+def collection_list(request, url_user=None):
+ """A User-defined Collection"""
+ collections = Collection.query.filter_by(
+ get_creator=url_user)
+
+ return render_to_response(
+ request,
+ 'mediagoblin/user_pages/collection_list.html',
+ {'user': url_user,
+ 'collections': collections})