]> git.parisson.com Git - django-google-tools.git/commitdiff
Initial commit of the project.
authorClint Ecker <clint@arstechnica.com>
Mon, 7 Jul 2008 19:55:49 +0000 (19:55 +0000)
committerClint Ecker <clint@arstechnica.com>
Mon, 7 Jul 2008 19:55:49 +0000 (19:55 +0000)
__init__.py [new file with mode: 0644]
models.py [new file with mode: 0644]
templates/google_analytics/analytics_template.html [new file with mode: 0644]
templatetags/__init__.py [new file with mode: 0644]
templatetags/analytics.py [new file with mode: 0644]

diff --git a/__init__.py b/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/models.py b/models.py
new file mode 100644 (file)
index 0000000..f120687
--- /dev/null
+++ b/models.py
@@ -0,0 +1,9 @@
+from django.db import models
+from django.contrib.sites.models import Site
+
+class Analytics(models.Model):
+    site = models.ForeignKey(Site, edit_inline=models.TABULAR, max_num_in_admin=1, min_num_in_admin=1)
+    analytics_code = models.CharField(blank=True, max_length=100, core=True)
+
+    def __unicode__(self):
+        return u"%s" % (self.analytics_code)
diff --git a/templates/google_analytics/analytics_template.html b/templates/google_analytics/analytics_template.html
new file mode 100644 (file)
index 0000000..5fb5df1
--- /dev/null
@@ -0,0 +1,9 @@
+<script type="text/javascript">
+var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
+document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
+</script>
+<script type="text/javascript">
+var pageTracker = _gat._getTracker("{{ analytics_code }}");
+pageTracker._initData();
+pageTracker._trackPageview();
+</script>
\ No newline at end of file
diff --git a/templatetags/__init__.py b/templatetags/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/templatetags/analytics.py b/templatetags/analytics.py
new file mode 100644 (file)
index 0000000..7ea2b49
--- /dev/null
@@ -0,0 +1,48 @@
+from django import template
+from django.db import models
+from django.contrib.sites.models import Site
+
+from django.template import Context, loader
+
+
+register = template.Library()
+Analytics = models.get_model('googleanalytics', 'analytics')
+
+def do_get_analytics(parser, token):
+    try:
+        # split_contents() knows not to split quoted strings.
+        tag_name, code = token.split_contents()
+    except ValueError:
+        code = None
+   
+    if not code:
+        print "No code, grabbing from sites"
+        current_site = Site.objects.get_current()
+    else:
+        if not (code[0] == code[-1] and code[0] in ('"', "'")):
+            raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
+        code = code[1:-1]
+        current_site = None
+    return AnalyticsNode(current_site, code)
+    
+class AnalyticsNode(template.Node):
+    def __init__(self, site=None, code=None):
+        self.site = site
+        self.code = code
+        
+    def render(self, context):
+        content = ''
+        if self.site:
+            code = self.site.analytics_set.all()[0].analytics_code
+        elif self.code:
+            code = self.code
+        else:
+            return ''
+
+        t = loader.get_template('google_analytics/analytics_template.html')
+        c = Context({
+            'analytics_code': code,
+        })
+        return t.render(c)
+        
+register.tag('analytics', do_get_analytics)