changeset 25:e93fb8cadd3a

new, less preliminary version 0.2 - lots of stuff rewritten - new template system - still no access control
author casties
date Fri, 07 Apr 2006 20:45:48 +0200
parents 2204e028811b
children dcadb05c8dfb
files documentViewer.py zpt/changeDocumentViewer.zpt zpt/thumbs_main.zpt
diffstat 3 files changed, 95 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/documentViewer.py	Thu Apr 06 19:13:43 2006 +0200
+++ b/documentViewer.py	Fri Apr 07 20:45:48 2006 +0200
@@ -18,6 +18,14 @@
 import urllib
 import zLOG
 
+def getInt(number, default=0):
+    """returns always an int (0 in case of problems)"""
+    try:
+        return int(number)
+    except:
+        return default
+    
+
 def getTextFromNode(nodename):
     nodelist=nodename.childNodes
     rc = ""
@@ -79,20 +87,23 @@
     changeDocumentViewerForm = PageTemplateFile('zpt/changeDocumentViewer', globals())
 
     
-    def __init__(self,id,imageViewerUrl,title="",digilibBaseUrl=None):
+    def __init__(self,id,imageViewerUrl,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10):
         """init document viewer"""
         self.id=id
         self.title=title
         self.imageViewerUrl=imageViewerUrl
-        self.digilibBaseUrl = digilibBaseUrl
-        if not self.digilibBaseUrl:
+        if not digilibBaseUrl:
             self.digilibBaseUrl = self.findDigilibUrl()
+        else:
+            self.digilibBaseUrl = digilibBaseUrl
+        self.thumbcols = thumbcols
+        self.thumbrows = thumbrows
         # add template folder so we can always use template.something
         self.manage_addFolder('template')
 
 
     security.declareProtected('View','index_html')
-    def index_html(self,mode,url,start=0,pn=1):
+    def index_html(self,mode,url,start=None,pn=1):
         '''
         view it
         @param mode: defines which type of document is behind url
@@ -112,71 +123,32 @@
             
         print "dlbaseurl:", self.digilibBaseUrl
 
-        docinfo = self.getDocinfo(mode=mode, url=url)
-        pageinfo = self.getPageinfo(start=start,current=pn)
+        docinfo = self.getDocinfo(mode=mode,url=url)
+        pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)
         pt = getattr(self.template, 'viewer_main')
         return pt(docinfo=docinfo,pageinfo=pageinfo)
   
   
-    def imageLink(self,nr):
-        """link hinter den images"""
-        paramsTmp=cgi.parse_qs(self.REQUEST['QUERY_STRING'])
-        params={}
-        for x in paramsTmp.iteritems():
-                params[x[0]]=x[1][0]
+    def getLink(self,param=None,val=None):
+        """link to documentviewer with parameter param set to val"""
+        params=cgi.parse_qs(self.REQUEST['QUERY_STRING'])
+        if param is not None:
+            if val is None and params.has_key(param):
+                del params[param]
+            else:
+                params[param] = val
+        
+        url=self.REQUEST['URL']+"?"+urllib.urlencode(params, doseq=True)
+        return url
+
     
-        params['pn']=nr
-        newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params)
-        return newUrl
-        
     def getStyle(self, idx, selected, style=""):
-        """returns a string with the given style + 'sel' if path == selected."""
+        """returns a string with the given style and append 'sel' if path == selected."""
         #zLOG.LOG("documentViewer (getstyle)", zLOG.INFO, "idx: %s selected: %s style: %s"%(idx,selected,style))
         if idx == selected:
             return style + 'sel'
         else:
             return style    
-
-            
-    def thumbruler(self,cols,rows,start,maximum):
-        """ruler for thumbs"""
-        ret=""
-        paramsTmp=cgi.parse_qs(self.REQUEST['QUERY_STRING'])
-        params={}
-        for x in paramsTmp.iteritems():
-
-            if not x[0]=="start":
-                params[x[0]]=x[1][0]
-
-        newUrlSelect=self.REQUEST['URL']+"?"+urllib.urlencode(params)    
-        if start>0:
-            newStart=max(start-cols*rows,0)
-            params['start']=newStart
-            newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params)
-            ret+="""<a href="%s">prev</a>"""%newUrl
-
-
-        ret+="""<select onChange="location.href='%s&start='+this.options[this.selectedIndex].value">"""%newUrlSelect
-        nr,rest=divmod(maximum,cols*rows)
-        if rest > 0:
-            nr+=1
-        for i in range(nr):
-            nr=i*cols*rows
-           
-            if (start >= nr) and (start < nr+cols*rows):  
-                ret+="""<option value="%s" selected>%s</option>"""%(nr,nr)
-            else:
-                ret+="""<option value="%s">%s</option>"""%(nr,nr)
-        ret+="</select>"
-        
-        if start<maximum:
-            newStart=min(start+cols*rows,maximum)
-            params['start']=newStart
-            newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params)
-            ret+="""<a href="%s">next</a>"""%newUrl
-        
-        return ret
-    
         
         
     def getBibinfoFromIndexMeta(self,path,docinfo=None,dom=None):
@@ -186,7 +158,6 @@
         if docinfo is None:
             docinfo = {}
             
-        metaData=self.metadata.main.meta.bib
         if dom is None:
             server="http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary/servlet/Texter?fn="
             path="/".join(path.split("/")[0:-1])
@@ -196,17 +167,18 @@
             except:
                 return docinfo
         
-        type=dom.xpath("//bib/@type")
-        if type and (len(type)>0):
-            type=type[0].value
+        metaData=self.metadata.main.meta.bib
+        bibtype=dom.xpath("//bib/@type")
+        if bibtype and (len(bibtype)>0):
+            bibtype=bibtype[0].value
         else:
-            type="generic"
-        type=type.replace("-"," ")# wrong typesiin index meta "-" instead of " "
-        hash=metaData.generateMappingForType(type)
-
-        docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%hash['author'][0])[0])
-        docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%hash['title'][0])[0])
-        docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%hash['year'][0])[0])
+            bibtype="generic"
+        bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC)
+        bibmap=metaData.generateMappingForType(bibtype)
+        if len(bibmap) > 0:
+            docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0])
+            docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0])
+            docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0])
         
         return docinfo
 
@@ -242,7 +214,7 @@
            image=image.replace("/mpiwg/online",'')
            pt=getParamFromDigilib(image,'size')
            docinfo['imagePath'] = image
-           docinfo['numberOfPages'] = pt
+           docinfo['numPages'] = pt
            
        viewerUrls=dom.xpath("//texttool/digiliburlprefix")
        
@@ -268,7 +240,7 @@
         docinfo['imagePath'] = path
         path=path.replace("/mpiwg/online","")
         pt=getParamFromDigilib(path,'size')
-        docinfo['numberOfPages'] = pt
+        docinfo['numPages'] = pt
         imageUrl=genericDigilib+"/servlet/Scaler?fn=%s"%path
         docinfo['imageURL'] = imageUrl
         
@@ -299,11 +271,28 @@
         return docinfo
         
         
-    def getPageinfo(self, start, current):
+    def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None):
         """returns pageinfo with the given parameters"""
         pageinfo = {}
+        current = getInt(current)
+        pageinfo['current'] = current
+        rows = int(rows or self.thumbrows)
+        pageinfo['rows'] = rows
+        cols = int(cols or self.thumbcols)
+        pageinfo['cols'] = cols
+        grpsize = cols * rows
+        pageinfo['groupsize'] = grpsize
+        start = getInt(start, default=(int(current / grpsize) * grpsize +1))
+        print "start3:", start
         pageinfo['start'] = start
-        pageinfo['current'] = current
+        pageinfo['end'] = start + grpsize
+        if docinfo is not None:
+            np = int(docinfo['numPages'])
+            pageinfo['end'] = min(pageinfo['end'], np)
+            pageinfo['numgroups'] = int(np / grpsize)
+            if np % grpsize > 0:
+                pageinfo['numgroups'] += 1
+                
         return pageinfo
                 
     def text(self,mode,url,pn):
@@ -360,22 +349,6 @@
 #        return list
 #
 
-    def image(self,mode,url,pn):
-        """give image out"""
-        if mode=="texttool": #index.meta with texttool information
-            (viewerUrl,imagepath,textpath)=parseUrlTextTool(url)
-            if not viewerUrl:
-                viewerUrl=self.imageViewerUrl
-            url=viewerUrl+"pn=%s&fn=%s"%(pn,imagepath[0])
-            ret="""<iframe height="100%%" width="100%%" src="%s"/>"""%url
-            return url
-        elif mode=="imagepath":
-            url=url.replace("/mpiwg/online","")
-            url=self.imageViewerUrl+"pn=%s&fn=%s"%(pn,url)
-            ret="""<iframe height="100%%" width="100%%" src="%s"/>"""%url
-            return url
-            
-        
     def findDigilibUrl(self):
         """try to get the digilib URL from zogilib"""
         url = self.imageViewerUrl[:-1] + "/getScalerUrl"
@@ -385,11 +358,13 @@
         except:
             return None
     
-    def changeDocumentViewer(self,imageViewerUrl,title="",digilibBaseUrl=None,RESPONSE=None):
+    def changeDocumentViewer(self,imageViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,RESPONSE=None):
         """init document viewer"""
         self.title=title
         self.imageViewerUrl=imageViewerUrl
         self.digilibBaseUrl = digilibBaseUrl
+        self.thumbrows = thumbrows
+        self.thumbcols = thumbcols
         
         if RESPONSE is not None:
             RESPONSE.redirect('manage_main')
--- a/zpt/changeDocumentViewer.zpt	Thu Apr 06 19:13:43 2006 +0200
+++ b/zpt/changeDocumentViewer.zpt	Fri Apr 07 20:45:48 2006 +0200
@@ -11,6 +11,10 @@
         <p class="form-element"><input size="80" tal:attributes="value here/digilibBaseUrl" name="digilibBaseUrl"></p>
         <p class="form-text">This information can be automatically detected if the image viewer above is a Zogilib instance.<br/>
         You must clear this field when you change the image viewer URL above.</p>
+        <p class="form-optional">Thumbnail rows</p>
+        <p class="form-element"><input size="3" tal:attributes="value here/thumbrows | default" name="thumbrows"></p>
+        <p class="form-optional">Thumbnail columns</p>
+        <p class="form-element"><input size="3" tal:attributes="value here/thumbcols | default" name="thumbcols"></p>
 		
 		<p><input type="submit" value="change"></p>
 		</form>
--- a/zpt/thumbs_main.zpt	Thu Apr 06 19:13:43 2006 +0200
+++ b/zpt/thumbs_main.zpt	Fri Apr 07 20:45:48 2006 +0200
@@ -1,24 +1,30 @@
-<div tal:define="cols python:2; rows python:10; docinfo python:options['docinfo']; pageinfo python:options['pageinfo']; start python:pageinfo['start']; end python:min(start+cols*rows,int(docinfo['numberOfPages'])); current pageinfo/current; rowsCorrected python:divmod(end-start,cols);">
-<span tal:replace="structure python:here.thumbruler(cols,rows,start,int(docinfo['numberOfPages']))"/>
-
+<div tal:define="docinfo options/docinfo; pageinfo options/pageinfo;  
+  start pageinfo/start; end pageinfo/end; rows pageinfo/rows; cols pageinfo/cols;
+  current pageinfo/current; grpsize pageinfo/groupsize">
+<div class="thumbruler">
+  <span tal:condition="python:(start>1)">
+    <a tal:attributes="href python:here.getLink(param='start',val=max(start-grpsize,1))">&lt;</a>
+  </span>
+  <select tal:attributes="onChange python:'location.href=\'%s&start=\'+this.options[this.selectedIndex].value'%here.getLink(param='start',val=None)">
+    <option tal:repeat="grp python:range(pageinfo['numgroups'])" 
+      tal:attributes="selected python:(start==grp*grpsize+1); value python:(grp*grpsize+1)"
+      tal:content="python:(grp*grpsize+1)"/>
+  </select>
+  <span tal:condition="python:(start+grpsize<docinfo['numPages'])">
+    <a tal:attributes="href python:here.getLink(param='start',val=start+grpsize)">&gt;</a>
+  </span>
+</div>
 <table>
-	<tr tal:repeat="i python:range(rowsCorrected[0])">
-		<td align="center" tal:repeat="j python:range(start+i*cols,start+(i+1)*cols)" 
-			tal:attributes="class python:here.getStyle(str(j+1),current,'thumb')">
-		<a tal:attributes="href python:here.imageLink(j+1)">
-		 <img class="thumbimg" border="0" tal:attributes="src python:docinfo['imageURL']+'&pn=%i&dw=100&dh=100'%(j+1)"/>
-		 <div class="thumbcap" tal:content="python:(j+1)"/>
+  <tr tal:repeat="row python:range(rows)">
+    <tal:block tal:repeat="idx python:range(start+row*cols,start+(row+1)*cols)">
+      <td align="center" tal:condition="python:(idx <= end)"
+		  	tal:attributes="class python:here.getStyle(idx,current,'thumb')">
+		<a tal:attributes="href python:here.getLink(param='pn',val=idx)">
+		  <img class="thumbimg" border="0" tal:attributes="src python:'%s&pn=%d&dw=100&dh=100'%(docinfo['imageURL'],idx)"/>
+		  <div class="thumbcap" tal:content="idx"/>
 		</a>
-		</td>
-	</tr>
-	<tr tal:condition="python:rowsCorrected[1]>0">
-		<td align="center" tal:repeat="j python:range(start+cols*rowsCorrected[0],int(docinfo['numberOfPages']))" 
-			tal:attributes="class python:here.getStyle(str(j+1),current,'thumb')">
-		<a tal:attributes="href python:here.imageLink(j+1)">
-		 <img class="thumbimg" border="0" tal:attributes="src python:docinfo['imageURL']+'&pn=%i&dw=100&dh=100'%(j+1)"/>
-		 <div class="thumbcap" tal:content="python:(j+1)"/>
-		 </a>
-		</td>
-	</tr>
+	  </td>
+    </tal:block>
+  </tr>
 </table>
 </div>