1 | import re |
---|
2 | from zope.component import queryAdapter |
---|
3 | from zope.component import queryMultiAdapter |
---|
4 | from Products.Five.browser import BrowserView |
---|
5 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
6 | |
---|
7 | from quintagroup.gauth.interfaces import IGAuthUtility |
---|
8 | |
---|
9 | REBODY = re.compile("<body\s[^\>]*>(.*)</body>", re.I|re.S) |
---|
10 | |
---|
11 | class TestView( BrowserView ): |
---|
12 | |
---|
13 | def __init__(self, *args, **kwargs): |
---|
14 | super(TestView, self).__init__(*args, **kwargs) |
---|
15 | self.gauth = queryAdapter(self.context, IGAuthUtility) |
---|
16 | |
---|
17 | @property |
---|
18 | def sh_title(self): |
---|
19 | return self.request.get("title", "mylan test") |
---|
20 | |
---|
21 | @property |
---|
22 | def base_download_url(self): |
---|
23 | return "%s/gauth-download?title=%s" % ( |
---|
24 | self.context.absolute_url(), self.sh_title) |
---|
25 | |
---|
26 | def files(self): |
---|
27 | return [{"title": fn, "html": self.getWSheetHTML(f)} \ |
---|
28 | for fn, f in self.gauth.downloadSpreadSheetByTitle(self.sh_title)] |
---|
29 | |
---|
30 | def getWSheetHTML(self, fpath): |
---|
31 | fulltext = self.getHTML(fpath) |
---|
32 | body = REBODY.search(fulltext) |
---|
33 | return body and body.groups()[0] or "" |
---|
34 | |
---|
35 | def getHTML(self, fpath): |
---|
36 | res = "" |
---|
37 | try: |
---|
38 | f = file(fpath, 'rb') |
---|
39 | res = f.read() |
---|
40 | except: |
---|
41 | pass |
---|
42 | if "f" in locals().keys(): |
---|
43 | f.close() |
---|
44 | return res |
---|
45 | |
---|
46 | def download(self): |
---|
47 | index = int(self.request.get("i", -1)) |
---|
48 | if index < 0: |
---|
49 | return |
---|
50 | |
---|
51 | files = self.gauth.downloadSpreadSheetByTitle(self.sh_title) |
---|
52 | data = self.getHTML(files[index][1]) |
---|
53 | if not data: |
---|
54 | return "" |
---|
55 | response = self.request.RESPONSE |
---|
56 | response.setHeader('Content-Type', "text/html") |
---|
57 | response.setHeader('Content-Length', len(data)) |
---|
58 | response.setHeader('Accept-Ranges', 'bytes') |
---|
59 | |
---|
60 | response.setBase(None) |
---|
61 | return data |
---|