[2603] | 1 | """ |
---|
| 2 | Provide utility for getting GData portal account email and password. |
---|
| 3 | """ |
---|
| 4 | |
---|
[2604] | 5 | import logging |
---|
[2613] | 6 | import gdata.service |
---|
[2603] | 7 | from zope.interface import implements |
---|
| 8 | from zope.component import queryMultiAdapter, queryAdapter |
---|
| 9 | from plone.memoize.view import memoize_contextless |
---|
| 10 | |
---|
[2609] | 11 | from quintagroup.gauth.interfaces import IGAuthUtility |
---|
[2606] | 12 | from quintagroup.gauth.browser.configlet import IGAuthConfigletSchema |
---|
[2603] | 13 | |
---|
[2604] | 14 | logger = logging.getLogger('quintagroup.gauth') |
---|
| 15 | def logException(msg, context=None): |
---|
| 16 | logger.exception(msg) |
---|
| 17 | if context is not None: |
---|
| 18 | error_log = getattr(context, 'error_log', None) |
---|
| 19 | if error_log is not None: |
---|
| 20 | error_log.raising(sys.exc_info()) |
---|
[2603] | 21 | |
---|
| 22 | class GAuthUtility(object): |
---|
[2609] | 23 | implements(IGAuthUtility) |
---|
[2603] | 24 | |
---|
[2606] | 25 | gconf = None |
---|
[2609] | 26 | |
---|
| 27 | def __init__(self, context): |
---|
| 28 | # Bind utility to the context |
---|
| 29 | pps = queryMultiAdapter((context, context.REQUEST), name="plone_portal_state") |
---|
[2606] | 30 | self.gconf = queryAdapter(pps.portal(), IGAuthConfigletSchema) |
---|
[2603] | 31 | |
---|
| 32 | @property |
---|
| 33 | def email(self): |
---|
| 34 | """ Get the email.""" |
---|
[2609] | 35 | return getattr(self.gconf, 'gauth_email', '') |
---|
[2603] | 36 | |
---|
| 37 | @property |
---|
| 38 | def password(self): |
---|
| 39 | """ Get the password.""" |
---|
[2609] | 40 | return getattr(self.gconf, 'gauth_pass', '') |
---|
[2603] | 41 | |
---|
| 42 | |
---|
[2604] | 43 | class SafeQuery(object): |
---|
| 44 | """ Base class for perform safe Google data service calls, |
---|
| 45 | with automatic programming re-loginning. |
---|
| 46 | |
---|
| 47 | So if class need automatic relogin to Google Date services, it must |
---|
| 48 | inherit from the class and use safeQuery metho to call to services methods |
---|
| 49 | |
---|
| 50 | For example ... |
---|
| 51 | |
---|
| 52 | class MyGdataService(SafeQuery): |
---|
| 53 | |
---|
| 54 | def __init__(self): |
---|
[2609] | 55 | gauth_util = queryUtility(IGAuthUtility) |
---|
[2604] | 56 | self.service = gdata.spreadsheet.service.SpreadsheetService( |
---|
| 57 | gauth_util.email, gauth_util.password) |
---|
| 58 | self.service.ProgrammaticLogin() |
---|
| 59 | |
---|
| 60 | def fooservice(self): |
---|
| 61 | ... |
---|
| 62 | self.safeQuery(self.service, self.service.GetSpreadsheetsFeed, |
---|
| 63 | title="some title") |
---|
| 64 | ... |
---|
| 65 | |
---|
| 66 | So call to fooservice should automatic relogin to SpreadsheetService even |
---|
| 67 | if token expired. |
---|
| 68 | """ |
---|
| 69 | |
---|
| 70 | def safeQuery(self, serv, meth, *margs, **mkwargs): |
---|
| 71 | try: |
---|
| 72 | return meth(*margs, **mkwargs) |
---|
| 73 | except gdata.service.RequestError, e: |
---|
| 74 | logException("Token Expired -> Update it.") |
---|
| 75 | if hasattr(serv, 'ProgrammaticLogin'): |
---|
| 76 | serv.ProgrammaticLogin() |
---|
| 77 | return meth(*margs, **mkwargs) |
---|
| 78 | else: |
---|
| 79 | raise |
---|
| 80 | |
---|