Mercurial > hg > documentViewer
annotate documentViewer.py @ 548:7acc919f52ff
mode=filepath works again
author | casties |
---|---|
date | Fri, 31 Aug 2012 15:55:45 +0200 |
parents | 2928037f9a75 |
children | 13eb36c0b00c |
rev | line source |
---|---|
0 | 1 from OFS.Folder import Folder |
2 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate | |
486 | 3 from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
487 | 4 from App.ImageFile import ImageFile |
0 | 5 from AccessControl import ClassSecurityInfo |
32 | 6 from AccessControl import getSecurityManager |
0 | 7 from Globals import package_home |
8 | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
9 import xml.etree.ElementTree as ET |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
10 |
489
55e3398e395e
more new templates. monkey-patch for App.ImageFile.
casties
parents:
488
diff
changeset
|
11 import os |
31 | 12 import sys |
0 | 13 import urllib |
50
6c0f20cecc60
added evaluation of the presentation/info.xml in texttools
dwinter
parents:
49
diff
changeset
|
14 import logging |
61 | 15 import math |
46 | 16 import urlparse |
174 | 17 import re |
389 | 18 import string |
526 | 19 import json |
231 | 20 |
541 | 21 from Products.MetaDataProvider import MetaDataFolder |
22 | |
514 | 23 from SrvTxtUtils import getInt, utf8ify, getText, getHttpData, refreshingImageFileIndexHtml |
52 | 24 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
25 def serializeNode(node, encoding="utf-8"): |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
26 """returns a string containing node as XML""" |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
27 s = ET.tostring(node) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
28 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
29 # 4Suite: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
30 # stream = cStringIO.StringIO() |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
31 # Ft.Xml.Domlette.Print(node, stream=stream, encoding=encoding) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
32 # s = stream.getvalue() |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
33 # stream.close() |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
34 return s |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
35 |
387 | 36 def browserCheck(self): |
37 """check the browsers request to find out the browser type""" | |
38 bt = {} | |
39 ua = self.REQUEST.get_header("HTTP_USER_AGENT") | |
40 bt['ua'] = ua | |
41 bt['isIE'] = False | |
42 bt['isN4'] = False | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
43 bt['versFirefox']="" |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
44 bt['versIE']="" |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
45 bt['versSafariChrome']="" |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
46 bt['versOpera']="" |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
47 |
387 | 48 if string.find(ua, 'MSIE') > -1: |
49 bt['isIE'] = True | |
50 else: | |
51 bt['isN4'] = (string.find(ua, 'Mozilla/4.') > -1) | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
52 # Safari oder Chrome identification |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
53 try: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
54 nav = ua[string.find(ua, '('):] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
55 nav1=ua[string.find(ua,')'):] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
56 nav2=nav1[string.find(nav1,'('):] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
57 nav3=nav2[string.find(nav2,')'):] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
58 ie = string.split(nav, "; ")[1] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
59 ie1 =string.split(nav1, " ")[2] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
60 ie2 =string.split(nav3, " ")[1] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
61 ie3 =string.split(nav3, " ")[2] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
62 if string.find(ie3, "Safari") >-1: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
63 bt['versSafariChrome']=string.split(ie2, "/")[1] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
64 except: pass |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
65 # IE identification |
387 | 66 try: |
67 nav = ua[string.find(ua, '('):] | |
68 ie = string.split(nav, "; ")[1] | |
69 if string.find(ie, "MSIE") > -1: | |
70 bt['versIE'] = string.split(ie, " ")[1] | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
71 except:pass |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
72 # Firefox identification |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
73 try: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
74 nav = ua[string.find(ua, '('):] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
75 nav1=ua[string.find(ua,')'):] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
76 if string.find(ie1, "Firefox") >-1: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
77 nav5= string.split(ie1, "/")[1] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
78 logging.debug("FIREFOX: %s"%(nav5)) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
79 bt['versFirefox']=nav5[0:3] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
80 except:pass |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
81 #Opera identification |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
82 try: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
83 if string.find(ua,"Opera") >-1: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
84 nav = ua[string.find(ua, '('):] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
85 nav1=nav[string.find(nav,')'):] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
86 bt['versOpera']=string.split(nav1,"/")[2] |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
87 except:pass |
387 | 88 |
89 bt['isMac'] = string.find(ua, 'Macintosh') > -1 | |
90 bt['isWin'] = string.find(ua, 'Windows') > -1 | |
91 bt['isIEWin'] = bt['isIE'] and bt['isWin'] | |
92 bt['isIEMac'] = bt['isIE'] and bt['isMac'] | |
93 bt['staticHTML'] = False | |
94 | |
95 return bt | |
234 | 96 |
464 | 97 def getParentPath(path, cnt=1): |
98 """returns pathname shortened by cnt""" | |
99 # make sure path doesn't end with / | |
100 path = path.rstrip('/') | |
101 # split by /, shorten, and reassemble | |
102 return '/'.join(path.split('/')[0:-cnt]) | |
103 | |
22 | 104 ## |
105 ## documentViewer class | |
106 ## | |
107 class documentViewer(Folder): | |
0 | 108 """document viewer""" |
109 meta_type="Document viewer" | |
110 | |
111 security=ClassSecurityInfo() | |
22 | 112 manage_options=Folder.manage_options+( |
507 | 113 {'label':'Configuration','action':'changeDocumentViewerForm'}, |
0 | 114 ) |
463 | 115 |
116 metadataService = None | |
117 """MetaDataFolder instance""" | |
525 | 118 |
0 | 119 |
525 | 120 # |
22 | 121 # templates and forms |
525 | 122 # |
123 # viewMode templates | |
479 | 124 viewer_text = PageTemplateFile('zpt/viewer_text', globals()) |
503 | 125 viewer_xml = PageTemplateFile('zpt/viewer_xml', globals()) |
483 | 126 viewer_images = PageTemplateFile('zpt/viewer_images', globals()) |
499
3f9703746fef
more cleanup. new template for viewMode=index (not pretty so far).
casties
parents:
498
diff
changeset
|
127 viewer_index = PageTemplateFile('zpt/viewer_index', globals()) |
528 | 128 viewer_thumbs = PageTemplateFile('zpt/viewer_thumbs', globals()) |
546 | 129 # available layer types (annotator not default) |
130 builtinLayers = {'text': ['dict','search','gis'], | |
526 | 131 'xml': None, 'images': None, 'index': None} |
132 availableLayers = builtinLayers; | |
525 | 133 # layer templates |
526 | 134 layer_text_dict = PageTemplateFile('zpt/layer_text_dict', globals()) |
135 layer_text_search = PageTemplateFile('zpt/layer_text_search', globals()) | |
525 | 136 layer_text_annotator = PageTemplateFile('zpt/layer_text_annotator', globals()) |
137 layer_text_gis = PageTemplateFile('zpt/layer_text_gis', globals()) | |
138 # toc templates | |
90
6a4a72033d58
new version with new full-text infrastructure and some more changed templates
casties
parents:
84
diff
changeset
|
139 toc_thumbs = PageTemplateFile('zpt/toc_thumbs', globals()) |
6a4a72033d58
new version with new full-text infrastructure and some more changed templates
casties
parents:
84
diff
changeset
|
140 toc_text = PageTemplateFile('zpt/toc_text', globals()) |
6a4a72033d58
new version with new full-text infrastructure and some more changed templates
casties
parents:
84
diff
changeset
|
141 toc_figures = PageTemplateFile('zpt/toc_figures', globals()) |
526 | 142 toc_concordance = PageTemplateFile('zpt/toc_concordance', globals()) |
491 | 143 toc_none = PageTemplateFile('zpt/toc_none', globals()) |
525 | 144 # other templates |
501 | 145 common_template = PageTemplateFile('zpt/common_template', globals()) |
57 | 146 info_xml = PageTemplateFile('zpt/info_xml', globals()) |
487 | 147 docuviewer_css = ImageFile('css/docuviewer.css',globals()) |
532 | 148 # make docuviewer_css refreshable for development |
490
6f116b86a226
more new template stuff. moved ImageFile index method to SrvTxtUtils
casties
parents:
489
diff
changeset
|
149 docuviewer_css.index_html = refreshingImageFileIndexHtml |
535
510bae2b593b
more fixes, e.g. IE support. first version of index page.
casties
parents:
532
diff
changeset
|
150 docuviewer_ie_css = ImageFile('css/docuviewer_ie.css',globals()) |
510bae2b593b
more fixes, e.g. IE support. first version of index page.
casties
parents:
532
diff
changeset
|
151 # make docuviewer_ie_css refreshable for development |
510bae2b593b
more fixes, e.g. IE support. first version of index page.
casties
parents:
532
diff
changeset
|
152 docuviewer_ie_css.index_html = refreshingImageFileIndexHtml |
501 | 153 jquery_js = ImageFile('js/jquery.js',globals()) |
130
5c779d7b5f71
more modular version with separate object MpdlXmlTextServer
casties
parents:
128
diff
changeset
|
154 |
5c779d7b5f71
more modular version with separate object MpdlXmlTextServer
casties
parents:
128
diff
changeset
|
155 |
95 | 156 def __init__(self,id,imageScalerUrl=None,textServerName=None,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=5,authgroups="mpiwg"): |
0 | 157 """init document viewer""" |
158 self.id=id | |
159 self.title=title | |
25 | 160 self.thumbcols = thumbcols |
161 self.thumbrows = thumbrows | |
32 | 162 # authgroups is list of authorized groups (delimited by ,) |
163 self.authgroups = [s.strip().lower() for s in authgroups.split(',')] | |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
164 # create template folder so we can always use template.something |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
165 |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
166 templateFolder = Folder('template') |
498 | 167 self['template'] = templateFolder # Zope-2.12 style |
168 #self._setObject('template',templateFolder) # old style | |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
169 try: |
130
5c779d7b5f71
more modular version with separate object MpdlXmlTextServer
casties
parents:
128
diff
changeset
|
170 import MpdlXmlTextServer |
132 | 171 textServer = MpdlXmlTextServer.MpdlXmlTextServer(id='fulltextclient',serverName=textServerName) |
498 | 172 templateFolder['fulltextclient'] = textServer |
173 #templateFolder._setObject('fulltextclient',textServer) | |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
174 except Exception, e: |
130
5c779d7b5f71
more modular version with separate object MpdlXmlTextServer
casties
parents:
128
diff
changeset
|
175 logging.error("Unable to create MpdlXmlTextServer for fulltextclient: "+str(e)) |
463 | 176 |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
177 try: |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
178 from Products.zogiLib.zogiLib import zogiLib |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
179 zogilib = zogiLib(id="zogilib", title="zogilib for docuviewer", dlServerURL=imageScalerUrl, layout="book") |
498 | 180 templateFolder['zogilib'] = zogilib |
181 #templateFolder._setObject('zogilib',zogilib) | |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
182 except Exception, e: |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
183 logging.error("Unable to create zogiLib for zogilib: "+str(e)) |
463 | 184 |
185 try: | |
186 # assume MetaDataFolder instance is called metadata | |
187 self.metadataService = getattr(self, 'metadata') | |
188 except Exception, e: | |
189 logging.error("Unable to find MetaDataFolder 'metadata': "+str(e)) | |
190 | |
477 | 191 if digilibBaseUrl is not None: |
192 self.digilibBaseUrl = digilibBaseUrl | |
193 | |
130
5c779d7b5f71
more modular version with separate object MpdlXmlTextServer
casties
parents:
128
diff
changeset
|
194 |
5c779d7b5f71
more modular version with separate object MpdlXmlTextServer
casties
parents:
128
diff
changeset
|
195 # proxy text server methods to fulltextclient |
5c779d7b5f71
more modular version with separate object MpdlXmlTextServer
casties
parents:
128
diff
changeset
|
196 def getTextPage(self, **args): |
498 | 197 """returns full text content of page""" |
130
5c779d7b5f71
more modular version with separate object MpdlXmlTextServer
casties
parents:
128
diff
changeset
|
198 return self.template.fulltextclient.getTextPage(**args) |
22 | 199 |
508 | 200 def getSearchResults(self, **args): |
201 """loads list of search results and stores XML in docinfo""" | |
202 return self.template.fulltextclient.getSearchResults(**args) | |
203 | |
204 def getResultsPage(self, **args): | |
205 """returns one page of the search results""" | |
206 return self.template.fulltextclient.getResultsPage(**args) | |
207 | |
516 | 208 def getTextInfo(self, **args): |
209 """returns document info from the text server""" | |
210 return self.template.fulltextclient.getTextInfo(**args) | |
211 | |
499
3f9703746fef
more cleanup. new template for viewMode=index (not pretty so far).
casties
parents:
498
diff
changeset
|
212 def getToc(self, **args): |
508 | 213 """loads table of contents and stores XML in docinfo""" |
499
3f9703746fef
more cleanup. new template for viewMode=index (not pretty so far).
casties
parents:
498
diff
changeset
|
214 return self.template.fulltextclient.getToc(**args) |
3f9703746fef
more cleanup. new template for viewMode=index (not pretty so far).
casties
parents:
498
diff
changeset
|
215 |
3f9703746fef
more cleanup. new template for viewMode=index (not pretty so far).
casties
parents:
498
diff
changeset
|
216 def getTocPage(self, **args): |
3f9703746fef
more cleanup. new template for viewMode=index (not pretty so far).
casties
parents:
498
diff
changeset
|
217 """returns one page of the table of contents""" |
3f9703746fef
more cleanup. new template for viewMode=index (not pretty so far).
casties
parents:
498
diff
changeset
|
218 return self.template.fulltextclient.getTocPage(**args) |
3f9703746fef
more cleanup. new template for viewMode=index (not pretty so far).
casties
parents:
498
diff
changeset
|
219 |
506 | 220 def getPlacesOnPage(self, **args): |
221 """get list of gis places on one page""" | |
222 return self.template.fulltextclient.getPlacesOnPage(**args) | |
307 | 223 |
548 | 224 # Thumb list for CoolIris Plugin |
499
3f9703746fef
more cleanup. new template for viewMode=index (not pretty so far).
casties
parents:
498
diff
changeset
|
225 thumbs_main_rss = PageTemplateFile('zpt/thumbs_main_rss', globals()) |
68 | 226 security.declareProtected('View','thumbs_rss') |
227 def thumbs_rss(self,mode,url,viewMode="auto",start=None,pn=1): | |
228 ''' | |
229 view it | |
230 @param mode: defines how to access the document behind url | |
231 @param url: url which contains display information | |
232 @param viewMode: if images display images, if text display text, default is images (text,images or auto) | |
233 | |
234 ''' | |
235 | |
236 if not hasattr(self, 'template'): | |
237 # create template folder if it doesn't exist | |
238 self.manage_addFolder('template') | |
239 | |
240 if not self.digilibBaseUrl: | |
241 self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary" | |
242 | |
243 docinfo = self.getDocinfo(mode=mode,url=url) | |
338 | 244 #pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo) |
345 | 245 pageinfo = self.getPageinfo(start=start,current=pn, docinfo=docinfo) |
331 | 246 ''' ZDES ''' |
68 | 247 pt = getattr(self.template, 'thumbs_main_rss') |
248 | |
249 if viewMode=="auto": # automodus gewaehlt | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
250 if docinfo.has_key("textURL") or docinfo.get('textURLPath',None): #texturl gesetzt und textViewer konfiguriert |
68 | 251 viewMode="text" |
252 else: | |
253 viewMode="images" | |
254 | |
255 return pt(docinfo=docinfo,pageinfo=pageinfo,viewMode=viewMode) | |
465 | 256 |
68 | 257 |
22 | 258 security.declareProtected('View','index_html') |
548 | 259 def index_html(self,url,mode="texttool",viewMode="auto",viewLayer=None,tocMode=None,start=1,pn=1): |
464 | 260 """ |
543 | 261 show page |
471 | 262 @param url: url which contains display information |
57 | 263 @param mode: defines how to access the document behind url |
503 | 264 @param viewMode: 'images': display images, 'text': display text, 'xml': display xml, default is 'auto' |
543 | 265 @param viewLayer: sub-type of viewMode, e.g. layer 'dict' for viewMode='text' |
99 | 266 @param tocMode: type of 'table of contents' for navigation (thumbs, text, figures, none) |
464 | 267 """ |
0 | 268 |
503 | 269 logging.debug("documentViewer(index_html) mode=%s url=%s viewMode=%s viewLayer=%s start=%s pn=%s"%(mode,url,viewMode,viewLayer,start,pn)) |
22 | 270 |
271 if not hasattr(self, 'template'): | |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
272 # this won't work |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
273 logging.error("template folder missing!") |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
274 return "ERROR: template folder missing!" |
22 | 275 |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
276 if not getattr(self, 'digilibBaseUrl', None): |
132 | 277 self.digilibBaseUrl = self.findDigilibUrl() or "http://digilib.mpiwg-berlin.mpg.de/digitallibrary" |
22 | 278 |
548 | 279 # mode=filepath should not have toc-thumbs |
280 if tocMode is None: | |
281 if mode == "filepath": | |
282 tocMode = "none" | |
283 else: | |
284 tocMode = "thumbs" | |
285 | |
543 | 286 # docinfo: information about document (cached) |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
516
diff
changeset
|
287 docinfo = self.getDocinfo(mode=mode,url=url,tocMode=tocMode) |
97 | 288 |
543 | 289 # userinfo: user settings (cached) |
290 userinfo = self.getUserinfo() | |
291 | |
471 | 292 # auto viewMode: text if there is a text else images |
455 | 293 if viewMode=="auto": |
530 | 294 if docinfo.get('textURLPath', None): |
295 # docinfo.get('textURL', None) not implemented yet | |
471 | 296 viewMode = "text" |
543 | 297 if viewLayer is None and 'viewLayer' not in userinfo: |
298 # use layer dict as default | |
508 | 299 viewLayer = "dict" |
51
c5d3aabbf61b
textviewer now integrated, new modus auto introduced as standard for viewing
dwinter
parents:
50
diff
changeset
|
300 else: |
471 | 301 viewMode = "images" |
90
6a4a72033d58
new version with new full-text infrastructure and some more changed templates
casties
parents:
84
diff
changeset
|
302 |
474 | 303 elif viewMode == "text_dict": |
304 # legacy fix | |
305 viewMode = "text" | |
503 | 306 viewLayer = "dict" |
474 | 307 |
543 | 308 # safe viewLayer in userinfo |
309 userinfo['viewLayer'] = viewLayer | |
548 | 310 |
543 | 311 # pageinfo: information about page (not cached) |
312 pageinfo = self.getPageinfo(start=start, current=pn, docinfo=docinfo, userinfo=userinfo, viewMode=viewMode, viewLayer=viewLayer, tocMode=tocMode) | |
469 | 313 |
475 | 314 # get template /template/viewer_$viewMode |
315 pt = getattr(self.template, 'viewer_%s'%viewMode, None) | |
316 if pt is None: | |
317 logging.error("No template for viewMode=%s!"%viewMode) | |
318 # TODO: error page? | |
319 return "No template for viewMode=%s!"%viewMode | |
320 | |
455 | 321 # and execute with parameters |
471 | 322 return pt(docinfo=docinfo, pageinfo=pageinfo) |
0 | 323 |
525 | 324 def getAvailableLayers(self): |
325 """returns dict with list of available layers per viewMode""" | |
326 return self.availableLayers | |
327 | |
387 | 328 def getBrowser(self): |
329 """getBrowser the version of browser """ | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
330 bt = browserCheck(self) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
331 logging.debug("BROWSER VERSION: %s"%(bt)) |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
332 return bt |
387 | 333 |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
334 def findDigilibUrl(self): |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
335 """try to get the digilib URL from zogilib""" |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
336 url = self.template.zogilib.getDLBaseUrl() |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
337 return url |
480 | 338 |
339 def getScalerUrl(self, fn=None, pn=None, dw=100, dh=100, docinfo=None): | |
340 """returns URL to digilib Scaler with params""" | |
341 url = None | |
342 if docinfo is not None: | |
343 url = docinfo.get('imageURL', None) | |
344 | |
345 if url is None: | |
346 url = "%s/servlet/Scaler?"%self.digilibBaseUrl | |
347 if fn is None and docinfo is not None: | |
348 fn = docinfo.get('imagePath','') | |
349 | |
350 url += "fn=%s"%fn | |
351 | |
352 if pn: | |
353 url += "&pn=%s"%pn | |
354 | |
355 url += "&dw=%s&dh=%s"%(dw,dh) | |
356 return url | |
126 | 357 |
358 def getDocumentViewerURL(self): | |
359 """returns the URL of this instance""" | |
360 return self.absolute_url() | |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
361 |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
362 def getStyle(self, idx, selected, style=""): |
480 | 363 """returns a string with the given style and append 'sel' if idx == selected.""" |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
364 #logger("documentViewer (getstyle)", logging.INFO, "idx: %s selected: %s style: %s"%(idx,selected,style)) |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
365 if idx == selected: |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
366 return style + 'sel' |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
367 else: |
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
368 return style |
74 | 369 |
478 | 370 def getParams(self, param=None, val=None, params=None, duplicates=None): |
469 | 371 """returns dict with URL parameters. |
372 | |
373 Takes URL parameters and additionally param=val or dict params. | |
374 Deletes key if value is None.""" | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
375 # copy existing request params |
469 | 376 newParams=self.REQUEST.form.copy() |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
377 # change single param |
25 | 378 if param is not None: |
31 | 379 if val is None: |
469 | 380 if newParams.has_key(param): |
381 del newParams[param] | |
25 | 382 else: |
469 | 383 newParams[param] = str(val) |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
384 |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
385 # change more params |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
386 if params is not None: |
478 | 387 for (k, v) in params.items(): |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
388 if v is None: |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
389 # val=None removes param |
469 | 390 if newParams.has_key(k): |
391 del newParams[k] | |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
392 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
393 else: |
469 | 394 newParams[k] = v |
478 | 395 |
396 if duplicates: | |
397 # eliminate lists (coming from duplicate keys) | |
398 for (k,v) in newParams.items(): | |
399 if isinstance(v, list): | |
400 if duplicates == 'comma': | |
401 # make comma-separated list of non-empty entries | |
402 newParams[k] = ','.join([t for t in v if t]) | |
403 elif duplicates == 'first': | |
404 # take first non-empty entry | |
405 newParams[k] = [t for t in v if t][0] | |
406 | |
469 | 407 return newParams |
408 | |
478 | 409 def getLink(self, param=None, val=None, params=None, baseUrl=None, paramSep='&', duplicates='comma'): |
469 | 410 """returns URL to documentviewer with parameter param set to val or from dict params""" |
478 | 411 urlParams = self.getParams(param=param, val=val, params=params, duplicates=duplicates) |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
412 # quote values and assemble into query string (not escaping '/') |
514 | 413 ps = paramSep.join(["%s=%s"%(k, urllib.quote_plus(utf8ify(v), '/')) for (k, v) in urlParams.items()]) |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
414 if baseUrl is None: |
469 | 415 baseUrl = self.getDocumentViewerURL() |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
416 |
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
417 url = "%s?%s"%(baseUrl, ps) |
25 | 418 return url |
419 | |
478 | 420 def getLinkAmp(self, param=None, val=None, params=None, baseUrl=None, duplicates='comma'): |
68 | 421 """link to documentviewer with parameter param set to val""" |
478 | 422 return self.getLink(param=param, val=val, params=params, baseUrl=baseUrl, paramSep='&', duplicates=duplicates) |
81
fae97f071724
fixed problem with info.xml when url without index.meta
casties
parents:
79
diff
changeset
|
423 |
526 | 424 |
425 def setAvailableLayers(self, newLayerString=None): | |
426 """sets availableLayers to newLayerString or tries to autodetect available layers. | |
427 assumes layer templates have the form layer_{m}_{l} for layer l in mode m. | |
428 newLayerString is parsed as JSON.""" | |
429 if newLayerString is not None: | |
430 try: | |
431 layers = json.loads(newLayerString) | |
432 if 'text' in layers and 'images' in layers: | |
433 self.availableLayers = layers | |
434 return | |
435 except: | |
436 pass | |
437 | |
438 logging.error("invalid layers=%s! autodetecting..."%repr(newLayerString)) | |
439 | |
440 # start with builtin layers | |
441 self.availableLayers = self.builtinLayers.copy() | |
442 # add layers from templates | |
443 for t in self.template: | |
444 if t.startswith('layer_'): | |
445 try: | |
446 (x, m, l) = t.split('_', 3) | |
447 if m not in self.availableLayers: | |
448 # mode m doesn't exist -> new list | |
449 self.availableLayers[m] = [l] | |
450 | |
451 else: | |
452 # m exists -> append | |
453 if l not in self.availableLayers[m]: | |
454 self.availableLayers[m].append() | |
455 | |
456 except: | |
457 pass | |
458 | |
459 def getAvailableLayersJson(self): | |
460 """returns available layers as JSON string.""" | |
461 return json.dumps(self.availableLayers) | |
462 | |
469 | 463 |
57 | 464 def getInfo_xml(self,url,mode): |
465 """returns info about the document as XML""" | |
466 if not self.digilibBaseUrl: | |
514 | 467 self.digilibBaseUrl = self.findDigilibUrl() or "http://digilib.mpiwg-berlin.mpg.de/digitallibrary" |
57 | 468 |
469 docinfo = self.getDocinfo(mode=mode,url=url) | |
470 pt = getattr(self.template, 'info_xml') | |
471 return pt(docinfo=docinfo) | |
472 | |
528 | 473 def getAuthenticatedUser(self, anon=None): |
474 """returns the authenticated user object or None. (ignores Zopes anonymous user)""" | |
475 user = getSecurityManager().getUser() | |
476 if user is not None and user.getUserName() != "Anonymous User": | |
477 return user | |
478 else: | |
479 return anon | |
480 | |
35 | 481 def isAccessible(self, docinfo): |
32 | 482 """returns if access to the resource is granted""" |
483 access = docinfo.get('accessType', None) | |
167 | 484 logging.debug("documentViewer (accessOK) access type %s"%access) |
465 | 485 if access == 'free': |
167 | 486 logging.debug("documentViewer (accessOK) access is free") |
32 | 487 return True |
465 | 488 |
45 | 489 elif access is None or access in self.authgroups: |
35 | 490 # only local access -- only logged in users |
528 | 491 user = self.getAuthenticatedUser() |
167 | 492 logging.debug("documentViewer (accessOK) user=%s ip=%s"%(user,self.REQUEST.getClientAddr())) |
528 | 493 return (user is not None) |
32 | 494 |
167 | 495 logging.error("documentViewer (accessOK) unknown access type %s"%access) |
32 | 496 return False |
464 | 497 |
498 | |
543 | 499 def getUserinfo(self): |
500 """returns userinfo object""" | |
501 logging.debug("getUserinfo") | |
502 userinfo = {} | |
503 # look for cached userinfo in session | |
504 if self.REQUEST.SESSION.has_key('userinfo'): | |
505 userinfo = self.REQUEST.SESSION['userinfo'] | |
506 # check if its still current? | |
507 else: | |
508 # store in session | |
509 self.REQUEST.SESSION['userinfo'] = userinfo | |
510 | |
511 return userinfo | |
512 | |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
516
diff
changeset
|
513 def getDocinfo(self, mode, url, tocMode=None): |
464 | 514 """returns docinfo depending on mode""" |
515 logging.debug("getDocinfo: mode=%s, url=%s"%(mode,url)) | |
516 # look for cached docinfo in session | |
517 if self.REQUEST.SESSION.has_key('docinfo'): | |
518 docinfo = self.REQUEST.SESSION['docinfo'] | |
519 # check if its still current | |
520 if docinfo is not None and docinfo.get('mode', None) == mode and docinfo.get('url', None) == url: | |
521 logging.debug("getDocinfo: docinfo in session. keys=%s"%docinfo.keys()) | |
522 return docinfo | |
523 | |
524 # new docinfo | |
525 docinfo = {'mode': mode, 'url': url} | |
526 # add self url | |
527 docinfo['viewerUrl'] = self.getDocumentViewerURL() | |
477 | 528 docinfo['digilibBaseUrl'] = self.digilibBaseUrl |
464 | 529 # get index.meta DOM |
530 docUrl = None | |
531 metaDom = None | |
532 if mode=="texttool": | |
533 # url points to document dir or index.meta | |
534 metaDom = self.metadataService.getDomFromPathOrUrl(url) | |
535 docUrl = url.replace('/index.meta', '') | |
536 if metaDom is None: | |
537 raise IOError("Unable to find index.meta for mode=texttool!") | |
538 | |
539 elif mode=="imagepath": | |
540 # url points to folder with images, index.meta optional | |
541 # asssume index.meta in parent dir | |
542 docUrl = getParentPath(url) | |
543 metaDom = self.metadataService.getDomFromPathOrUrl(docUrl) | |
544 | |
545 elif mode=="filepath": | |
546 # url points to image file, index.meta optional | |
548 | 547 docinfo['imagePath'] = url |
548 docinfo['imageURL'] = self.digilibBaseUrl + "/servlet/Scaler?fn=" + url | |
549 docinfo['numPages'] = 1 | |
464 | 550 # asssume index.meta is two path segments up |
551 docUrl = getParentPath(url, 2) | |
552 metaDom = self.metadataService.getDomFromPathOrUrl(docUrl) | |
553 | |
554 else: | |
555 logging.error("documentViewer (getdocinfo) unknown mode: %s!"%mode) | |
556 raise ValueError("Unknown mode %s! Has to be one of 'texttool','imagepath','filepath'."%(mode)) | |
557 | |
558 docinfo['documentUrl'] = docUrl | |
559 # process index.meta contents | |
473 | 560 if metaDom is not None and metaDom.tag == 'resource': |
464 | 561 # document directory name and path |
562 resource = self.metadataService.getResourceData(dom=metaDom) | |
563 if resource: | |
564 docinfo = self.getDocinfoFromResource(docinfo, resource) | |
565 | |
566 # texttool info | |
567 texttool = self.metadataService.getTexttoolData(dom=metaDom) | |
568 if texttool: | |
569 docinfo = self.getDocinfoFromTexttool(docinfo, texttool) | |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
516
diff
changeset
|
570 # document info (including toc) from full text |
516 | 571 if docinfo.get('textURLPath', None): |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
516
diff
changeset
|
572 docinfo = self.getTextInfo(mode=tocMode, docinfo=docinfo) |
464 | 573 |
574 # bib info | |
575 bib = self.metadataService.getBibData(dom=metaDom) | |
576 if bib: | |
548 | 577 # save extended version as 'bibx' TODO: ugly |
541 | 578 bibx = self.metadataService.getBibData(dom=metaDom, all=True, recursive=1) |
579 if len(bibx) == 1: | |
580 # unwrap list if possible | |
581 bibx = bibx[0] | |
582 | |
583 docinfo['bibx'] = bibx | |
584 docinfo = self.getDocinfoFromBib(docinfo, bib, bibx) | |
465 | 585 else: |
586 # no bib - try info.xml | |
587 docinfo = self.getDocinfoFromPresentationInfoXml(docinfo) | |
464 | 588 |
589 # auth info | |
590 access = self.metadataService.getAccessData(dom=metaDom) | |
591 if access: | |
592 docinfo = self.getDocinfoFromAccess(docinfo, access) | |
593 | |
466 | 594 # attribution info |
595 attribution = self.metadataService.getAttributionData(dom=metaDom) | |
596 if attribution: | |
597 logging.debug("getDocinfo: attribution=%s"%repr(attribution)) | |
598 docinfo['attribution'] = attribution | |
599 | |
600 # copyright info | |
601 copyright = self.metadataService.getCopyrightData(dom=metaDom) | |
602 if copyright: | |
603 logging.debug("getDocinfo: copyright=%s"%repr(copyright)) | |
604 docinfo['copyright'] = copyright | |
538 | 605 |
606 # DRI (permanent ID) | |
607 dri = self.metadataService.getDRI(dom=metaDom, type='escidoc-test') | |
608 if dri: | |
609 logging.debug("getDRI: dri=%s"%repr(dri)) | |
610 docinfo['DRI'] = dri | |
466 | 611 |
464 | 612 # image path |
613 if mode != 'texttool': | |
532 | 614 # override image path from texttool with url parameter TODO: how about mode=auto? |
465 | 615 docinfo['imagePath'] = url.replace('/mpiwg/online/', '', 1) |
464 | 616 |
548 | 617 # check numPages |
618 if docinfo.get('numPages', 0) == 0: | |
619 # number of images from digilib | |
620 if docinfo.get('imagePath', None): | |
532 | 621 docinfo['imageURL'] = self.digilibBaseUrl + "/servlet/Scaler?fn=" + docinfo['imagePath'] |
548 | 622 docinfo = self.getDocinfoFromDigilib(docinfo, docinfo['imagePath']) |
623 else: | |
624 # imagePath still missing? try "./pageimg" | |
625 imgPath = os.path.join(docUrl, 'pageimg') | |
626 docinfo = self.getDocinfoFromDigilib(docinfo, imgPath) | |
627 if docinfo.get('numPages', 0) > 0: | |
628 # there are pages | |
629 docinfo['imagePath'] = imgPath | |
630 docinfo['imageURL'] = self.digilibBaseUrl + "/servlet/Scaler?fn=" + docinfo['imagePath'] | |
464 | 631 |
516 | 632 # check numPages |
633 if docinfo.get('numPages', 0) == 0: | |
634 if docinfo.get('numTextPages', 0) > 0: | |
635 # replace with numTextPages (text-only?) | |
636 docinfo['numPages'] = docinfo['numTextPages'] | |
637 | |
464 | 638 logging.debug("documentViewer (getdocinfo) docinfo: keys=%s"%docinfo.keys()) |
639 #logging.debug("documentViewer (getdocinfo) docinfo: %s"%docinfo) | |
640 # store in session | |
641 self.REQUEST.SESSION['docinfo'] = docinfo | |
642 return docinfo | |
643 | |
516 | 644 |
464 | 645 def getDocinfoFromResource(self, docinfo, resource): |
646 """reads contents of resource element into docinfo""" | |
647 docName = resource.get('name', None) | |
648 docinfo['documentName'] = docName | |
649 docPath = resource.get('archive-path', None) | |
650 if docPath: | |
651 # clean up document path | |
652 if docPath[0] != '/': | |
653 docPath = '/' + docPath | |
654 | |
655 if docName and (not docPath.endswith(docName)): | |
656 docPath += "/" + docName | |
657 | |
658 else: | |
659 # use docUrl as docPath | |
660 docUrl = docinfo['documentURL'] | |
661 if not docUrl.startswith('http:'): | |
662 docPath = docUrl | |
465 | 663 if docPath: |
664 # fix URLs starting with /mpiwg/online | |
665 docPath = docPath.replace('/mpiwg/online', '', 1) | |
666 | |
464 | 667 docinfo['documentPath'] = docPath |
668 return docinfo | |
669 | |
670 def getDocinfoFromTexttool(self, docinfo, texttool): | |
671 """reads contents of texttool element into docinfo""" | |
672 # image dir | |
673 imageDir = texttool.get('image', None) | |
674 docPath = docinfo.get('documentPath', None) | |
675 if imageDir and docPath: | |
676 #print "image: ", imageDir, " archivepath: ", archivePath | |
677 imageDir = os.path.join(docPath, imageDir) | |
678 imageDir = imageDir.replace('/mpiwg/online', '', 1) | |
679 docinfo['imagePath'] = imageDir | |
680 | |
681 # old style text URL | |
682 textUrl = texttool.get('text', None) | |
683 if textUrl and docPath: | |
684 if urlparse.urlparse(textUrl)[0] == "": #keine url | |
685 textUrl = os.path.join(docPath, textUrl) | |
686 | |
687 docinfo['textURL'] = textUrl | |
688 | |
689 # new style text-url-path | |
690 textUrl = texttool.get('text-url-path', None) | |
691 if textUrl: | |
692 docinfo['textURLPath'] = textUrl | |
468 | 693 |
694 # page flow | |
695 docinfo['pageFlow'] = texttool.get('page-flow', 'ltr') | |
696 | |
697 # odd pages are left | |
476 | 698 docinfo['oddPage'] = texttool.get('odd-scan-position', 'left') |
468 | 699 |
541 | 700 # number of title page (default 1) |
701 docinfo['titlePage'] = texttool.get('title-scan-no', 1) | |
464 | 702 |
703 # old presentation stuff | |
704 presentation = texttool.get('presentation', None) | |
705 if presentation and docPath: | |
465 | 706 if presentation.startswith('http:'): |
707 docinfo['presentationUrl'] = presentation | |
708 else: | |
709 docinfo['presentationUrl'] = os.path.join(docPath, presentation) | |
468 | 710 |
464 | 711 return docinfo |
712 | |
541 | 713 def getDocinfoFromBib(self, docinfo, bib, bibx=None): |
464 | 714 """reads contents of bib element into docinfo""" |
465 | 715 logging.debug("getDocinfoFromBib bib=%s"%repr(bib)) |
464 | 716 # put all raw bib fields in dict "bib" |
717 docinfo['bib'] = bib | |
718 bibtype = bib.get('@type', None) | |
719 docinfo['bibType'] = bibtype | |
720 # also store DC metadata for convenience | |
721 dc = self.metadataService.getDCMappedData(bib) | |
536
abd36d4d97b8
new version of index page. improvements for digilib page and thumbnail overview.
casties
parents:
535
diff
changeset
|
722 docinfo['creator'] = dc.get('creator','') |
abd36d4d97b8
new version of index page. improvements for digilib page and thumbnail overview.
casties
parents:
535
diff
changeset
|
723 docinfo['title'] = dc.get('title','') |
abd36d4d97b8
new version of index page. improvements for digilib page and thumbnail overview.
casties
parents:
535
diff
changeset
|
724 docinfo['date'] = dc.get('date','') |
464 | 725 return docinfo |
726 | |
727 def getDocinfoFromAccess(self, docinfo, acc): | |
728 """reads contents of access element into docinfo""" | |
729 #TODO: also read resource type | |
465 | 730 logging.debug("getDocinfoFromAccess acc=%s"%repr(acc)) |
464 | 731 try: |
465 | 732 acctype = acc['@attr']['type'] |
464 | 733 if acctype: |
734 access=acctype | |
735 if access in ['group', 'institution']: | |
736 access = acc['name'].lower() | |
737 | |
738 docinfo['accessType'] = access | |
739 | |
740 except: | |
741 pass | |
742 | |
743 return docinfo | |
744 | |
745 def getDocinfoFromDigilib(self, docinfo, path): | |
746 infoUrl=self.digilibBaseUrl+"/dirInfo-xml.jsp?mo=dir&fn="+path | |
747 # fetch data | |
748 txt = getHttpData(infoUrl) | |
749 if not txt: | |
750 logging.error("Unable to get dir-info from %s"%(infoUrl)) | |
751 return docinfo | |
752 | |
753 dom = ET.fromstring(txt) | |
754 size = getText(dom.find("size")) | |
755 logging.debug("getDocinfoFromDigilib: size=%s"%size) | |
756 if size: | |
757 docinfo['numPages'] = int(size) | |
758 else: | |
759 docinfo['numPages'] = 0 | |
760 | |
761 # TODO: produce and keep list of image names and numbers | |
762 return docinfo | |
763 | |
764 | |
465 | 765 def getDocinfoFromPresentationInfoXml(self,docinfo): |
766 """gets DC-like bibliographical information from the presentation entry in texttools""" | |
767 url = docinfo.get('presentationUrl', None) | |
768 if not url: | |
769 logging.error("getDocinfoFromPresentation: no URL!") | |
770 return docinfo | |
771 | |
772 dom = None | |
773 metaUrl = None | |
774 if url.startswith("http://"): | |
775 # real URL | |
776 metaUrl = url | |
777 else: | |
778 # online path | |
779 | |
780 server=self.digilibBaseUrl+"/servlet/Texter?fn=" | |
781 metaUrl=server+url | |
782 | |
783 txt=getHttpData(metaUrl) | |
784 if txt is None: | |
785 logging.error("Unable to read info.xml from %s"%(url)) | |
786 return docinfo | |
787 | |
788 dom = ET.fromstring(txt) | |
789 docinfo['creator']=getText(dom.find(".//author")) | |
790 docinfo['title']=getText(dom.find(".//title")) | |
791 docinfo['date']=getText(dom.find(".//date")) | |
792 return docinfo | |
793 | |
794 | |
543 | 795 def getPageinfo(self, current=None, start=None, rows=None, cols=None, docinfo=None, userinfo=None, viewMode=None, viewLayer=None, tocMode=None): |
22 | 796 """returns pageinfo with the given parameters""" |
503 | 797 logging.debug("getPageInfo(current=%s, start=%s, rows=%s, cols=%s, viewMode=%s, viewLayer=%s, tocMode=%s)"%(current,start,rows,cols,viewMode,viewLayer,tocMode)) |
22 | 798 pageinfo = {} |
471 | 799 pageinfo['viewMode'] = viewMode |
508 | 800 # split viewLayer if necessary |
801 if isinstance(viewLayer,basestring): | |
802 viewLayer = viewLayer.split(',') | |
803 | |
804 if isinstance(viewLayer, list): | |
805 logging.debug("getPageinfo: viewLayer is list:%s"%viewLayer) | |
806 # save (unique) list in viewLayers | |
807 seen = set() | |
808 viewLayers = [l for l in viewLayer if l and l not in seen and not seen.add(l)] | |
809 pageinfo['viewLayers'] = viewLayers | |
810 # stringify viewLayer | |
811 viewLayer = ','.join(viewLayers) | |
812 else: | |
813 #create list | |
814 pageinfo['viewLayers'] = [viewLayer] | |
815 | |
503 | 816 pageinfo['viewLayer'] = viewLayer |
471 | 817 pageinfo['tocMode'] = tocMode |
818 | |
516 | 819 # TODO: unify current and pn! |
25 | 820 current = getInt(current) |
821 pageinfo['current'] = current | |
480 | 822 pageinfo['pn'] = current |
25 | 823 rows = int(rows or self.thumbrows) |
824 pageinfo['rows'] = rows | |
825 cols = int(cols or self.thumbcols) | |
826 pageinfo['cols'] = cols | |
827 grpsize = cols * rows | |
828 pageinfo['groupsize'] = grpsize | |
476 | 829 # is start is empty use one around current |
61 | 830 start = getInt(start, default=(math.ceil(float(current)/float(grpsize))*grpsize-(grpsize-1))) |
831 # int(current / grpsize) * grpsize +1)) | |
22 | 832 pageinfo['start'] = start |
511 | 833 # get number of pages |
469 | 834 np = int(docinfo.get('numPages', 0)) |
835 if np == 0: | |
518
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
516
diff
changeset
|
836 # try numTextPages |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
516
diff
changeset
|
837 np = docinfo.get('numTextPages', 0) |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
516
diff
changeset
|
838 if np != 0: |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
516
diff
changeset
|
839 docinfo['numPages'] = np |
91051b36b9cc
uses xml info from doc-info.xql for table of contents now.
casties
parents:
516
diff
changeset
|
840 |
511 | 841 # cache table of contents |
842 pageinfo['tocPageSize'] = getInt(self.REQUEST.get('tocPageSize', 30)) | |
469 | 843 pageinfo['numgroups'] = int(np / grpsize) |
844 if np % grpsize > 0: | |
845 pageinfo['numgroups'] += 1 | |
476 | 846 |
847 pageFlowLtr = docinfo.get('pageFlow', 'ltr') != 'rtl' | |
848 oddScanLeft = docinfo.get('oddPage', 'left') != 'right' | |
849 # add zeroth page for two columns | |
850 pageZero = (cols == 2 and (pageFlowLtr != oddScanLeft)) | |
851 pageinfo['pageZero'] = pageZero | |
480 | 852 pageinfo['pageBatch'] = self.getPageBatch(start=start, rows=rows, cols=cols, pageFlowLtr=pageFlowLtr, pageZero=pageZero, minIdx=1, maxIdx=np) |
516 | 853 # more page parameters |
453
beb7ccb92564
first version using elementtree instead of 4suite xml
casties
parents:
405
diff
changeset
|
854 pageinfo['characterNormalization'] = self.REQUEST.get('characterNormalization','reg') |
516 | 855 if docinfo.get('pageNumbers'): |
856 # get original page numbers | |
857 pageNumber = docinfo['pageNumbers'].get(current, None) | |
858 if pageNumber is not None: | |
859 pageinfo['pageNumberOrig'] = pageNumber['no'] | |
860 pageinfo['pageNumberOrigNorm'] = pageNumber['non'] | |
158 | 861 |
511 | 862 # cache search results |
863 pageinfo['resultPageSize'] = getInt(self.REQUEST.get('resultPageSize', 10)) | |
864 query = self.REQUEST.get('query',None) | |
865 pageinfo['query'] = query | |
866 if query: | |
867 queryType = self.REQUEST.get('queryType', 'fulltextMorph') | |
868 pageinfo['queryType'] = queryType | |
869 pageinfo['resultStart'] = getInt(self.REQUEST.get('resultStart', '1')) | |
870 self.getSearchResults(mode=queryType, query=query, pageinfo=pageinfo, docinfo=docinfo) | |
871 | |
872 # highlighting | |
873 highlightQuery = self.REQUEST.get('highlightQuery', None) | |
874 if highlightQuery: | |
875 pageinfo['highlightQuery'] = highlightQuery | |
876 pageinfo['highlightElement'] = self.REQUEST.get('highlightElement', '') | |
877 pageinfo['highlightElementPos'] = self.REQUEST.get('highlightElementPos', '') | |
460 | 878 |
22 | 879 return pageinfo |
460 | 880 |
463 | 881 |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
882 def getPageBatch(self, start=1, rows=10, cols=2, pageFlowLtr=True, pageZero=False, minIdx=1, maxIdx=0): |
480 | 883 """returns dict with array of page informations for one screenfull of thumbnails""" |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
884 batch = {} |
480 | 885 grpsize = rows * cols |
476 | 886 if maxIdx == 0: |
480 | 887 maxIdx = start + grpsize |
476 | 888 |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
889 nb = int(math.ceil(maxIdx / float(grpsize))) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
890 # list of all batch start and end points |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
891 batches = [] |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
892 if pageZero: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
893 ofs = 0 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
894 else: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
895 ofs = 1 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
896 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
897 for i in range(nb): |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
898 s = i * grpsize + ofs |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
899 e = min((i + 1) * grpsize + ofs - 1, maxIdx) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
900 batches.append({'start':s, 'end':e}) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
901 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
902 batch['batches'] = batches |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
903 |
476 | 904 pages = [] |
905 if pageZero and start == 1: | |
906 # correct beginning | |
907 idx = 0 | |
908 else: | |
909 idx = start | |
910 | |
911 for r in range(rows): | |
912 row = [] | |
913 for c in range(cols): | |
914 if idx < minIdx or idx > maxIdx: | |
915 page = {'idx':None} | |
916 else: | |
917 page = {'idx':idx} | |
918 | |
919 idx += 1 | |
920 if pageFlowLtr: | |
921 row.append(page) | |
922 else: | |
923 row.insert(0, page) | |
924 | |
925 pages.append(row) | |
926 | |
480 | 927 if start > 1: |
928 batch['prevStart'] = max(start - grpsize, 1) | |
929 else: | |
930 batch['prevStart'] = None | |
931 | |
526 | 932 if start + grpsize <= maxIdx: |
480 | 933 batch['nextStart'] = start + grpsize |
934 else: | |
935 batch['nextStart'] = None | |
936 | |
937 batch['pages'] = pages | |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
938 return batch |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
939 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
940 def getBatch(self, start=1, size=10, end=0, data=None, fullData=True): |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
941 """returns dict with information for one screenfull of data.""" |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
942 batch = {} |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
943 if end == 0: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
944 end = start + size |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
945 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
946 nb = int(math.ceil(end / float(size))) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
947 # list of all batch start and end points |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
948 batches = [] |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
949 for i in range(nb): |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
950 s = i * size + 1 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
951 e = min((i + 1) * size, end) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
952 batches.append({'start':s, 'end':e}) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
953 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
954 batch['batches'] = batches |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
955 # list of elements in this batch |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
956 this = [] |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
957 j = 0 |
526 | 958 for i in range(start, min(start+size, end+1)): |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
959 if data: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
960 if fullData: |
526 | 961 d = data.get(i, None) |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
962 else: |
526 | 963 d = data.get(j, None) |
482
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
964 j += 1 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
965 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
966 else: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
967 d = i+1 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
968 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
969 this.append(d) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
970 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
971 batch['this'] = this |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
972 if start > 1: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
973 batch['prevStart'] = max(start - size, 1) |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
974 else: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
975 batch['prevStart'] = None |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
976 |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
977 if start + size < end: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
978 batch['nextStart'] = start + size |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
979 else: |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
980 batch['nextStart'] = None |
7ca8ac7db06e
more new template stuff. more batching methods in documentViewer.
casties
parents:
480
diff
changeset
|
981 |
526 | 982 logging.debug("getBatch start=%s size=%s end=%s batch=%s"%(start,size,end,repr(batch))) |
480 | 983 return batch |
476 | 984 |
985 | |
546 | 986 def getAnnotatorGroupsForUser(self, user, annotationServerUrl="http://tuxserve03.mpiwg-berlin.mpg.de/AnnotationManager"): |
987 """returns list of groups {name:*, id:*} on the annotation server for the user""" | |
988 groups = [] | |
989 groupsUrl = "%s/annotator/groups?user=%s"%(annotationServerUrl,user) | |
990 data = getHttpData(url=groupsUrl, noExceptions=True) | |
991 if data: | |
992 res = json.loads(data) | |
993 rows = res.get('rows', None) | |
994 if rows is None: | |
995 return groups | |
996 for r in rows: | |
997 groups.append({'id': r.get('id', None), 'name': r.get('name', None), 'uri': r.get('uri', None)}) | |
998 | |
999 return groups | |
1000 | |
1001 | |
463 | 1002 security.declareProtected('View management screens','changeDocumentViewerForm') |
1003 changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals()) | |
22 | 1004 |
526 | 1005 def changeDocumentViewer(self,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=5,authgroups='mpiwg',availableLayers=None,RESPONSE=None): |
22 | 1006 """init document viewer""" |
1007 self.title=title | |
1008 self.digilibBaseUrl = digilibBaseUrl | |
25 | 1009 self.thumbrows = thumbrows |
1010 self.thumbcols = thumbcols | |
32 | 1011 self.authgroups = [s.strip().lower() for s in authgroups.split(',')] |
463 | 1012 try: |
1013 # assume MetaDataFolder instance is called metadata | |
1014 self.metadataService = getattr(self, 'metadata') | |
1015 except Exception, e: | |
1016 logging.error("Unable to find MetaDataFolder 'metadata': "+str(e)) | |
526 | 1017 |
1018 self.setAvailableLayers(availableLayers) | |
463 | 1019 |
22 | 1020 if RESPONSE is not None: |
1021 RESPONSE.redirect('manage_main') | |
0 | 1022 |
1023 def manage_AddDocumentViewerForm(self): | |
1024 """add the viewer form""" | |
22 | 1025 pt=PageTemplateFile('zpt/addDocumentViewer', globals()).__of__(self) |
0 | 1026 return pt() |
1027 | |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
1028 def manage_AddDocumentViewer(self,id,imageScalerUrl="",textServerName="",title="",RESPONSE=None): |
0 | 1029 """add the viewer""" |
84
a6e4f9b6729a
first version with new full-text infrastructure and slightly changed templates
casties
parents:
83
diff
changeset
|
1030 newObj=documentViewer(id,imageScalerUrl=imageScalerUrl,title=title,textServerName=textServerName) |
0 | 1031 self._setObject(id,newObj) |
1032 | |
1033 if RESPONSE is not None: | |
1034 RESPONSE.redirect('manage_main') |