Merge branch 'master' of git://gitorious.org/mediagoblin/mediagoblin
[mediagoblin.git] / mediagoblin / user_pages / views.py
index 738cc054f51b594d2c48d96a4d276fd89f486e13..e5646faaed807835cd1bbd08fb62ae6d28d1c7ec 100644 (file)
 
 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, 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)
-
+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,
     require_active_login, user_may_delete_media, user_may_alter_collection,
     get_user_collection, get_user_collection_item, active_user_from_url)
 
 from werkzeug.contrib.atom import AtomFeed
+from werkzeug.exceptions import MethodNotAllowed
+from werkzeug.wrappers import Response
 
 
 _log = logging.getLogger(__name__)
@@ -110,6 +114,7 @@ def user_gallery(request, page, url_user=None):
          'media_entries': media_entries,
          'pagination': pagination})
 
+
 MEDIA_COMMENTS_PER_PAGE = 50
 
 
@@ -121,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']),
@@ -136,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,
@@ -154,11 +162,13 @@ 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
+    print request.form['comment_content']
     comment.content = unicode(request.form['comment_content'])
 
     # Show error message if commenting is disabled.
@@ -179,15 +189,25 @@ 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_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))
+
 @get_media_entry_by_id
 @require_active_login
 def media_collect(request, media):
@@ -266,11 +286,29 @@ def media_collect(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):
 
+@require_active_login
+def media_confirm_delete(request):
+    
+    allowed_state = [u'failed', u'processed']
+    media = None
+    for media_state in allowed_state:
+        media = request.db.MediaEntry.query.filter_by(id=request.matchdict['media_id'], state=media_state).first()
+        if media:
+            break
+    
+    if not media:
+        return render_404(request)
+    
+    given_username = request.matchdict.get('user')
+    if given_username and (given_username != media.get_uploader.username):
+        return render_404(request)
+    
+    uploader_id = media.uploader
+    if not (request.user.is_admin or
+            request.user.id == uploader_id):
+        raise Forbidden()
+    
     form = user_forms.ConfirmDeleteForm(request.form)
 
     if request.method == 'POST' and form.validate():
@@ -281,8 +319,13 @@ def media_confirm_delete(request, media):
             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,