Skip to content

Commit

Permalink
Updated tags to use new Disqus format. Added variable mapping for bac…
Browse files Browse the repository at this point in the history
…kwards compat. Closes arthurk#75
  • Loading branch information
petersanchez committed Feb 20, 2017
1 parent 0db52c2 commit 6cc2517
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 59 deletions.
6 changes: 0 additions & 6 deletions disqus/templates/disqus/disqus_dev.html
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
{% if disqus_url %}
<script type="text/javascript">
var disqus_developer = 1;
var disqus_url = '{{ disqus_url }}';
</script>
{% endif %}
14 changes: 1 addition & 13 deletions disqus/templates/disqus/num_replies.html
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
<script type="text/javascript">
{% block config_variables %}
var disqus_shortname = '{{ shortname }}';
{{ config|safe }}
{% endblock %}
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<script id="dsq-count-scr" src="//{{ shortname }}.disqus.com/count.js" async></script>
9 changes: 1 addition & 8 deletions disqus/templates/disqus/recent_comments.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
<div id="dsq-recent-comments" class="dsq-widget">
<script type="text/javascript">
{% block config_variables %}
var disqus_shortname = '{{ shortname }}';
{{ config|safe }}
{% endblock %}
</script>
<script src='//{{ shortname }}.disqus.com/recent_comments_widget.js?num_items={{ num_items }}&hide_avatars={{ hide_avatars }}&avatar_size={{ avatar_size }}&excerpt_length={{ excerpt_length }}'>
</script>
<script src='//{{ shortname }}.disqus.com/recent_comments_widget.js?num_items={{ num_items }}&hide_avatars={{ hide_avatars }}&avatar_size={{ avatar_size }}&excerpt_length={{ excerpt_length }}'></script>
</div>
30 changes: 16 additions & 14 deletions disqus/templates/disqus/show_comments.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<div id="disqus_thread"></div>
<script type="text/javascript">
/* <![CDATA[ */
{% block config_variables %}
var disqus_shortname = '{{ shortname }}';
<script>

/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
{{ config|safe}}
{% endblock %}
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
/* ]]> */
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//{{ shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="//disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
55 changes: 37 additions & 18 deletions disqus/templatetags/disqus_tags.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,31 +17,36 @@ 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)
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.
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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=''):
"""
Expand All @@ -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):
"""
Expand All @@ -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=''):
"""
Expand Down

0 comments on commit 6cc2517

Please sign in to comment.