Add missing import, explains why it was broken before
Add missing import, explains why it was broken before

--- a/ckanext/ga_report/download_analytics.py
+++ b/ckanext/ga_report/download_analytics.py
@@ -258,37 +258,32 @@
         return data
 
     def _get_json(self, params, prev_fail=False):
-        if prev_fail:
-          import os
-          ga_token_filepath = os.path.expanduser(config.get('googleanalytics.token.filepath', ''))
-          if not ga_token_filepath:
-              print 'ERROR: In the CKAN config you need to specify the filepath of the ' \
-                    'Google Analytics token file under key: googleanalytics.token.filepath'
-              return
-
-          try:
-              self.token, svc = init_service(ga_token_filepath, None)
-          except TypeError:
-              print ('Have you correctly run the getauthtoken task and '
-                     'specified the correct token file in the CKAN config under '
-                     '"googleanalytics.token.filepath"?')
-
-        try:
-            # Because of issues of invalid responses, we are going to make these requests
-            # ourselves.
+        ga_token_filepath = os.path.expanduser(config.get('googleanalytics.token.filepath', ''))
+        if not ga_token_filepath:
+            print 'ERROR: In the CKAN config you need to specify the filepath of the ' \
+                'Google Analytics token file under key: googleanalytics.token.filepath'
+            return
+
+        log.info("Trying to refresh our OAuth token")
+        try:
+            from ga_auth import init_service
+            self.token, svc = init_service(ga_token_filepath, None)
+            log.info("OAuth token refreshed")
+        except Exception, auth_exception:
+            log.error("Oauth refresh failed")
+            log.exception(auth_exception)
+            return
+
+        try:
             headers = {'authorization': 'Bearer ' + self.token}
             r = requests.get("https://www.googleapis.com/analytics/v3/data/ga", params=params, headers=headers)
             if r.status_code != 200:
-              log.info("STATUS: %s" % (r.status_code,))
-              log.info("CONTENT: %s" % (r.content,))
-              raise Exception("Request with params: %s failed" % params)
+                log.info("STATUS: %s" % (r.status_code,))
+                log.info("CONTENT: %s" % (r.content,))
+                raise Exception("Request with params: %s failed" % params)
 
             return json.loads(r.content)
         except Exception, e:
-            if not prev_fail:
-              print e
-              results = self._get_json(self, params, prev_fail=True)
-            else:
               log.exception(e)
 
         return dict(url=[])

--- a/ckanext/ga_report/ga_model.py
+++ b/ckanext/ga_report/ga_model.py
@@ -125,7 +125,7 @@
         dataset_ref = dataset_match.groups()[0]
         dataset = model.Package.get(dataset_ref)
         if dataset:
-            publisher_groups = dataset.get_groups('publisher')
+            publisher_groups = dataset.get_groups('organization')
             if publisher_groups:
                 return dataset_ref,publisher_groups[0].name
         return dataset_ref, None
@@ -323,11 +323,11 @@
     """
     toplevel = get_top_level()
     publishers = model.Session.query(model.Group).\
-        filter(model.Group.type=='publisher').\
+        filter(model.Group.type=='organization').\
         filter(model.Group.state=='active').all()
     for publisher in publishers:
         views, visits, subpub = update_publisher(period_name, publisher, publisher.name)
-        parent, parents = '', publisher.get_groups('publisher')
+        parent, parents = '', publisher.get_groups('organization')
         if parents:
             parent = parents[0].name
         item = model.Session.query(GA_Publisher).\
@@ -377,7 +377,7 @@
                      model.Member.table_name == 'group' and \
                      model.Member.state == 'active').\
            filter(model.Member.id==None).\
-           filter(model.Group.type=='publisher').\
+           filter(model.Group.type=='organization').\
            order_by(model.Group.name).all()
 
 def get_children(publisher):

--- a/ckanext/ga_report/helpers.py
+++ b/ckanext/ga_report/helpers.py
@@ -71,7 +71,7 @@
 def single_popular_dataset_html(top=20):
     dataset_dict = single_popular_dataset(top)
     groups = package.get('groups', [])
-    publishers = [ g for g in groups if g.get('type') == 'publisher' ]
+    publishers = [ g for g in groups if g.get('type') == 'organization' ]
     publisher = publishers[0] if publishers else {'name':'', 'title': ''}
     context = {
         'dataset': dataset_dict,
@@ -118,7 +118,7 @@
 
             if not p in datasets:
                 datasets[p] = {'views':0, 'visits': 0}
-                
+
             datasets[p]['views'] = datasets[p]['views'] + int(entry.pageviews)
             datasets[p]['visits'] = datasets[p]['visits'] + int(entry.visits)
 

--- a/ckanext/ga_report/templates/ga_report/publisher/index.html
+++ b/ckanext/ga_report/templates/ga_report/publisher/index.html
@@ -9,9 +9,9 @@
 
 
 
-  <py:def function="sidebar">
+  <py:match path="primarysidebar">
     ${ga_sidebar(download_link=h.url_for(controller='ckanext.ga_report.controller:GaDatasetReport',action='publisher_csv',month=c.month or 'all'))}
-  </py:def>
+  </py:match>
 
 
   <py:def function="optional_head">

--- a/ckanext/ga_report/templates/ga_report/publisher/read.html
+++ b/ckanext/ga_report/templates/ga_report/publisher/read.html
@@ -19,9 +19,9 @@
     <script type="text/javascript" src="/scripts/vendor/rickshaw.min.js"></script>
   </py:def>
 
-  <py:def function="sidebar">
+  <py:match path="primarysidebar">
     ${ga_sidebar(download_link=h.url_for(controller='ckanext.ga_report.controller:GaDatasetReport',action='dataset_csv',id=c.publisher_name or 'all',month=c.month or 'all'))}
-  </py:def>
+  </py:match>
   <py:def function="page_heading">Site Usage ${usage_nav('Datasets')}</py:def>
 
   <div py:match="content">

--- a/ckanext/ga_report/templates/ga_report/site/downloads.html
+++ b/ckanext/ga_report/templates/ga_report/site/downloads.html
@@ -7,9 +7,9 @@
 
   <py:def function="page_title">Downloads</py:def>
 
-  <py:def path="sidebar">
+  <py:match path="primarysidebar">
     ${ga_sidebar(download_link=h.url_for(controller='ckanext.ga_report.controller:GaReport',action='csv_downloads',month=c.month or 'all'))}
-  </py:def>
+  </py:match>
 
   <py:def function="page_heading">Downloads ${usage_nav('Downloads')}</py:def>
   <div py:match="content">

--- a/ckanext/ga_report/templates/ga_report/site/index.html
+++ b/ckanext/ga_report/templates/ga_report/site/index.html
@@ -19,9 +19,9 @@
     <script type="text/javascript" src="/scripts/vendor/rickshaw.min.js"></script>
   </py:def>
 
-  <py:def function="sidebar">
+  <py:match path="primarysidebar">
     ${ga_sidebar(download_link=h.url_for(controller='ckanext.ga_report.controller:GaReport',action='csv',month=c.month or 'all'))}
-  </py:def>
+  </py:match>
 
   <py:def function="page_heading">Site Usage ${usage_nav('Site-wide')}</py:def>
 
@@ -168,7 +168,7 @@
         CKAN.GA_Reports.bind_sparklines();
         CKAN.GA_Reports.bind_sidebar();
         CKAN.GA_Reports.bind_month_selector();
-      });  
+      });
     </script>
   </py:def>