1 | """ |
---|
2 | Provide utility for getting GData portal account email and password. |
---|
3 | """ |
---|
4 | |
---|
5 | import logging |
---|
6 | import gdata.service |
---|
7 | from zope.interface import implements |
---|
8 | from zope.component import queryMultiAdapter, queryAdapter |
---|
9 | from plone.memoize.view import memoize_contextless |
---|
10 | |
---|
11 | from quintagroup.gauth.interfaces import IGAuthUtility |
---|
12 | from quintagroup.gauth.browser.configlet import IGAuthConfigletSchema |
---|
13 | |
---|
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()) |
---|
21 | |
---|
22 | class 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 | |
---|
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): |
---|
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 | |
---|