Skip to content
Browse files

working on some management commands, generate import csv from google …

…analytics data
  • Loading branch information...
1 parent 7c33db5 commit 6172af4447246331e8a43c09a0d67564f785f61b @andrewschoen andrewschoen committed May 1, 2011
View
3 .gitignore
@@ -10,4 +10,5 @@ build
dist
*.egg-info
redirect_demo/redirect_demo.db
-redirect_demo/media/*
+redirect_demo/media/*
+redirect_demo/settings_dev.py
View
0 cms_redirects/management/__init__.py
No changes.
View
0 cms_redirects/management/commands/__init__.py
No changes.
View
82 cms_redirects/management/commands/redirect_csv.py
@@ -0,0 +1,82 @@
+import StringIO
+import csv
+import datetime
+
+from django.core.management.base import BaseCommand, CommandError
+from django.contrib.sites.models import Site
+from django.conf import settings
+from django.utils import simplejson
+
+from googleanalytics import Connection
+from optparse import make_option
+
+class Command(BaseCommand):
+ can_import_settings = True
+ help='''
+
+ Sends to stdout a csv file that can be used to import redirects.
+
+ Optionally, you can specify google analytics account information to
+ prepopulate the csv file.
+
+ Usage:
+ ./manage.py redirect_csv > import.csv
+ ./manage.py redirect_csv -ga > import_google_analytics.csv
+
+ '''
+ option_list = BaseCommand.option_list + (
+ make_option('--rows',
+ dest="num_rows",
+ default=5,
+ help="Use to specify the number of extra rows"),
+ make_option('--ga',
+ action='store_true',
+ dest="use_analytics",
+ default=False,
+ help="Use google analytics data to preopulate the csv"),
+ make_option('--gauser',
+ dest="ga_user",
+ default=getattr(settings,"REDIRECT_GA_USERNAME",None),
+ help="Google analytics user, defaults to settings.REDIRECT_GA_USERNAME"),
+ make_option('--gapwd',
+ dest="ga_pwd",
+ default=getattr(settings, "REDIRECT_GA_PASSWORD",None),
+ help="Google analytics password, defaults to settings.REDIRECT_GA_PASSWORD"),
+ make_option('--gaprofile',
+ dest="ga_profile",
+ default=getattr(settings, "REDIRECT_GA_PROFILE", None),
+ help="Google analytics profile, defaults to settings.REDIRECT_GA_PROFILE"),
+ make_option('--gamonths',
+ dest="num_analytics_months",
+ default=6,
+ help="Number of months to pull google analytics data for"),
+ )
+
+
+ def execute(self, *args, **options):
+ num_rows = options["num_rows"]
+ output = StringIO.StringIO()
+ writer = csv.writer(output)
+ writer.writerow(['Old Url','New Url','Response Code'])
+ writer.writerow(["/old.html","/new",'301, 302 or 410'])
+ if options["use_analytics"]:
+ connection = Connection(options["ga_user"], options["ga_pwd"])
+ account = connection.get_account(options["ga_profile"])
+ end_date = datetime.date.today()
+ num_months = options["num_analytics_months"]
+ start_date = end_date - datetime.timedelta(num_months*365/12)
+ data = account.get_data(start_date=start_date, end_date=end_date, dimensions=['pagepath'], metrics=['visits'])
+ for url, visits in data.dict.iteritems():
+ writer.writerow([csv_safe(url),'',''])
+ else:
+ [writer.writerow(['','','']) for row in range(0,num_rows)]
+ print output.getvalue()
+
+def csv_safe(s):
+ if isinstance(s,basestring):
+ return s.encode("utf-8")
+ else:
+ return s
+
+
+
View
3 redirect_demo/requirements.txt
@@ -1,4 +1,5 @@
django==1.2.5
django-cms==2.1.3
django-appmedia
-south
+south
+python-googleanalytics
View
7 redirect_demo/settings.py
@@ -175,7 +175,10 @@
'cms_redirects',
)
-#admin tools
-ADMIN_TOOLS_INDEX_DASHBOARD = 'dashboard.UncommonCMSIndexDashboard'
SOUTH_TESTS_MIGRATE = False
+
+try:
+ from settings_dev import *
+except ImportError:
+ pass

0 comments on commit 6172af4

Please sign in to comment.
Something went wrong with that request. Please try again.