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
@@ -46,9 +49,10 @@
assuming it is correct.
"""
from ga_auth import init_service
- initialize_service('token.dat',
- self.args[0] if self.args
- else 'credentials.json')
+ 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
@@ -73,18 +77,20 @@
def command(self):
self._load_config()
- from ga_auth import init_service
+ from download_analytics import DownloadAnalytics
+ from ga_auth import (init_service, get_profile_id)
+
try:
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':
@@ -93,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,12 +7,12 @@
from pylons import config
-def _prepare_credentials( token_filename, credentials_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 )
+ storage = Storage(token_filename)
credentials = storage.get()
if credentials is None or credentials.invalid:
@@ -23,7 +23,8 @@
return credentials
-def init_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
@@ -56,3 +57,4 @@
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))