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 quintagroiup.gauth.interfaces import IGAuthInterface |
---|
11 | from quintagroup.gdata.browser.configlet import IGDataConfigletSchema |
---|
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 | def __init__(self, context): |
---|
25 | self.context = context |
---|
26 | |
---|
27 | @property |
---|
28 | @memoize_contextless |
---|
29 | def gdataconf(self): |
---|
30 | pps = queryMultiAdapter((self.context, self.context.REQUEST), name="plone_portal_state") |
---|
31 | return queryAdapter(pps.portal(), IGDataConfigletSchema) |
---|
32 | |
---|
33 | @property |
---|
34 | def email(self): |
---|
35 | """ Get the email.""" |
---|
36 | return self.gdataconf.gdata_email |
---|
37 | |
---|
38 | @property |
---|
39 | def password(self): |
---|
40 | """ Get the password.""" |
---|
41 | return self.gdataconf.gdata_pass |
---|
42 | |
---|
43 | |
---|
44 | class SafeQuery(object): |
---|
45 | """ Base class for perform safe Google data service calls, |
---|
46 | with automatic programming re-loginning. |
---|
47 | |
---|
48 | So if class need automatic relogin to Google Date services, it must |
---|
49 | inherit from the class and use safeQuery metho to call to services methods |
---|
50 | |
---|
51 | For example ... |
---|
52 | |
---|
53 | class MyGdataService(SafeQuery): |
---|
54 | |
---|
55 | def __init__(self): |
---|
56 | gauth_util = queryUtility(IGAuthInterface) |
---|
57 | self.service = gdata.spreadsheet.service.SpreadsheetService( |
---|
58 | gauth_util.email, gauth_util.password) |
---|
59 | self.service.ProgrammaticLogin() |
---|
60 | |
---|
61 | def fooservice(self): |
---|
62 | ... |
---|
63 | self.safeQuery(self.service, self.service.GetSpreadsheetsFeed, |
---|
64 | title="some title") |
---|
65 | ... |
---|
66 | |
---|
67 | So call to fooservice should automatic relogin to SpreadsheetService even |
---|
68 | if token expired. |
---|
69 | """ |
---|
70 | |
---|
71 | def safeQuery(self, serv, meth, *margs, **mkwargs): |
---|
72 | try: |
---|
73 | return meth(*margs, **mkwargs) |
---|
74 | except gdata.service.RequestError, e: |
---|
75 | logException("Token Expired -> Update it.") |
---|
76 | if hasattr(serv, 'ProgrammaticLogin'): |
---|
77 | serv.ProgrammaticLogin() |
---|
78 | return meth(*margs, **mkwargs) |
---|
79 | else: |
---|
80 | raise |
---|
81 | |
---|