1 | from zope.component import queryUtility |
---|
2 | from zope.interface import implements, Interface |
---|
3 | |
---|
4 | from Products.Five import BrowserView |
---|
5 | from Products.CMFCore.utils import getToolByName |
---|
6 | |
---|
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 |
---|
12 | |
---|
13 | from quintagroup.gdocs.spreadsheet import spreadsheetMessageFactory as _ |
---|
14 | from quintagroup.gdocs.spreadsheet import logException, logger |
---|
15 | from quintagroup.gdocs.spreadsheet.config import ORDER_COLUMNS |
---|
16 | |
---|
17 | |
---|
18 | class IViewWorksheetView(Interface): |
---|
19 | """ |
---|
20 | ViewWorksheet view interface |
---|
21 | """ |
---|
22 | |
---|
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 | """ |
---|
29 | |
---|
30 | |
---|
31 | class ViewWorksheetView(BrowserView): |
---|
32 | """ |
---|
33 | ViewWorksheet browser view |
---|
34 | """ |
---|
35 | implements(IViewWorksheetView) |
---|
36 | |
---|
37 | def __init__(self, context, request): |
---|
38 | self.context = context |
---|
39 | self.request = request |
---|
40 | self.query = shDocumentQuery() |
---|
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 | |
---|
50 | @memoize_contextless |
---|
51 | def renderWorksheet(self, ssh_id="", |
---|
52 | wsh_id='', startrow_idx=0): |
---|
53 | """ |
---|
54 | """ |
---|
55 | |
---|
56 | # Authorization on spreadsheets.google.com |
---|
57 | gauth = queryUtility(IGAuthUtility) |
---|
58 | self.sh_client = SpreadsheetsService(email=gauth.email, password=gauth.password) |
---|
59 | sh_client = self.sh_client |
---|
60 | sh_client.ProgrammaticLogin() |
---|
61 | |
---|
62 | try: |
---|
63 | feed = sh_client.GetListFeed(ssh_id, wksht_id=wsh_id, query=self.query) |
---|
64 | except Exception: |
---|
65 | logException('GetListFeed function call: ' |
---|
66 | 'key=%s, wksht_id=%s' % (ssh_id, wsh_id)) |
---|
67 | return "" |
---|
68 | |
---|
69 | # Prepare table |
---|
70 | table = "<table id=\"sshwsh\">" |
---|
71 | table += "<tr>" |
---|
72 | table += ''.join(["<th>%s</th>" % key for key in ORDER_COLUMNS]) |
---|
73 | table += "</tr>" |
---|
74 | |
---|
75 | for i, entry in enumerate(feed.entry): |
---|
76 | if i+1 > startrow_idx: |
---|
77 | td_row = "<tr>" |
---|
78 | for key in ORDER_COLUMNS: |
---|
79 | td_row += "<td>%s</td>" \ |
---|
80 | % (not (entry.custom[key].text == 'None') and entry.custom[key].text or '') |
---|
81 | td_row += "</tr>\n" |
---|
82 | table += td_row |
---|
83 | |
---|
84 | table += "</table>" |
---|
85 | return table |
---|