source: products/quintagroup.gauth/trunk/quintagroup/gauth/utility.py @ 2609

Last change on this file since 2609 was 2609, checked in by mylan, 9 years ago

Register IGAuthUtility to LocalSiteManager? on installation and bound to configlet; some refactoring and updates

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1"""
2    Provide utility for getting GData portal account email and password.
3"""
4
5import logging
6from zope.interface import implements
7from zope.component import queryMultiAdapter, queryAdapter
8from plone.memoize.view import memoize_contextless
9
10from quintagroup.gauth.interfaces import IGAuthUtility
11from quintagroup.gauth.browser.configlet import IGAuthConfigletSchema
12
13logger = logging.getLogger('quintagroup.gauth')
14def 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())
20
21class GAuthUtility(object):
22    implements(IGAuthUtility)
23
24    gconf = None
25   
26    def __init__(self, context):
27        # Bind utility to the context
28        pps = queryMultiAdapter((context, context.REQUEST), name="plone_portal_state")
29        self.gconf = queryAdapter(pps.portal(), IGAuthConfigletSchema)
30
31    @property
32    def email(self):
33        """ Get the email."""
34        return getattr(self.gconf, 'gauth_email', '')
35
36    @property
37    def password(self):
38        """ Get the password."""
39        return getattr(self.gconf, 'gauth_pass', '')
40
41
42class 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):
54                gauth_util = queryUtility(IGAuthUtility)
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
Note: See TracBrowser for help on using the repository browser.