From: Ross Jones Date: Tue, 09 Oct 2012 12:30:34 +0000 Subject: Commented the code in ga_auth X-Git-Url: http://maxious.lambdacomplex.org/git/?p=ckanext-ga-report.git&a=commitdiff&h=10281338ba3b9cf2233f70d500bb063274802363 --- Commented the code in ga_auth --- --- a/ckanext/ga_report/command.py +++ b/ckanext/ga_report/command.py @@ -26,6 +26,12 @@ class GetAuthToken(CkanCommand): """ Get's the Google auth token + + Usage: paster getauthtoken + + Where 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,7 +39,13 @@ min_args = 0 def command(self): - from ga_auth import initialize_service + """ + 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 initialize_service('token.dat', self.args[0] if self.args else 'credentials.json') @@ -61,9 +73,9 @@ def command(self): self._load_config() - from ga_auth import initialize_service + from ga_auth import init_service 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' return --- a/ckanext/ga_report/ga_auth.py +++ b/ckanext/ga_report/ga_auth.py @@ -8,6 +8,10 @@ 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() @@ -19,7 +23,12 @@ 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 +36,23 @@ 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