diff options
Diffstat (limited to 'mediagoblin/user_pages/views.py')
-rw-r--r-- | mediagoblin/user_pages/views.py | 170 |
1 files changed, 117 insertions, 53 deletions
diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index 21b0c016..64fa793e 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -16,42 +16,47 @@ import logging import datetime +import json from mediagoblin import messages, mg_globals from mediagoblin.db.models import (MediaEntry, MediaTag, Collection, CollectionItem, User) -from mediagoblin.tools.response import render_to_response, render_404, redirect +from mediagoblin.tools.response import render_to_response, render_404, \ + redirect, redirect_obj +from mediagoblin.tools.text import cleaned_markdown_conversion from mediagoblin.tools.translate import pass_to_ugettext as _ from mediagoblin.tools.pagination import Pagination from mediagoblin.user_pages import forms as user_forms from mediagoblin.user_pages.lib import (send_comment_email, - add_media_to_collection) + add_media_to_collection, build_report_object) +from mediagoblin.notifications import trigger_notification, \ + add_comment_subscription, mark_comment_notification_seen from mediagoblin.decorators import (uses_pagination, get_user_media_entry, - get_media_entry_by_id, + get_media_entry_by_id, user_has_privilege, user_not_banned, require_active_login, user_may_delete_media, user_may_alter_collection, - get_user_collection, get_user_collection_item, active_user_from_url) + get_user_collection, get_user_collection_item, active_user_from_url, + get_optional_media_comment_by_id, allow_reporting) from werkzeug.contrib.atom import AtomFeed +from werkzeug.exceptions import MethodNotAllowed +from werkzeug.wrappers import Response _log = logging.getLogger(__name__) _log.setLevel(logging.DEBUG) - +@user_not_banned @uses_pagination def user_home(request, page): """'Homepage' of a User()""" - # TODO: decide if we only want homepages for active users, we can - # then use the @get_active_user decorator and also simplify the - # template html. user = User.query.filter_by(username=request.matchdict['user']).first() if not user: return render_404(request) - elif user.status != u'active': + elif not user.has_privilege(u'active'): return render_to_response( request, - 'mediagoblin/user_pages/user.html', + 'mediagoblin/user_pages/user_nonactive.html', {'user': user}) cursor = MediaEntry.query.\ @@ -77,7 +82,7 @@ def user_home(request, page): 'media_entries': media_entries, 'pagination': pagination}) - +@user_not_banned @active_user_from_url @uses_pagination def user_gallery(request, page, url_user=None): @@ -109,9 +114,10 @@ def user_gallery(request, page, url_user=None): 'media_entries': media_entries, 'pagination': pagination}) -MEDIA_COMMENTS_PER_PAGE = 50 +MEDIA_COMMENTS_PER_PAGE = 50 +@user_not_banned @get_user_media_entry @uses_pagination def media_home(request, media, page, **kwargs): @@ -120,6 +126,9 @@ def media_home(request, media, page, **kwargs): """ comment_id = request.matchdict.get('comment', None) if comment_id: + if request.user: + mark_comment_notification_seen(comment_id, request.user) + pagination = Pagination( page, media.get_comments( mg_globals.app_config['comments_ascending']), @@ -135,7 +144,7 @@ def media_home(request, media, page, **kwargs): comment_form = user_forms.MediaCommentForm(request.form) - media_template_name = media.media_manager['display_template'] + media_template_name = media.media_manager.display_template return render_to_response( request, @@ -148,19 +157,26 @@ def media_home(request, media, page, **kwargs): @get_media_entry_by_id -@require_active_login +@user_has_privilege(u'commenter') def media_post_comment(request, media): """ recieves POST from a MediaEntry() comment form, saves the comment. """ - assert request.method == 'POST' + if not request.method == 'POST': + raise MethodNotAllowed() comment = request.db.MediaComment() comment.media_entry = media.id comment.author = request.user.id comment.content = unicode(request.form['comment_content']) - if not comment.content.strip(): + # 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, @@ -172,15 +188,26 @@ def media_post_comment(request, media): request, messages.SUCCESS, _('Your comment has been posted!')) - media_uploader = media.get_uploader - #don't send email if you comment on your own post - if (comment.author != media_uploader and - media_uploader.wants_comment_notification): - send_comment_email(media_uploader, comment, media, request) + trigger_notification(comment, media, request) + + add_comment_subscription(request.user, media) - return redirect(request, location=media.url_for_self(request.urlgen)) + return redirect_obj(request, media) + +def media_preview_comment(request): + """Runs a comment through markdown so it can be previewed.""" + # If this isn't an ajax request, render_404 + if not request.is_xhr: + return render_404(request) + + comment = unicode(request.form['comment_content']) + cleancomment = { "content":cleaned_markdown_conversion(comment)} + + return Response(json.dumps(cleancomment)) + +@user_not_banned @get_media_entry_by_id @require_active_login def media_collect(request, media): @@ -255,9 +282,7 @@ def media_collect(request, media): _('"%s" added to collection "%s"') % (media.title, collection.title)) - return redirect(request, "mediagoblin.user_pages.media_home", - user=media.get_uploader.username, - media=media.slug_or_id) + return redirect_obj(request, media) #TODO: Why does @user_may_delete_media not implicate @require_active_login? @@ -271,21 +296,30 @@ def media_confirm_delete(request, media): if request.method == 'POST' and form.validate(): if form.confirm.data is True: username = media.get_uploader.username + + media.get_uploader.uploaded = media.get_uploader.uploaded - \ + media.file_size + media.get_uploader.save() + # 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) + location = media.url_to_next(request.urlgen) + if not location: + location=media.url_to_prev(request.urlgen) + if not location: + location=request.urlgen("mediagoblin.user_pages.user_home", + user=username) + return redirect(request, location=location) else: messages.add_message( request, messages.ERROR, _("The media was not deleted because you didn't check that you were sure.")) - return redirect(request, - location=media.url_for_self(request.urlgen)) + return redirect_obj(request, media) - if ((request.user.is_admin and + if ((request.user.has_privilege(u'admin') and request.user.id != media.uploader)): messages.add_message( request, messages.WARNING, @@ -298,7 +332,7 @@ def media_confirm_delete(request, media): {'media': media, 'form': form}) - +@user_not_banned @active_user_from_url @uses_pagination def user_collection(request, page, url_user=None): @@ -328,7 +362,7 @@ def user_collection(request, page, url_user=None): 'collection_items': collection_items, 'pagination': pagination}) - +@user_not_banned @active_user_from_url def collection_list(request, url_user=None): """A User-defined Collection""" @@ -355,7 +389,6 @@ def collection_item_confirm_remove(request, collection_item): if form.confirm.data is True: entry = collection_item.get_media_entry - entry.collected = entry.collected - 1 entry.save() collection_item.delete() @@ -369,11 +402,9 @@ def collection_item_confirm_remove(request, collection_item): request, messages.ERROR, _("The item was not removed because you didn't check that you were sure.")) - return redirect(request, "mediagoblin.user_pages.user_collection", - user=username, - collection=collection.slug) + return redirect_obj(request, collection) - if ((request.user.is_admin and + if ((request.user.has_privilege(u'admin') and request.user.id != collection_item.in_collection.creator)): messages.add_message( request, messages.WARNING, @@ -404,7 +435,6 @@ def collection_confirm_delete(request, collection): # Delete all the associated collection items for item in collection.get_collection_items(): entry = item.get_media_entry - entry.collected = entry.collected - 1 entry.save() item.delete() @@ -419,11 +449,9 @@ def collection_confirm_delete(request, collection): request, messages.ERROR, _("The collection was not deleted because you didn't check that you were sure.")) - return redirect(request, "mediagoblin.user_pages.user_collection", - user=username, - collection=collection.slug) + return redirect_obj(request, collection) - if ((request.user.is_admin and + if ((request.user.has_privilege(u'admin') and request.user.id != collection.creator)): messages.add_message( request, messages.WARNING, @@ -445,9 +473,8 @@ def atom_feed(request): generates the atom feed with the newest images """ user = User.query.filter_by( - username = request.matchdict['user'], - status = u'active').first() - if not user: + username = request.matchdict['user']).first() + if not user or not user.has_privilege(u'active'): return render_404(request) cursor = MediaEntry.query.filter_by( @@ -508,9 +535,8 @@ def collection_atom_feed(request): generates the atom feed with the newest images from a collection """ user = User.query.filter_by( - username = request.matchdict['user'], - status = u'active').first() - if not user: + username = request.matchdict['user']).first() + if not user or not user.has_privilege(u'active'): return render_404(request) collection = Collection.query.filter_by( @@ -528,9 +554,7 @@ def collection_atom_feed(request): ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI) """ atomlinks = [{ - 'href': request.urlgen( - 'mediagoblin.user_pages.user_collection', - qualified=True, user=request.matchdict['user'], collection=collection.slug), + 'href': collection.url_for_self(request.urlgen, qualified=True), 'rel': 'alternate', 'type': 'text/html' }] @@ -574,7 +598,6 @@ def collection_atom_feed(request): return feed.get_response() - @require_active_login def processing_panel(request): """ @@ -586,7 +609,7 @@ def processing_panel(request): # # Make sure we have permission to access this user's panel. Only # admins and this user herself should be able to do so. - if not (user.id == request.user.id or request.user.is_admin): + if not (user.id == request.user.id or request.user.has_privilege(u'admin')): # No? Simply redirect to this user's homepage. return redirect( request, 'mediagoblin.user_pages.user_home', @@ -618,3 +641,44 @@ def processing_panel(request): 'processing_entries': processing_entries, 'failed_entries': failed_entries, 'processed_entries': processed_entries}) + +@allow_reporting +@get_user_media_entry +@user_has_privilege(u'reporter') +@get_optional_media_comment_by_id +def file_a_report(request, media, comment): + """ + This view handles the filing of a MediaReport or a CommentReport. + """ + if comment is not None: + if not comment.get_media_entry.id == media.id: + return render_404(request) + + form = user_forms.CommentReportForm(request.form) + context = {'media': media, + 'comment':comment, + 'form':form} + else: + form = user_forms.MediaReportForm(request.form) + context = {'media': media, + 'form':form} + form.reporter_id.data = request.user.id + + + if request.method == "POST": + report_object = build_report_object(form, + media_entry=media, + comment=comment) + + # if the object was built successfully, report_table will not be None + if report_object: + report_object.save() + return redirect( + request, + 'index') + + + return render_to_response( + request, + 'mediagoblin/user_pages/report.html', + context) |