media_home: order comments by ascending date.
[mediagoblin.git] / mediagoblin / user_pages / views.py
index d4ff1fcedcf834547551e120d9c2602679f794df..98a21bb45a51c631515e6dfa2c2e69e6cf65bc9b 100644 (file)
@@ -1,5 +1,5 @@
 # MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 Free Software Foundation, Inc
+# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
 
 from webob import exc
 
-from mediagoblin import messages
+from mediagoblin import messages, mg_globals
 from mediagoblin.db.util import DESCENDING, ObjectId
-from mediagoblin.util import (
-    Pagination, render_to_response, redirect, cleaned_markdown_conversion)
+from mediagoblin.tools.text import cleaned_markdown_conversion
+from mediagoblin.tools.response import render_to_response, render_404, redirect
+from mediagoblin.tools.translate import pass_to_ugettext as _
+from mediagoblin.tools.pagination import Pagination
+from mediagoblin.tools.files import delete_media_files
 from mediagoblin.user_pages import forms as user_forms
 
 from mediagoblin.decorators import (uses_pagination, get_user_media_entry,
-    require_active_login)
+    require_active_login, user_may_delete_media)
 
 from werkzeug.contrib.atom import AtomFeed
 
@@ -34,7 +37,7 @@ def user_home(request, page):
     user = request.db.User.find_one({
             'username': request.matchdict['user']})
     if not user:
-        return exc.HTTPNotFound()
+        return render_404(request)
     elif user['status'] != u'active':
         return render_to_response(
             request,
@@ -42,7 +45,7 @@ def user_home(request, page):
             {'user': user})
 
     cursor = request.db.MediaEntry.find(
-        {'uploader': user['_id'],
+        {'uploader': user._id,
          'state': 'processed'}).sort('created', DESCENDING)
 
     pagination = Pagination(page, cursor)
@@ -50,8 +53,8 @@ def user_home(request, page):
 
     #if no data is available, return NotFound
     if media_entries == None:
-        return exc.HTTPNotFound()
-    
+        return render_404(request)
+
     user_gallery_url = request.urlgen(
         'mediagoblin.user_pages.user_gallery',
         user=user['username'])
@@ -64,6 +67,7 @@ def user_home(request, page):
          'media_entries': media_entries,
          'pagination': pagination})
 
+
 @uses_pagination
 def user_gallery(request, page):
     """'Gallery' of a User()"""
@@ -71,10 +75,10 @@ def user_gallery(request, page):
             'username': request.matchdict['user'],
             'status': 'active'})
     if not user:
-        return exc.HTTPNotFound()
+        return render_404(request)
 
     cursor = request.db.MediaEntry.find(
-        {'uploader': user['_id'],
+        {'uploader': user._id,
          'state': 'processed'}).sort('created', DESCENDING)
 
     pagination = Pagination(page, cursor)
@@ -82,8 +86,8 @@ def user_gallery(request, page):
 
     #if no data is available, return NotFound
     if media_entries == None:
-        return exc.HTTPNotFound()
-    
+        return render_404(request)
+
     return render_to_response(
         request,
         'mediagoblin/user_pages/gallery.html',
@@ -93,6 +97,7 @@ def user_gallery(request, page):
 
 MEDIA_COMMENTS_PER_PAGE = 50
 
+
 @get_user_media_entry
 @uses_pagination
 def media_home(request, media, page, **kwargs):
@@ -101,11 +106,11 @@ def media_home(request, media, page, **kwargs):
     """
     if ObjectId(request.matchdict.get('comment')):
         pagination = Pagination(
-            page, media.get_comments(), MEDIA_COMMENTS_PER_PAGE,
+            page, media.get_comments(True), MEDIA_COMMENTS_PER_PAGE,
             ObjectId(request.matchdict.get('comment')))
     else:
         pagination = Pagination(
-            page, media.get_comments(), MEDIA_COMMENTS_PER_PAGE)
+            page, media.get_comments(True), MEDIA_COMMENTS_PER_PAGE)
 
     comments = pagination()
 
@@ -117,34 +122,79 @@ def media_home(request, media, page, **kwargs):
         {'media': media,
          'comments': comments,
          'pagination': pagination,
-         'comment_form': comment_form})
+         'comment_form': comment_form,
+         'app_config': mg_globals.app_config})
 
 
+@get_user_media_entry
 @require_active_login
-def media_post_comment(request):
+def media_post_comment(request, media):
     """
     recieves POST from a MediaEntry() comment form, saves the comment.
     """
-    comment = request.db.MediaComment()
-    comment['media_entry'] = ObjectId(request.matchdict['media'])
-    comment['author'] = request.user['_id']
-    comment['content'] = request.POST['comment_content']
+    assert request.method == 'POST'
 
+    comment = request.db.MediaComment()
+    comment['media_entry'] = media._id
+    comment['author'] = request.user._id
+    comment['content'] = unicode(request.POST['comment_content'])
     comment['content_html'] = cleaned_markdown_conversion(comment['content'])
 
-    comment.save()
+    if not comment['content'].strip():
+        messages.add_message(
+            request,
+            messages.ERROR,
+            _("Empty comments are not allowed."))
+    else:
+        comment.save()
+
+        messages.add_message(
+            request, messages.SUCCESS,
+            _('Comment posted!'))
 
-    messages.add_message(
-        request, messages.SUCCESS,
-        'Comment posted!')
+    return exc.HTTPFound(
+        location=media.url_for_self(request.urlgen))
 
-    return redirect(request, 'mediagoblin.user_pages.media_home',
-        media = request.matchdict['media'],
-        user = request.matchdict['user'])
+
+@get_user_media_entry
+@require_active_login
+@user_may_delete_media
+def media_confirm_delete(request, media):
+
+    form = user_forms.ConfirmDeleteForm(request.POST)
+
+    if request.method == 'POST' and form.validate():
+        if form.confirm.data is True:
+            username = media.uploader()['username']
+
+            # Delete all files on the public storage
+            delete_media_files(media)
+
+            media.delete()
+
+            return redirect(request, "mediagoblin.user_pages.user_home",
+                user=username)
+        else:
+            return exc.HTTPFound(
+                location=media.url_for_self(request.urlgen))
+
+    if ((request.user[u'is_admin'] and
+         request.user._id != media.uploader()._id)):
+        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})
 
 
 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
 
+
 def atom_feed(request):
     """
     generates the atom feed with the newest images
@@ -154,10 +204,10 @@ def atom_feed(request):
                'username': request.matchdict['user'],
                'status': 'active'})
     if not user:
-        return exc.HTTPNotFound()
+        return render_404(request)
 
     cursor = request.db.MediaEntry.find({
-                 'uploader': user['_id'],
+                 'uploader': user._id,
                  'state': 'processed'}) \
                  .sort('created', DESCENDING) \
                  .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
@@ -165,7 +215,7 @@ def atom_feed(request):
     feed = AtomFeed(request.matchdict['user'],
                feed_url=request.url,
                url=request.host_url)
-    
+
     for entry in cursor:
         feed.add(entry.get('title'),
             entry.get('description_html'),
@@ -190,7 +240,7 @@ def processing_panel(request):
 
     # Make sure the user exists and is active
     if not user:
-        return exc.HTTPNotFound()
+        return render_404(request)
     elif user['status'] != u'active':
         return render_to_response(
             request,
@@ -201,7 +251,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[u'_id'] == request.user[u'_id']
+    if not (user._id == request.user._id
             or request.user.is_admin):
         # No?  Let's simply redirect to this user's homepage then.
         return redirect(
@@ -210,12 +260,12 @@ def processing_panel(request):
 
     # Get media entries which are in-processing
     processing_entries = request.db.MediaEntry.find(
-        {'uploader': user['_id'],
+        {'uploader': user._id,
          'state': 'processing'}).sort('created', DESCENDING)
 
     # Get media entries which have failed to process
     failed_entries = request.db.MediaEntry.find(
-        {'uploader': user['_id'],
+        {'uploader': user._id,
          'state': 'failed'}).sort('created', DESCENDING)
 
     # Render to response