From patchwork Thu Jul 25 12:08:23 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stevan Radakovic X-Patchwork-Id: 18573 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-gh0-f198.google.com (mail-gh0-f198.google.com [209.85.160.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 8FDCB25E19 for ; Thu, 25 Jul 2013 12:08:26 +0000 (UTC) Received: by mail-gh0-f198.google.com with SMTP id r13sf1510283ghr.1 for ; Thu, 25 Jul 2013 05:08:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-beenthere:x-forwarded-to:x-forwarded-for:delivered-to :mime-version:x-launchpad-project:x-launchpad-branch :x-launchpad-message-rationale:x-launchpad-branch-revision-number :x-launchpad-notification-type:to:from:subject:message-id:date :reply-to:sender:errors-to:precedence:x-generated-by :x-launchpad-hash:x-gm-message-state:x-original-sender :x-original-authentication-results:mailing-list:list-id :x-google-group-id:list-post:list-help:list-archive:list-unsubscribe :content-type; bh=HLwU0DDxqwUEnwlYsrqBYsBEsy4eABtlPpCv2hjtN6g=; b=PvJz27srnA46myHXiB+4+jCdbmTqb6YihpuYIVO+8FmetvIzYFE+aW4A1MV1yp8XSR p6tMQvw6j3bAWRzJ01QkGn+1v1nFrm8+bhxGm6otoHJ8stmLH0elN+MzGGv8KYb5UVRT IuYJoa/ZPflgt4V5suLVR4rjdz3io5AN/9hQ4RNsLAagHOZ5FOi3dgcHpIZD+0t9IV98 q+RdyJfu2+XuJXKr3WS3p3qdbiO7RLpwj4oJsrENj6qQk9R2sCqERcoJl5QKTqCzzvLh FvBfRRZgM8d10wCxnRZSoML/rz2/DN5eCTgtA3lBtE3ANN7DhYK30qqiPBysx1itHyQP RkRA== X-Received: by 10.58.188.15 with SMTP id fw15mr5110938vec.9.1374754105662; Thu, 25 Jul 2013 05:08:25 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.82.101 with SMTP id h5ls610659qey.91.gmail; Thu, 25 Jul 2013 05:08:25 -0700 (PDT) X-Received: by 10.52.177.6 with SMTP id cm6mr14794401vdc.0.1374754105541; Thu, 25 Jul 2013 05:08:25 -0700 (PDT) Received: from mail-ve0-f170.google.com (mail-ve0-f170.google.com [209.85.128.170]) by mx.google.com with ESMTPS id i7si12245055vdv.138.2013.07.25.05.08.25 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 25 Jul 2013 05:08:25 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.170 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.170; Received: by mail-ve0-f170.google.com with SMTP id 14so429698vea.1 for ; Thu, 25 Jul 2013 05:08:25 -0700 (PDT) X-Received: by 10.58.2.137 with SMTP id 9mr17860923veu.50.1374754105450; Thu, 25 Jul 2013 05:08:25 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.58.165.8 with SMTP id yu8csp74172veb; Thu, 25 Jul 2013 05:08:24 -0700 (PDT) X-Received: by 10.180.107.163 with SMTP id hd3mr1873199wib.13.1374754104389; Thu, 25 Jul 2013 05:08:24 -0700 (PDT) Received: from indium.canonical.com (indium.canonical.com. [91.189.90.7]) by mx.google.com with ESMTPS id m4si1372747wiw.0.2013.07.25.05.08.23 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 25 Jul 2013 05:08:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) client-ip=91.189.90.7; Received: from ackee.canonical.com ([91.189.89.26]) by indium.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1V2KLH-0005CP-QL for ; Thu, 25 Jul 2013 12:08:23 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id AFC98E0469 for ; Thu, 25 Jul 2013 12:08:23 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: lava-server X-Launchpad-Branch: ~linaro-validation/lava-server/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 413 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~linaro-validation/lava-server/trunk] Rev 413: Add google analytics integration. Reviewed by terceiro. Message-Id: <20130725120823.10631.92354.launchpad@ackee.canonical.com> Date: Thu, 25 Jul 2013 12:08:23 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: list X-Generated-By: Launchpad (canonical.com); Revision="16700"; Instance="launchpad-lazr.conf" X-Launchpad-Hash: 67ae5ea9399454f32eab100a70c81aabfff1125d X-Gm-Message-State: ALoCoQlDrqvHpQebqXbm5OK8PuG+t0oh5S0TnkTyXfE2eglx7jaVz9LKlXPP6WUUvp7C7L04FzQX X-Original-Sender: noreply@launchpad.net X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.170 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Merge authors: Stevan Radaković (stevanr) Related merge proposals: https://code.launchpad.net/~stevanr/lava-server/add-google-analytics/+merge/176411 proposed by: Stevan Radaković (stevanr) review: Approve - Antonio Terceiro (terceiro) ------------------------------------------------------------ revno: 413 [merge] committer: Stevan Radakovic branch nick: trunk timestamp: Thu 2013-07-25 14:07:17 +0200 message: Add google analytics integration. Reviewed by terceiro. added: google_analytics/ google_analytics/LICENSE google_analytics/USAGE google_analytics/__init__.py google_analytics/migrations/ google_analytics/migrations/0001_initial.py google_analytics/migrations/__init__.py google_analytics/models.py google_analytics/templates/ google_analytics/templates/google_analytics/ google_analytics/templates/google_analytics/analytics_template.html google_analytics/templatetags/ google_analytics/templatetags/__init__.py google_analytics/templatetags/analytics.py modified: lava_server/settings/common.py lava_server/templates/layouts/base.html --- lp:lava-server https://code.launchpad.net/~linaro-validation/lava-server/trunk You are subscribed to branch lp:lava-server. To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-server/trunk/+edit-subscription === added directory 'google_analytics' === added file 'google_analytics/LICENSE' --- google_analytics/LICENSE 1970-01-01 00:00:00 +0000 +++ google_analytics/LICENSE 2013-07-22 17:01:41 +0000 @@ -0,0 +1,27 @@ +Copyright (c) 2008, Clint Ecker +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +Neither the name of the nor the names of its contributors may +be used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file === added file 'google_analytics/USAGE' --- google_analytics/USAGE 1970-01-01 00:00:00 +0000 +++ google_analytics/USAGE 2013-07-22 17:01:41 +0000 @@ -0,0 +1,51 @@ +I manage a lot of Django projects that present slightly-different forms to +users depending on the site/domain they're visiting. There's also a bunch of +custom submission code that differs from form to form, but that's neither here +nor there. + +I need different Google Analytics codes depending on the sites and after +sticking these tags into every single template, I thought it would be cool to +be able to manage these Google analytics accounts from the Django admin page. +I also added a mode of operation that excludes the admin interface altogether +(you can just use the template tag) + +==Two modes of operation== + +Administering and associating codes with Django sites +--------------------------------------------------------- +1. Add the `google_analytics` application to your `INSTALLED_APPS` + section of your `settings.py`. This mode requires that you be using + the Django sites framework too, so make sure you have that set up as + well. + +2. Add `GOOGLE_ANALYTICS_MODEL = True` to your `settings.py` + +3. Run a `./manage.py syncdb` to add the database tables + +4. Go to your project's admin page (usually `/admin/`) and click into a site + objects + +5. You'll now see a new field under the normal site information called + "Analytics Code". In this box you put your unique analytics code for + your project's domain. It looks like `UA-xxxxxx-x` and save the site. + +6. In your base template (usually a `base.html`) insert this tag at the very + top: `{% load analytics %}` + +7. In the same template, insert the following code right before the closing + body tag: `{% analytics %}` + + + +===Just using the template tag=== + +1. Add the `google_analytics` application to your `INSTALLED_APPS` section of + your `settings.py`. + +2. In your base template, usually a `base.html`, insert this tag at the very + top: `{% load analytics %}` + +3. In the same template, insert the following code right before the closing + body tag: `{% analytics "UA-xxxxxx-x" %}` the `UA-xxxxxx-x` is a + unique Google Analytics code for you domain when you sign up for a new + account. === added file 'google_analytics/__init__.py' === added directory 'google_analytics/migrations' === added file 'google_analytics/migrations/0001_initial.py' --- google_analytics/migrations/0001_initial.py 1970-01-01 00:00:00 +0000 +++ google_analytics/migrations/0001_initial.py 2013-07-22 17:01:41 +0000 @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding model 'Analytic' + db.create_table('google_analytics_analytic', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('site', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['sites.Site'], unique=True)), + ('analytics_code', self.gf('django.db.models.fields.CharField')(max_length=100, blank=True)), + )) + db.send_create_signal('google_analytics', ['Analytic']) + + + def backwards(self, orm): + # Deleting model 'Analytic' + db.delete_table('google_analytics_analytic') + + + models = { + 'google_analytics.analytic': { + 'Meta': {'object_name': 'Analytic'}, + 'analytics_code': ('django.db.models.fields.CharField', [], {'max_length': '100', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'site': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['sites.Site']", 'unique': 'True'}) + }, + 'sites.site': { + 'Meta': {'ordering': "('domain',)", 'object_name': 'Site', 'db_table': "'django_site'"}, + 'domain': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + } + } + + complete_apps = ['google_analytics'] \ No newline at end of file === added file 'google_analytics/migrations/__init__.py' === added file 'google_analytics/models.py' --- google_analytics/models.py 1970-01-01 00:00:00 +0000 +++ google_analytics/models.py 2013-07-23 12:14:04 +0000 @@ -0,0 +1,19 @@ +from django.db import models +from django.conf import settings +from django.contrib.sites.admin import SiteAdmin +from django.contrib.sites.models import Site +from django.contrib import admin + +if getattr(settings, 'GOOGLE_ANALYTICS_MODEL', False): + + class Analytic(models.Model): + site = models.ForeignKey(Site, unique=True) + analytics_code = models.CharField(blank=True, max_length=100) + + def __unicode__(self): + return u"%s" % (self.analytics_code) + + class AnalyticAdmin(admin.ModelAdmin): + list_display = ('site', 'analytics_code',) + + admin.site.register(Analytic, AnalyticAdmin) === added directory 'google_analytics/templates' === added directory 'google_analytics/templates/google_analytics' === added file 'google_analytics/templates/google_analytics/analytics_template.html' --- google_analytics/templates/google_analytics/analytics_template.html 1970-01-01 00:00:00 +0000 +++ google_analytics/templates/google_analytics/analytics_template.html 2013-07-24 11:50:02 +0000 @@ -0,0 +1,17 @@ + === added directory 'google_analytics/templatetags' === added file 'google_analytics/templatetags/__init__.py' === added file 'google_analytics/templatetags/analytics.py' --- google_analytics/templatetags/analytics.py 1970-01-01 00:00:00 +0000 +++ google_analytics/templatetags/analytics.py 2013-07-24 11:50:02 +0000 @@ -0,0 +1,55 @@ +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('google_analytics', 'analytic') + +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: + 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_set = Analytics.objects.filter(site=self.site) + if code_set: + code = code_set[0].analytics_code + else: + return '' + elif self.code: + code = self.code + else: + return '' + + if code.strip() != '': + t = loader.get_template('google_analytics/analytics_template.html') + c = Context({ + 'analytics_code': code, + 'domain': '.'.join(self.site.domain.split('.')[2:]) + }) + return t.render(c) + else: + return '' + +register.tag('analytics', do_get_analytics) === modified file 'lava_server/settings/common.py' --- lava_server/settings/common.py 2013-07-11 11:26:54 +0000 +++ lava_server/settings/common.py 2013-07-25 12:07:17 +0000 @@ -122,6 +122,7 @@ 'south', # Uncomment to enable Atlassian Crowd auth support. #'crowdrest', + 'google_analytics', ] try: @@ -168,6 +169,9 @@ RESTRUCTUREDTEXT_FILTER_SETTINGS = {"initial_header_level": 4} +# Add google analytics model. +GOOGLE_ANALYTICS_MODEL = True + # Skip south tests as they seem to break everything else. # This is fixed in south 0.7.1, if we upgrade past that it's safe to # remove this line. === modified file 'lava_server/templates/layouts/base.html' --- lava_server/templates/layouts/base.html 2012-09-20 20:58:20 +0000 +++ lava_server/templates/layouts/base.html 2013-07-24 10:06:01 +0000 @@ -1,3 +1,4 @@ +{% load analytics %} @@ -15,6 +16,7 @@ {% block extrahead %}{% endblock %} + {% analytics %} {% block body %} {% endblock %}