From 6cc2517348d22e78a00e815c5e234a65035ee5e8 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Mon, 20 Feb 2017 12:58:14 -0800 Subject: [PATCH] Updated tags to use new Disqus format. Added variable mapping for backwards compat. Closes #75 --- disqus/templates/disqus/disqus_dev.html | 6 --- disqus/templates/disqus/num_replies.html | 14 +---- disqus/templates/disqus/recent_comments.html | 9 +--- disqus/templates/disqus/show_comments.html | 30 ++++++----- disqus/templatetags/disqus_tags.py | 55 +++++++++++++------- 5 files changed, 55 insertions(+), 59 deletions(-) diff --git a/disqus/templates/disqus/disqus_dev.html b/disqus/templates/disqus/disqus_dev.html index d531145..e69de29 100644 --- a/disqus/templates/disqus/disqus_dev.html +++ b/disqus/templates/disqus/disqus_dev.html @@ -1,6 +0,0 @@ -{% if disqus_url %} - -{% endif %} diff --git a/disqus/templates/disqus/num_replies.html b/disqus/templates/disqus/num_replies.html index e363eaf..2c9806d 100644 --- a/disqus/templates/disqus/num_replies.html +++ b/disqus/templates/disqus/num_replies.html @@ -1,13 +1 @@ - + diff --git a/disqus/templates/disqus/recent_comments.html b/disqus/templates/disqus/recent_comments.html index cd9bd34..fba6ad6 100644 --- a/disqus/templates/disqus/recent_comments.html +++ b/disqus/templates/disqus/recent_comments.html @@ -1,10 +1,3 @@
- - +
diff --git a/disqus/templates/disqus/show_comments.html b/disqus/templates/disqus/show_comments.html index d91fd4c..e013a0e 100644 --- a/disqus/templates/disqus/show_comments.html +++ b/disqus/templates/disqus/show_comments.html @@ -1,17 +1,19 @@
- - -blog comments powered by Disqus + diff --git a/disqus/templatetags/disqus_tags.py b/disqus/templatetags/disqus_tags.py index 1c63f05..9906fa3 100644 --- a/disqus/templatetags/disqus_tags.py +++ b/disqus/templatetags/disqus_tags.py @@ -1,8 +1,8 @@ -import base64 -import hashlib import hmac import json import time +import base64 +import hashlib from django import template from django.conf import settings @@ -17,24 +17,28 @@ def set_disqus_developer(context, disqus_developer): context['disqus_developer'] = disqus_developer return "" + # Set the disqus_identifier variable to some unique value. Defaults to page's URL @register.simple_tag(takes_context=True) def set_disqus_identifier(context, *args): context['disqus_identifier'] = "".join(args) return "" + # Set the disqus_url variable to some value. Defaults to page's location @register.simple_tag(takes_context=True) def set_disqus_url(context, *args): context['disqus_url'] = "".join(args) return "" + # Set the disqus_title variable to some value. Defaults to page's title or URL @register.simple_tag(takes_context=True) def set_disqus_title(context, disqus_title): context['disqus_title'] = disqus_title return "" + # Set the disqus_category_id variable to some value. No default. See # http://help.disqus.com/customer/portal/articles/472098-javascript-configuration-variables#disqus_category_id @register.simple_tag(takes_context=True) @@ -42,6 +46,7 @@ def set_disqus_category_id(context, disqus_category_id): context['disqus_category_id'] = disqus_category_id return "" + def get_config(context): """ Return the formatted javascript for any disqus config variables. @@ -54,30 +59,41 @@ def get_config(context): 'disqus_category_id' ] - js = '\tvar {} = "{}";' - - output = [js.format(item, context[item]) for item in conf_vars \ - if item in context] - - return '\n'.join(output) + conf_map = { + 'disqus_identifier': 'this.page.identifier', + 'disqus_url': 'this.page.url', + 'disqus_title': 'this.page.title', + 'disqus_category_id': 'this.page.category_id', + } -@register.inclusion_tag('disqus/disqus_dev.html', takes_context=True) -def disqus_dev(context): - """ - Return the HTML/js code to enable DISQUS comments on a local - development server if settings.DEBUG is True. - """ + js = '{0} = "{1}";' if settings.DEBUG: - disqus_url = '//{}{}'.format( + # Add these default settings if not set. Useful for + # local dev. + disqus_url = '{0}://{1}{2}'.format( + 'https' if context['request'].is_secure() else 'http', Site.objects.get_current().domain, context['request'].path ) + for x in ['disqus_url', 'disqus_identifier']: + if x not in context: + context[x] = disqus_url - return {'disqus_url': disqus_url} + output = [js.format(conf_map[item], context[item]) for item in conf_vars \ + if item in context and item in conf_map] + return '\n'.join(output) + + +@register.inclusion_tag('disqus/disqus_dev.html', takes_context=True) +def disqus_dev(context): + """ + No longer supported by Disqus + """ return {} + @register.inclusion_tag('disqus/disqus_sso.html', takes_context=True) def disqus_sso(context): """ @@ -112,19 +128,20 @@ def disqus_sso(context): timestamp = int(time.time()) key = DISQUS_SECRET_KEY.encode('utf-8') - msg = ('%s %s' % (message, timestamp)).encode('utf-8') + msg = ('{0} {1}'.format(message, timestamp)).encode('utf-8') digestmod = hashlib.sha1 # generate our hmac signature sig = hmac.HMAC(key, msg, digestmod).hexdigest() - return dict( + return dict( message=message, timestamp=timestamp, sig=sig, pub_key=DISQUS_PUBLIC_KEY, ) + @register.inclusion_tag('disqus/num_replies.html', takes_context=True) def disqus_num_replies(context, shortname=''): """ @@ -138,6 +155,7 @@ def disqus_num_replies(context, shortname=''): 'config': get_config(context), } + @register.inclusion_tag('disqus/recent_comments.html', takes_context=True) def disqus_recent_comments(context, shortname='', num_items=5, excerpt_length=200, hide_avatars=0, avatar_size=32): """ @@ -154,6 +172,7 @@ def disqus_recent_comments(context, shortname='', num_items=5, excerpt_length=20 'config': get_config(context), } + @register.inclusion_tag('disqus/show_comments.html', takes_context=True) def disqus_show_comments(context, shortname=''): """