<s> import base64 <EOL> import hashlib <EOL> import hmac <EOL> import json <EOL> import time <EOL> from django import template <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import Site <EOL> register = template . Library ( ) <EOL> @ register . simple_tag ( takes_context = True ) <EOL> def set_disqus_developer ( context , disqus_developer ) : <EOL> context [ '<STR_LIT>' ] = disqus_developer <EOL> return "<STR_LIT>" <EOL> @ register . simple_tag ( takes_context = True ) <EOL> def set_disqus_identifier ( context , * args ) : <EOL> context [ '<STR_LIT>' ] = "<STR_LIT>" . join ( args ) <EOL> return "<STR_LIT>" <EOL> @ register . simple_tag ( takes_context = True ) <EOL> def set_disqus_url ( context , * args ) : <EOL> context [ '<STR_LIT>' ] = "<STR_LIT>" . join ( args ) <EOL> return "<STR_LIT>" <EOL> @ register . simple_tag ( takes_context = True ) <EOL> def set_disqus_title ( context , disqus_title ) : <EOL> context [ '<STR_LIT>' ] = disqus_title <EOL> return "<STR_LIT>" <EOL> @ register . simple_tag ( takes_context = True ) <EOL> def set_disqus_category_id ( context , disqus_category_id ) : <EOL> context [ '<STR_LIT>' ] = disqus_category_id <EOL> return "<STR_LIT>" <EOL> def get_config ( context ) : <EOL> """<STR_LIT>""" <EOL> conf_vars = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> js = '<STR_LIT>' <EOL> output = [ js . format ( item , context [ item ] ) for item in conf_vars if item in context ] <EOL> return '<STR_LIT:\n>' . join ( output ) <EOL> @ register . inclusion_tag ( '<STR_LIT>' , takes_context = True ) <EOL> def disqus_dev ( context ) : <EOL> """<STR_LIT>""" <EOL> if settings . DEBUG : <EOL> disqus_url = '<STR_LIT>' . format ( <EOL> Site . objects . get_current ( ) . domain , <EOL> context [ '<STR_LIT>' ] . path <EOL> ) <EOL> return { '<STR_LIT>' : disqus_url } <EOL> return { } <EOL> @ register . inclusion_tag ( '<STR_LIT>' , takes_context = True ) <EOL> def disqus_sso ( context ) : <EOL> """<STR_LIT>""" <EOL> DISQUS_SECRET_KEY = getattr ( settings , '<STR_LIT>' , None ) <EOL> if DISQUS_SECRET_KEY is None : <EOL> return "<STR_LIT>" <EOL> DISQUS_PUBLIC_KEY = getattr ( settings , '<STR_LIT>' , None ) <EOL> if DISQUS_PUBLIC_KEY is None : <EOL> return "<STR_LIT>" <EOL> user = context [ '<STR_LIT:user>' ] <EOL> if user . is_anonymous ( ) : <EOL> return "<STR_LIT>" <EOL> data = json . dumps ( { <EOL> '<STR_LIT:id>' : user . id , <EOL> '<STR_LIT:username>' : user . username , <EOL> '<STR_LIT:email>' : user . email , <EOL> } ) <EOL> message = base64 . b64encode ( data . encode ( '<STR_LIT:utf-8>' ) ) <EOL> timestamp = int ( time . time ( ) ) <EOL> key = DISQUS_SECRET_KEY . encode ( '<STR_LIT:utf-8>' ) <EOL> msg = ( '<STR_LIT>' % ( message , timestamp ) ) . encode ( '<STR_LIT:utf-8>' ) <EOL> digestmod = hashlib . sha1 <EOL> sig = hmac . HMAC ( key , msg , digestmod ) . hexdigest ( ) <EOL> return dict ( <EOL> message = message , <EOL> timestamp = timestamp , <EOL> sig = sig , <EOL> pub_key = DISQUS_PUBLIC_KEY , <EOL> ) <EOL> @ register . inclusion_tag ( '<STR_LIT>' , takes_context = True ) <EOL> def disqus_num_replies ( context , shortname = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> shortname = getattr ( settings , '<STR_LIT>' , shortname ) <EOL> return { <EOL> '<STR_LIT>' : shortname , <EOL> '<STR_LIT>' : get_config ( context ) , <EOL> } <EOL> @ register . inclusion_tag ( '<STR_LIT>' , takes_context = True ) <EOL> def disqus_recent_comments ( context , shortname = '<STR_LIT>' , num_items = <NUM_LIT:5> , excerpt_length = <NUM_LIT:200> , hide_avatars = <NUM_LIT:0> , avatar_size = <NUM_LIT:32> ) : <EOL> """<STR_LIT>""" <EOL> shortname = getattr ( settings , '<STR_LIT>' , shortname ) <EOL> return { <EOL> '<STR_LIT>' : shortname , <EOL> '<STR_LIT>' : num_items , <EOL> '<STR_LIT>' : hide_avatars , <EOL> '<STR_LIT>' : avatar_size , <EOL> '<STR_LIT>' : excerpt_length , <EOL> '<STR_LIT>' : get_config ( context ) , <EOL> } <EOL> @ register . inclusion_tag ( '<STR_LIT>' , takes_context = True ) <EOL> def disqus_show_comments ( context , shortname = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> shortname = getattr ( settings , '<STR_LIT>' , shortname ) <EOL> return { <EOL> '<STR_LIT>' : shortname , <EOL> '<STR_LIT>' : get_config ( context ) , <EOL> } </s>
