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