Issue #362 - Updated `mediagoblin.user_pages.views` to handle new "Simple comments...
authorJoar Wandborg <git@wandborg.com>
Tue, 28 Jun 2011 23:29:39 +0000 (01:29 +0200)
committerJoar Wandborg <git@wandborg.com>
Tue, 28 Jun 2011 23:29:39 +0000 (01:29 +0200)
*   ADDED `media_post_comment(request)` which creates `MediaComments`
    from POST requests
*   UPDATED `media_home(request, media, **kwargs)` now passes `comments`,
    `pagination` and `comments_form` to `media.html`

mediagoblin/user_pages/views.py

index d6cd6034a8865402a59b56529247a8cad7bad74c..67a57d9750a18a59bc40fadcfa5a5c0df624be90 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from webob import exc
-from mediagoblin.db.util import DESCENDING
-from mediagoblin.util import Pagination, render_to_response
+from mediagoblin.db.util import DESCENDING, ObjectId
+from mediagoblin.util import Pagination, render_to_response, redirect, \
+    clean_html
+from mediagoblin.user_pages import forms as user_forms
 
-from mediagoblin.decorators import uses_pagination, get_user_media_entry
+from mediagoblin.decorators import uses_pagination, get_user_media_entry, \
+    require_active_login
 
 from werkzeug.contrib.atom import AtomFeed
 
+import markdown
+
 @uses_pagination
 def user_home(request, page):
     """'Homepage' of a User()"""
@@ -78,13 +83,45 @@ def user_gallery(request, page):
 
 
 @get_user_media_entry
-def media_home(request, media):
-    """'Homepage' of a MediaEntry()"""
+@uses_pagination
+def media_home(request, media, **kwargs):
+    """
+    'Homepage' of a MediaEntry()
+    """
+
+    comment_form = user_forms.MediaCommentForm(request.POST)
+
+    (comments, pagination) = media.get_comments(kwargs.get('page'))
+
     return render_to_response(
         request,
         'mediagoblin/user_pages/media.html',
-        {'media': media})
+        {'media': media,
+         'comments': comments,
+         'pagination': pagination,
+         'comment_form': comment_form})
+
+@require_active_login
+def media_post_comment(request):
+    """
+    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']
+
+    md = markdown.Markdown(
+        safe_mode = 'escape')
+    comment['content_html'] = clean_html(
+        md.convert(
+            comment['content']))
+
+    comment.save()
 
+    return redirect(request, 'mediagoblin.user_pages.media_home',
+        media = request.matchdict['media'],
+        user = request.matchdict['user'])
 
 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 5
 
@@ -117,4 +154,4 @@ def atom_feed(request):
             updated=entry.get('created'),
             url=entry.url_for_self(request.urlgen))
 
-    return feed.get_response() 
+    return feed.get_response()