From 0a8247a41b8bfa7f14473f49fa0196fd78ca2fb3 Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Thu, 6 Jun 2013 07:57:32 +0200 Subject: [PATCH 1/7] Added js controls logic which is backwards compatible but allows is_reversed to be specified --- README.rst | 18 ++++++++++++++++++ .../static/fluent_comments/js/ajaxcomments.js | 14 +++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index e6cdbf7..b3ba345 100644 --- a/README.rst +++ b/README.rst @@ -172,6 +172,24 @@ The templates and admin interface adapt themselves automatically to show the threaded comments. +Javascript Controls +------------------- + +You are able to control various output settings via javascript. + +1. is_reversed: Will use prepend instead of append to insert the ajax created comment + + +Insert the following code, that does not wait for document.ready + +``` + +``` + Contributing ------------ diff --git a/fluent_comments/static/fluent_comments/js/ajaxcomments.js b/fluent_comments/static/fluent_comments/js/ajaxcomments.js index 12def5a..2e6004f 100644 --- a/fluent_comments/static/fluent_comments/js/ajaxcomments.js +++ b/fluent_comments/static/fluent_comments/js/ajaxcomments.js @@ -7,9 +7,13 @@ var COMMENT_SCROLL_TOP_OFFSET = 40; var PREVIEW_SCROLL_TOP_OFFSET = 20; - $.fn.ready(function() { + + var COMMENT_CONTROLS = (window.COMMENT_CONTROLS !== undefined) ? window.COMMENT_CONTROLS : { + 'is_reversed': false, + }; + var commentform = $('form.js-comments-form'); if( commentform.length > 0 ) { @@ -226,18 +230,22 @@ var parent_id = data['parent_id']; var $new_comment; + + // define the action by which the comment is inserted at the top of the list or the bottom + var insert_action = (COMMENT_CONTROLS.is_reversed === true) ? 'prepend' : 'append' ; + if(parent_id) { var $parentLi = $("#c" + parseInt(parent_id)).parent('li.comment-wrapper'); var $commentUl = $parentLi.children('ul'); if( $commentUl.length == 0 ) $commentUl = $parentLi.append('').children('ul.comment-list-wrapper'); - $commentUl.append('
  • ' + html + '
  • '); + $commentUl[insert_action]('
  • ' + html + '
  • '); } else { var $comments = getCommentsDiv(); - $comments.append(html).removeClass('empty'); + $comments[insert_action](html).removeClass('empty'); } return $("#c" + parseInt(data.comment_id)); From 840c469be7b99ce02813339309fb2001e8aff9f9 Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Fri, 7 Jun 2013 17:47:29 +0200 Subject: [PATCH 2/7] Added revere tag and optimised sql to select_related and stop massive queries --- .../static/fluent_comments/js/ajaxcomments.js | 6 +-- .../templatetags/fluent_comments_tags.py | 46 ++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/fluent_comments/static/fluent_comments/js/ajaxcomments.js b/fluent_comments/static/fluent_comments/js/ajaxcomments.js index 2e6004f..12abf36 100644 --- a/fluent_comments/static/fluent_comments/js/ajaxcomments.js +++ b/fluent_comments/static/fluent_comments/js/ajaxcomments.js @@ -226,7 +226,7 @@ function addComment(data) { // data contains the server-side response. - var html = data['html'] + var html = $(data['html']) // create the domElement and thus fire appropriate events var parent_id = data['parent_id']; var $new_comment; @@ -240,12 +240,12 @@ var $commentUl = $parentLi.children('ul'); if( $commentUl.length == 0 ) $commentUl = $parentLi.append('').children('ul.comment-list-wrapper'); - $commentUl[insert_action]('
  • ' + html + '
  • '); + $commentUl[insert_action]('
  • ' + html.html() + '
  • '); } else { var $comments = getCommentsDiv(); - $comments[insert_action](html).removeClass('empty'); + $comments[insert_action](html.html()).removeClass('empty'); } return $("#c" + parseInt(data.comment_id)); diff --git a/fluent_comments/templatetags/fluent_comments_tags.py b/fluent_comments/templatetags/fluent_comments_tags.py index 8251ea8..b3cc624 100644 --- a/fluent_comments/templatetags/fluent_comments_tags.py +++ b/fluent_comments/templatetags/fluent_comments_tags.py @@ -1,11 +1,13 @@ from django.conf import settings from django.template import Library from django.core import context_processors -from django.template.loader import get_template +from django.template.loader import get_template, render_to_string +from django.contrib.comments.templatetags.comments import RenderCommentListNode from fluent_comments import appsettings from fluent_comments.models import get_comments_for_model from fluent_comments.moderation import comments_are_open, comments_are_moderated + register = Library() @register.inclusion_tag("fluent_comments/templatetags/ajax_comment_tags.html", takes_context=True) @@ -56,3 +58,45 @@ def fluent_comments_list(context): context['USE_THREADEDCOMMENTS'] = appsettings.USE_THREADEDCOMMENTS return template.render(context) + + +class RenderCommentListReversedNode(RenderCommentListNode): + """Render the comment list directly in reverse """ + + def render(self, context): + ctype, object_pk = self.get_target_ctype_pk(context) + if object_pk: + template_search_list = [ + "comments/%s/%s/list.html" % (ctype.app_label, ctype.model), + "comments/%s/list.html" % ctype.app_label, + "comments/list.html" + ] + qs = self.get_query_set(context).select_related('user').order_by('-id') + context.push() + liststr = render_to_string(template_search_list, { + "comment_list" : self.get_context_value_from_queryset(context, qs) + }, context) + context.pop() + return liststr + else: + return '' + +@register.tag +def render_comment_list_reversed(parser, token): + """ + Render the comment list (as returned by ``{% get_comment_list %}``) + through the ``comments/list.html`` template + + but in reverse order + + Syntax:: + + {% render_comment_list_reversed for [object] %} + {% render_comment_list_reversed for [app].[model] [object_id] %} + + Example usage:: + + {% render_comment_list_reversed for event %} + + """ + return RenderCommentListReversedNode.handle_token(parser, token) \ No newline at end of file From d4c327110a9cb82bfac86a4bef159f8a57fb37e5 Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Sun, 9 Jun 2013 12:34:28 +0200 Subject: [PATCH 3/7] Added reverse template tag and added to comments --- fluent_comments/static/fluent_comments/js/ajaxcomments.js | 5 +++-- fluent_comments/templatetags/fluent_comments_tags.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/fluent_comments/static/fluent_comments/js/ajaxcomments.js b/fluent_comments/static/fluent_comments/js/ajaxcomments.js index 12abf36..89e16ce 100644 --- a/fluent_comments/static/fluent_comments/js/ajaxcomments.js +++ b/fluent_comments/static/fluent_comments/js/ajaxcomments.js @@ -12,6 +12,7 @@ var COMMENT_CONTROLS = (window.COMMENT_CONTROLS !== undefined) ? window.COMMENT_CONTROLS : { 'is_reversed': false, + 'scroll_to_comment': true, }; var commentform = $('form.js-comments-form'); @@ -240,12 +241,12 @@ var $commentUl = $parentLi.children('ul'); if( $commentUl.length == 0 ) $commentUl = $parentLi.append('
      ').children('ul.comment-list-wrapper'); - $commentUl[insert_action]('
    • ' + html.html() + '
    • '); + $commentUl[insert_action]('
    • ' + html.prop('outerHTML') + '
    • '); } else { var $comments = getCommentsDiv(); - $comments[insert_action](html.html()).removeClass('empty'); + $comments[insert_action](html.prop('outerHTML')).removeClass('empty'); } return $("#c" + parseInt(data.comment_id)); diff --git a/fluent_comments/templatetags/fluent_comments_tags.py b/fluent_comments/templatetags/fluent_comments_tags.py index b3cc624..1fd8eaf 100644 --- a/fluent_comments/templatetags/fluent_comments_tags.py +++ b/fluent_comments/templatetags/fluent_comments_tags.py @@ -71,7 +71,7 @@ def render(self, context): "comments/%s/list.html" % ctype.app_label, "comments/list.html" ] - qs = self.get_query_set(context).select_related('user').order_by('-id') + qs = self.get_query_set(context).prefetch_related('user').order_by('-id') context.push() liststr = render_to_string(template_search_list, { "comment_list" : self.get_context_value_from_queryset(context, qs) From a005c85437b92117c06f410f36eec5ceff77e462 Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Sun, 9 Jun 2013 12:45:53 +0200 Subject: [PATCH 4/7] added scrolls_to_comment control --- fluent_comments/static/fluent_comments/js/ajaxcomments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fluent_comments/static/fluent_comments/js/ajaxcomments.js b/fluent_comments/static/fluent_comments/js/ajaxcomments.js index 89e16ce..f9e1e56 100644 --- a/fluent_comments/static/fluent_comments/js/ajaxcomments.js +++ b/fluent_comments/static/fluent_comments/js/ajaxcomments.js @@ -98,7 +98,7 @@ function scrollToElement( $element, speed, offset ) { - if( $element.length ) + if( $element.length && COMMENT_CONTROLS.scroll_to_comment === true ) $(scrollElement).animate( {scrollTop: $element.offset().top - (offset || 0) }, speed || 1000 ); } From e69bb107223c218b5b08732d005acc7829a9dd1a Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Mon, 10 Jun 2013 06:34:37 +0200 Subject: [PATCH 5/7] coercing context object to unicode so emails dont fail being sent --- fluent_comments/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fluent_comments/models.py b/fluent_comments/models.py index 38fad31..ff661de 100644 --- a/fluent_comments/models.py +++ b/fluent_comments/models.py @@ -20,6 +20,8 @@ def on_comment_posted(sender, comment, request, **kwargs): # # Instead of implementing this feature in the moderator class, the signal is used instead # so the notification feature works regardless of a manual moderator.register() call in the project. + import pdb + pdb.set_trace() if not appsettings.FLUENT_COMMENTS_USE_EMAIL_NOTIFICATION: return @@ -34,8 +36,8 @@ def on_comment_posted(sender, comment, request, **kwargs): 'content_object': content_object } - message = render(request, "comments/comment_notification_email.txt", context) - send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True) + message = unicode(render(request, "comments/comment_notification_email.txt", context)) + send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=False) def get_comments_for_model(content_object, include_moderated=False): From 8a828dfaa2679b8846240e094bb51e7e09b66f66 Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Mon, 10 Jun 2013 06:37:25 +0200 Subject: [PATCH 6/7] coercing context object to unicode so emails dont fail being sent --- fluent_comments/models.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/fluent_comments/models.py b/fluent_comments/models.py index ff661de..95c19e4 100644 --- a/fluent_comments/models.py +++ b/fluent_comments/models.py @@ -20,8 +20,6 @@ def on_comment_posted(sender, comment, request, **kwargs): # # Instead of implementing this feature in the moderator class, the signal is used instead # so the notification feature works regardless of a manual moderator.register() call in the project. - import pdb - pdb.set_trace() if not appsettings.FLUENT_COMMENTS_USE_EMAIL_NOTIFICATION: return From 6a4eeb435c6d2a06970952eb16ea8b0f9262c360 Mon Sep 17 00:00:00 2001 From: Ross Crawford-d'Heureuse Date: Tue, 24 Sep 2013 09:07:52 +0200 Subject: [PATCH 7/7] Fixed error thrown when deleted objects exist --- fluent_comments/admin.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fluent_comments/admin.py b/fluent_comments/admin.py index 9460d5d..547f667 100644 --- a/fluent_comments/admin.py +++ b/fluent_comments/admin.py @@ -55,8 +55,11 @@ class FluentCommentsAdmin(CommentsAdminBase): def object_link(self, comment): object = comment.content_object - title = unicode(object) - return u'{1}'.format(escape(object.get_absolute_url()), escape(title)) + if object is not None and hasattr(object, 'get_absolute_url'): + title = unicode(object) + return u'{1}'.format(escape(object.get_absolute_url()), escape(title)) + else: + return u'{1}'.format('#', 'Comment Content_Object does not exist') object_link.short_description = _("Page") object_link.allow_tags = True