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

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

Added tests for SafeQuery? class

  • 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
6import gdata.service
7from zope.interface import implements
8from zope.component import queryMultiAdapter, queryAdapter
9from plone.memoize.view import memoize_contextless
10
11from quintagroup.gauth.interfaces import IGAuthUtility
12from quintagroup.gauth.browser.configlet import IGAuthConfigletSchema
13
14logger = logging.getLogger('quintagroup.gauth')
15def 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())
21
22class GAuthUtility(object):
23    implements(IGAuthUtility)
24
25    gconf = None
26   
27    def __init__(self, context):
28        # Bind utility to the context
29        pps = queryMultiAdapter((context, context.REQUEST), name="plone_portal_state")
30        self.gconf = queryAdapter(pps.portal(), IGAuthConfigletSchema)
31
32    @property
33    def email(self):
34        """ Get the email."""
35        return getattr(self.gconf, 'gauth_email', '')
36
37    @property
38    def password(self):
39        """ Get the password."""
40        return getattr(self.gconf, 'gauth_pass', '')
41
42
43class 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):
55                gauth_util = queryUtility(IGAuthUtility)
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
Note: See TracBrowser for help on using the repository browser.