Commits

John Groszko  committed ea00bda

In the spirit of DRY, a comment template can be used to render the comment
server side, instead of generating HTML inside javascript (gross).

  • Participants
  • Parent commits fcd5f3f

Comments (0)

Files changed (3)

File ajaxcomments/media/js/post-comment.js

     name = $('#id_name').val();
     url = $('#id_url').val();
     
-    // Create an MD5 hash from the email address to use with Gravatar
-    gravatar = 'http://www.gravatar.com/avatar.php' +
-        '?default=&size=48&gravatar_id=' + $.md5(email);
-    
     if ($('div#comments').children().length == 0) {
         $('div#comments').prepend(
             '<h2 class="comment-hd">1 comment so far:</h2>'
         )
     }
     
-    comment_html = '\
-        <div class="comment" style="display: none;">\
-            <div class="comment-body">\
-                <a href="http://www.gravatar.com">\
-                    <img src="' + gravatar + '" /></a>\
-                <p>' + comment + '</p>\
-            </div>\
-        <div class="clear"></div>\
-        <p class="posted-by">Posted by <a href="' + url + '">' + 
-            name + '</a> 0 minutes ago.</p></div>\
-    ';
-    
+   $('div.comment-form form textarea')[0].value = "";
+
     $('#id_comment').val('');
     
-    $('#comments').append(comment_html);
+    $('#comments').append(data['html']);
     $('div.comment:last').show('slow');
     
     $('p.submit').after('\
     // Remove the wait animation and message
     $('.ajax-loader').remove();
     $('div.comment-waiting').stop().remove();
-}
+}

File ajaxcomments/templates/comments/comment.html

+{% load i18n %}
+{% load avatar_tags %}
+
+<div class="comment">
+  <div class="comment-body">
+    <div class="avatar">{% avatar comment.user %}</div>
+    <p>comment.comment</p>
+  </div>
+  <div class="clear"></div>
+  <p class="posted-by">Posted by <a href="{% url profile_detail comment.user
+  %}">{{comment.user}}</a> {{ comment.submit_date|timesince }} ago</div>
+</div>

File ajaxcomments/utils.py

 from django.utils import simplejson
 from django import http
+from django.template.loader import render_to_string
 from django.contrib.comments.views import comments
+from django.contrib.comments.models import Comment
 from django.shortcuts import render_to_response
 from django.contrib.comments.views.utils import next_redirect
 
                     success = False
             except TypeError:
                 pass
-    
+            
             json_response = simplejson.dumps({
                 'success': success,
                 'errors': json_errors,
+                'html': render_to_string('comments/comment.html',
+                                         {'comment': Comment.objects.get(id=kwargs['c']) }),
             })
     
             return http.HttpResponse(json_response, mimetype="application/json")