Package madrona :: Package google-analytics :: Package templatetags :: Module analytics
[hide private]

Source Code for Module madrona.google-analytics.templatetags.analytics

 1  from django import template 
 2  from django.db import models 
 3  from django.contrib.sites.models import Site 
 4   
 5  from django.template import Context, loader 
 6   
 7   
 8  register = template.Library() 
 9  Analytics = models.get_model('googleanalytics', 'analytics') 
10   
11 -def do_get_analytics(parser, token):
12 try: 13 # split_contents() knows not to split quoted strings. 14 tag_name, code = token.split_contents() 15 except ValueError: 16 code = None 17 18 if not code: 19 current_site = Site.objects.get_current() 20 else: 21 if not (code[0] == code[-1] and code[0] in ('"', "'")): 22 raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name 23 code = code[1:-1] 24 current_site = None 25 return AnalyticsNode(current_site, code)
26
27 -class AnalyticsNode(template.Node):
28 - def __init__(self, site=None, code=None):
29 self.site = site 30 self.code = code
31
32 - def render(self, context):
33 content = '' 34 if self.site: 35 code_set = self.site.analytics_set.all() 36 if code_set: 37 code = code_set[0].analytics_code 38 else: 39 return '' 40 elif self.code: 41 code = self.code 42 else: 43 return '' 44 45 if code.strip() != '': 46 t = loader.get_template('google-analytics/analytics_template.html') 47 c = Context({ 48 'analytics_code': code, 49 }) 50 return t.render(c) 51 else: 52 return ''
53 54 register.tag('analytics', do_get_analytics) 55