X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=mediagoblin%2Fuser_pages%2Fviews.py;h=596d4c20b6111c1496f49ec3b576edffe7e889d0;hb=58a947578cde02244c08268205e6855bee408c94;hp=8602c5838e97278f12f89fcafcfe643829064b8b;hpb=19ad2e0cd0ff2e03cdf1243a9cedaecd4a187592;p=mediagoblin.git diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index 8602c583..596d4c20 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -18,13 +18,16 @@ import logging import datetime from mediagoblin import messages, mg_globals -from mediagoblin.db.models import (MediaEntry, Collection, CollectionItem, - User) -from mediagoblin.tools.response import render_to_response, render_404, redirect +from mediagoblin.db.models import (MediaEntry, MediaTag, Collection, + CollectionItem, User) +from mediagoblin.tools.response import render_to_response, render_404, \ + redirect, redirect_obj 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 +from mediagoblin.user_pages.lib import add_media_to_collection +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, @@ -32,6 +35,7 @@ from mediagoblin.decorators import (uses_pagination, get_user_media_entry, get_user_collection, get_user_collection_item, active_user_from_url) from werkzeug.contrib.atom import AtomFeed +from werkzeug.exceptions import MethodNotAllowed _log = logging.getLogger(__name__) @@ -81,10 +85,17 @@ def user_home(request, page): @uses_pagination def user_gallery(request, page, url_user=None): """'Gallery' of a User()""" + tag = request.matchdict.get('tag', None) cursor = MediaEntry.query.filter_by( uploader=url_user.id, state=u'processed').order_by(MediaEntry.created.desc()) + # Filter potentially by tag too: + if tag: + cursor = cursor.filter( + MediaEntry.tags_helper.any( + MediaTag.slug == request.matchdict['tag'])) + # Paginate gallery pagination = Pagination(page, cursor) media_entries = pagination() @@ -97,10 +108,11 @@ def user_gallery(request, page, url_user=None): return render_to_response( request, 'mediagoblin/user_pages/gallery.html', - {'user': url_user, + {'user': url_user, 'tag': tag, 'media_entries': media_entries, 'pagination': pagination}) + MEDIA_COMMENTS_PER_PAGE = 50 @@ -112,6 +124,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']), @@ -127,7 +142,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, @@ -145,14 +160,21 @@ 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, @@ -164,16 +186,14 @@ 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) - return redirect(request, location=media.url_for_self(request.urlgen)) + add_comment_subscription(request.user, media) + return redirect_obj(request, media) -@get_user_media_entry + +@get_media_entry_by_id @require_active_login def media_collect(request, media): """Add media to collection submission""" @@ -197,30 +217,31 @@ def media_collect(request, media): # If we are here, method=POST and the form is valid, submit things. # If the user is adding a new collection, use that: - if request.form['collection_title']: + 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=request.form['collection_title']).first() + title=form.collection_title.data).first() if existing_collection: messages.add_message(request, messages.ERROR, - _('You already have a collection called "%s"!' - % collection.title)) + _('You already have a collection called "%s"!') + % existing_collection.title) return redirect(request, "mediagoblin.user_pages.media_home", - user=request.user.username, - media=media.id) + user=media.get_uploader.username, + media=media.slug_or_id) collection = Collection() - collection.title = request.form['collection_title'] - collection.description = request.form.get('collection_description') + 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 = Collection.query.filter_by( - id=request.form.get('collection')).first() + 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: @@ -229,7 +250,7 @@ def media_collect(request, media): _('You have to select or add a collection')) return redirect(request, "mediagoblin.user_pages.media_collect", user=media.get_uploader.username, - media=media.id) + media_id=media.id) # Check whether media already exists in collection @@ -237,29 +258,16 @@ def media_collect(request, media): media_entry=media.id, collection=collection.id).first(): messages.add_message(request, messages.ERROR, - _('"%s" already in collection "%s"' - % (media.title, collection.title))) + _('"%s" already in collection "%s"') + % (media.title, collection.title)) else: # Add item to collection - collection_item = request.db.CollectionItem() - collection_item.collection = collection.id - collection_item.media_entry = media.id - collection_item.author = request.user.id - collection_item.note = request.form['note'] - collection_item.save() - - collection.items = collection.items + 1 - collection.save() - - media.collected = media.collected + 1 - media.save() + add_media_to_collection(collection, media, form.note.data) messages.add_message(request, messages.SUCCESS, - _('"%s" added to collection "%s"' - % (media.title, collection.title))) + _('"%s" added to collection "%s"') + % (media.title, collection.title)) - return redirect(request, "mediagoblin.user_pages.media_home", - user=media.get_uploader.username, - media=media.id) + return redirect_obj(request, media) #TODO: Why does @user_may_delete_media not implicate @require_active_login? @@ -284,8 +292,7 @@ def media_confirm_delete(request, media): 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 request.user.id != media.uploader)): @@ -309,6 +316,9 @@ def user_collection(request, page, url_user=None): 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) @@ -368,9 +378,7 @@ 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 request.user.id != collection_item.in_collection.creator)): @@ -408,8 +416,8 @@ def collection_confirm_delete(request, collection): item.delete() collection.delete() - messages.add_message( - request, messages.SUCCESS, _('You deleted the collection "%s"' % collection_title)) + messages.add_message(request, messages.SUCCESS, + _('You deleted the collection "%s"') % collection_title) return redirect(request, "mediagoblin.user_pages.user_home", user=username) @@ -418,9 +426,7 @@ 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 request.user.id != collection.creator)): @@ -515,6 +521,8 @@ def collection_atom_feed(request): collection = Collection.query.filter_by( creator=user.id, slug=request.matchdict['collection']).first() + if not collection: + return render_404(request) cursor = CollectionItem.query.filter_by( collection=collection.id) \ @@ -525,9 +533,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' }] @@ -542,7 +548,7 @@ def collection_atom_feed(request): "MediaGoblin: Feed for %s's collection %s" % (request.matchdict['user'], collection.title), feed_url=request.url, - id=u'tag:{user}@{host},{year}:collection.slug-{slug}'\ + id=u'tag:{host},{year}:gnu-mediagoblin.{user}.collection.{slug}'\ .format( host=request.host, year=collection.created.strftime('%Y'),