[2665] | 1 | from zope.component import queryUtility |
---|
[2654] | 2 | from zope.interface import implements, Interface |
---|
| 3 | |
---|
| 4 | from Products.Five import BrowserView |
---|
| 5 | from Products.CMFCore.utils import getToolByName |
---|
| 6 | |
---|
[2665] | 7 | from plone.memoize.view import memoize_contextless |
---|
| 8 | |
---|
| 9 | from quintagroup.gauth.interfaces import IGAuthUtility |
---|
| 10 | from gdata.spreadsheet.service import SpreadsheetsService |
---|
| 11 | from gdata.spreadsheet.service import DocumentQuery as shDocumentQuery |
---|
[2667] | 12 | from gdata.spreadsheet import SpreadsheetsListFeed |
---|
[2665] | 13 | |
---|
[2654] | 14 | from quintagroup.gdocs.spreadsheet import spreadsheetMessageFactory as _ |
---|
[2665] | 15 | from quintagroup.gdocs.spreadsheet import logException, logger |
---|
[2654] | 16 | |
---|
| 17 | |
---|
[2665] | 18 | class IViewWorksheetView(Interface): |
---|
[2654] | 19 | """ |
---|
[2665] | 20 | ViewWorksheet view interface |
---|
[2654] | 21 | """ |
---|
| 22 | |
---|
[2665] | 23 | def renderWorksheet(ssh_id, wsh_idx, startrow_idx): |
---|
| 24 | """ |
---|
| 25 | ssh_id - Id of the spreadsheet |
---|
| 26 | wsh_id - Id of the worksheet |
---|
| 27 | startrow_idx - index of row, from which table should be rendered |
---|
| 28 | """ |
---|
[2654] | 29 | |
---|
| 30 | |
---|
[2665] | 31 | class ViewWorksheetView(BrowserView): |
---|
[2654] | 32 | """ |
---|
[2665] | 33 | ViewWorksheet browser view |
---|
[2654] | 34 | """ |
---|
[2665] | 35 | implements(IViewWorksheetView) |
---|
[2654] | 36 | |
---|
| 37 | def __init__(self, context, request): |
---|
| 38 | self.context = context |
---|
| 39 | self.request = request |
---|
[2665] | 40 | self.query = shDocumentQuery() |
---|
[2654] | 41 | |
---|
| 42 | @property |
---|
| 43 | def portal_catalog(self): |
---|
| 44 | return getToolByName(self.context, 'portal_catalog') |
---|
| 45 | |
---|
| 46 | @property |
---|
| 47 | def portal(self): |
---|
| 48 | return getToolByName(self.context, 'portal_url').getPortalObject() |
---|
| 49 | |
---|
[2665] | 50 | @memoize_contextless |
---|
[2667] | 51 | def renderWorksheet(self, ssh_id="", wsh_id='', startrow_idx=0): |
---|
[2654] | 52 | """ |
---|
| 53 | """ |
---|
[2667] | 54 | feed = self.getFeed(ssh_id, wsh_id, startrow_idx) |
---|
| 55 | if isinstance(feed, SpreadsheetsListFeed): |
---|
| 56 | indx_keys = filter(None, [elem['select_title'] for elem in self.context.title_column]) |
---|
| 57 | titles_columns = filter(None, [self.context.order_columns[int(i)] for i in indx_keys]) |
---|
[2654] | 58 | |
---|
[2667] | 59 | # Prepare table |
---|
| 60 | table = "<table id=\"sshwsh\">" |
---|
| 61 | table += "<tr>" |
---|
| 62 | table += ''.join(["<th>%s</th>" % key for key in titles_columns]) |
---|
| 63 | table += "</tr>" |
---|
| 64 | for i, entry in enumerate(feed.entry): |
---|
| 65 | if i+1 > startrow_idx: |
---|
| 66 | td_row = "<tr>" |
---|
| 67 | for key in titles_columns: |
---|
| 68 | td_row += "<td>%s</td>" \ |
---|
| 69 | % (not (entry.custom[key].text == 'None') and entry.custom[key].text or '') |
---|
| 70 | td_row += "</tr>\n" |
---|
| 71 | table += td_row |
---|
| 72 | table += "</table>" |
---|
| 73 | else: |
---|
| 74 | table = '' |
---|
| 75 | return table |
---|
| 76 | |
---|
| 77 | def getFeed(self, ssh_id="", wsh_id='', startrow_idx=0): |
---|
| 78 | """ Get SpreadsheetsListFeed |
---|
| 79 | """ |
---|
[2665] | 80 | # Authorization on spreadsheets.google.com |
---|
| 81 | gauth = queryUtility(IGAuthUtility) |
---|
| 82 | self.sh_client = SpreadsheetsService(email=gauth.email, password=gauth.password) |
---|
[2667] | 83 | self.sh_client.ProgrammaticLogin() |
---|
[2665] | 84 | try: |
---|
[2667] | 85 | feed = self.sh_client.GetListFeed(ssh_id, wksht_id=wsh_id, query=self.query) |
---|
| 86 | self.context.order_columns = len(feed.entry) and feed.entry[0].custom.keys() or [] |
---|
[2665] | 87 | except Exception: |
---|
| 88 | logException('GetListFeed function call: ' |
---|
[2667] | 89 | 'key=%s, wksht_id=%s' % (ssh_id, wsh_id), self.context) |
---|
| 90 | feed = None |
---|
| 91 | return feed |
---|