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