<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import json <EOL> import re <EOL> from django . conf import settings <EOL> from django . template import Library , Node , TemplateSyntaxError <EOL> from analytical . utils import get_identity , get_user_from_context , is_internal_ip , disable_html , get_required_setting <EOL> DOMAIN_RE = re . compile ( r'<STR_LIT>' ) <EOL> TRACKING_CODE = """<STR_LIT>""" <EOL> register = Library ( ) <EOL> @ register . tag <EOL> def woopra ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) > <NUM_LIT:1> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> return WoopraNode ( ) <EOL> class WoopraNode ( Node ) : <EOL> def __init__ ( self ) : <EOL> self . domain = get_required_setting ( '<STR_LIT>' , DOMAIN_RE , <EOL> "<STR_LIT>" ) <EOL> def render ( self , context ) : <EOL> settings = self . _get_settings ( context ) <EOL> visitor = self . _get_visitor ( context ) <EOL> html = TRACKING_CODE % { <EOL> '<STR_LIT>' : json . dumps ( settings , sort_keys = True ) , <EOL> '<STR_LIT>' : json . dumps ( visitor , sort_keys = True ) , <EOL> } <EOL> if is_internal_ip ( context , '<STR_LIT>' ) : <EOL> html = disable_html ( html , '<STR_LIT>' ) <EOL> return html <EOL> def _get_settings ( self , context ) : <EOL> vars = { '<STR_LIT>' : self . domain } <EOL> try : <EOL> vars [ '<STR_LIT>' ] = str ( settings . WOOPRA_IDLE_TIMEOUT ) <EOL> except AttributeError : <EOL> pass <EOL> return vars <EOL> def _get_visitor ( self , context ) : <EOL> vars = { } <EOL> for dict_ in context : <EOL> for var , val in dict_ . items ( ) : <EOL> if var . startswith ( '<STR_LIT>' ) : <EOL> vars [ var [ <NUM_LIT:7> : ] ] = val <EOL> if '<STR_LIT:name>' not in vars and '<STR_LIT:email>' not in vars : <EOL> user = get_user_from_context ( context ) <EOL> if user is not None and user . is_authenticated ( ) : <EOL> vars [ '<STR_LIT:name>' ] = get_identity ( context , '<STR_LIT>' , <EOL> self . _identify , user ) <EOL> if user . email : <EOL> vars [ '<STR_LIT:email>' ] = user . email <EOL> return vars <EOL> def _identify ( self , user ) : <EOL> name = user . get_full_name ( ) <EOL> if not name : <EOL> name = user . username <EOL> return name <EOL> def contribute_to_analytical ( add_node ) : <EOL> WoopraNode ( ) <EOL> add_node ( '<STR_LIT>' , WoopraNode ) </s>
