flake8
--- a/ckanext/ga_report/command.py
+++ b/ckanext/ga_report/command.py
@@ -1,7 +1,10 @@
import logging
+import datetime
from ckan.lib.cli import CkanCommand
-# No other CKAN imports allowed until _load_config is run, or logging is disabled
+# No other CKAN imports allowed until _load_config is run,
+# or logging is disabled
+
class InitDB(CkanCommand):
"""Initialise the extension's database tables
@@ -26,6 +29,12 @@
class GetAuthToken(CkanCommand):
""" Get's the Google auth token
+
+ Usage: paster getauthtoken <credentials_file>
+
+ Where <credentials_file> is the file name containing the details
+ for the service (obtained from https://code.google.com/apis/console).
+ By default this is set to credentials.json
"""
summary = __doc__.split('\n')[0]
usage = __doc__
@@ -33,10 +42,17 @@
min_args = 0
def command(self):
- from ga_auth import initialize_service
- initialize_service('token.dat',
- self.args[0] if self.args
- else 'credentials.json')
+ """
+ In this case we don't want a valid service, but rather just to
+ force the user through the auth flow. We allow this to complete to
+ act as a form of verification instead of just getting the token and
+ assuming it is correct.
+ """
+ from ga_auth import init_service
+ init_service('token.dat',
+ self.args[0] if self.args
+ else 'credentials.json')
+
class LoadAnalytics(CkanCommand):
"""Get data from Google Analytics API and save it
@@ -61,18 +77,20 @@
def command(self):
self._load_config()
- from ga_auth import initialize_service
+ from download_analytics import DownloadAnalytics
+ from ga_auth import (init_service, get_profile_id)
+
try:
- svc = initialize_service(self.args[0], None)
+ svc = init_service(self.args[0], None)
except TypeError:
- print 'Have you correctly run the getauthtoken task and specified the correct file here'
+ print ('Have you correctly run the getauthtoken task and '
+ 'specified the correct file here')
return
- from download_analytics import DownloadAnalytics
- from ga_auth import get_profile_id
downloader = DownloadAnalytics(svc, profile_id=get_profile_id(svc))
- time_period = self.args[1] if self.args and len(self.args) > 1 else 'latest'
+ time_period = self.args[1] if self.args and len(self.args) > 1 \
+ else 'latest'
if time_period == 'all':
downloader.all_()
elif time_period == 'latest':
@@ -81,4 +99,3 @@
since_date = datetime.datetime.strptime(time_period, '%Y-%m-%d')
downloader.since_date(since_date)
-
--- a/ckanext/ga_report/ga_auth.py
+++ b/ckanext/ga_report/ga_auth.py
@@ -7,8 +7,12 @@
from pylons import config
-def _prepare_credentials( token_filename, credentials_filename ):
- storage = Storage( token_filename )
+def _prepare_credentials(token_filename, credentials_filename):
+ """
+ Either returns the user's oauth credentials or uses the credentials
+ file to generate a token (by forcing the user to login in the browser)
+ """
+ storage = Storage(token_filename)
credentials = storage.get()
if credentials is None or credentials.invalid:
@@ -19,7 +23,13 @@
return credentials
-def initialize_service( token_file, credentials_file ):
+
+def init_service(token_file, credentials_file):
+ """
+ Given a file containing the user's oauth token (and another with
+ credentials in case we need to generate the token) will return a
+ service object representing the analytics API.
+ """
http = httplib2.Http()
credentials = _prepare_credentials(token_file, credentials_file)
@@ -27,19 +37,24 @@
return build('analytics', 'v3', http=http)
+
def get_profile_id(service):
- # Get a list of all Google Analytics accounts for this user
+ """
+ Get the profile ID for this user and the service specified by the
+ 'googleanalytics.id' configuration option.
+ """
accounts = service.management().accounts().list().execute()
- if accounts.get('items'):
- firstAccountId = accounts.get('items')[0].get('id')
- webPropertyId = config.get('googleanalytics.id')
- profiles = service.management().profiles().list(
- accountId=firstAccountId,
- webPropertyId=webPropertyId).execute()
+ if not accounts.get('items'):
+ return None
- if profiles.get('items'):
- # return the first Profile ID
- return profiles.get('items')[0].get('id')
+ accountId = accounts.get('items')[0].get('id')
+ webPropertyId = config.get('googleanalytics.id')
+ profiles = service.management().profiles().list(
+ accountId=accountId, webPropertyId=webPropertyId).execute()
+
+ if profiles.get('items'):
+ return profiles.get('items')[0].get('id')
return None
+
--- a/ckanext/ga_report/ga_model.py
+++ b/ckanext/ga_report/ga_model.py
@@ -3,20 +3,23 @@
from sqlalchemy import Table, Column, MetaData
from sqlalchemy import types
-from sqlalchemy.sql import select, text
+from sqlalchemy.sql import select
from sqlalchemy import func
import ckan.model as model
from ckan.model.types import JsonType
from ckan.lib.base import *
+
def make_uuid():
return unicode(uuid.uuid4())
+
def init_tables():
metadata = MetaData()
package_stats = Table('ga_url', metadata,
- Column('id', types.UnicodeText, primary_key=True, default=make_uuid),
+ Column('id', types.UnicodeText, primary_key=True,
+ default=make_uuid),
Column('period_name', types.UnicodeText),
Column('period_complete_day', types.Integer),
Column('visits', types.Integer),
@@ -27,6 +30,7 @@
cached_tables = {}
+
def get_table(name):
if name not in cached_tables:
@@ -46,6 +50,7 @@
url = re.sub('https?://(www\.)?data.gov.uk', '', url)
return url
+
def _get_department_id_of_url(url):
# e.g. /dataset/fuel_prices
# e.g. /dataset/fuel_prices/resource/e63380d4
@@ -57,6 +62,7 @@
publisher_groups = dataset.get_groups('publisher')
if publisher_groups:
return publisher_groups[0].id
+
def update_url_stats(period_name, period_complete_day, url_data):
table = get_table('ga_url')
@@ -71,9 +77,9 @@
count = connection.execute(s).fetchone()
if count and count[0]:
# update the row
- connection.execute(table.update()\
+ connection.execute(table.update()
.where(table.c.period_name == period_name,
- table.c.url == url)\
+ table.c.url == url)
.values(period_complete_day=period_complete_day,
views=views,
department_id=department_id,
@@ -86,6 +92,6 @@
'views': views,
'department_id': department_id,
'next_page': next_page}
- connection.execute(stats.insert()\
- .values(**values))
+ connection.execute(stats.insert().
+ values(**values))