changeset 1:e4bae49e657b

more renovation
author casties
date Mon, 25 Jul 2011 20:59:15 +0200
parents 9f9d9be26e53
children ac8e119b25ec
files MetaData.py MetaDataMapping.py SrvTxtUtils.py zpt/addMetadataMappingForm.zpt zpt/changeFieldOrder.zpt zpt/changeMetadata.zpt zpt/changeMetadataMapping.zpt
diffstat 7 files changed, 243 insertions(+), 121 deletions(-) [+]
line wrap: on
line diff
--- a/MetaData.py	Mon Jul 25 16:50:48 2011 +0200
+++ b/MetaData.py	Mon Jul 25 20:59:15 2011 +0200
@@ -39,6 +39,20 @@
     bt = bt.strip().replace(' ', '-').lower()
     return bt
 
+def getBibdataFromDom(dom):
+    """returns dict with all elements from bib-tag"""
+    bibinfo = {}
+    bib = dom.find(".//meta/bib")
+    if bib is not None:
+        # put type in @type
+        type = bib.get('type')
+        bibinfo['@type'] = normalizedBibtype(type)
+        # put all subelements in dict
+        for e in bib:
+            bibinfo[e.tag] = getText(e)
+            
+    return bibinfo
+
 def toString(list):
     ret=u""
     
@@ -76,19 +90,6 @@
     
     return ret
         
-def getBibdataFromDom(dom):
-    """returns dict with all elements from bib-tag"""
-    bibinfo = {}
-    bib = dom.find(".//meta/bib")
-    if bib is not None:
-        # put type in @type
-        type = bib.get('type')
-        bibinfo['@type'] = normalizedBibtype(type)
-        # put all subelements in dict
-        for e in bib:
-            bibinfo[e.tag] = getText(e)
-            
-    return bibinfo
 
                
 
@@ -272,9 +273,8 @@
         return ret
 
     
-    def getStdMappedHash(self, bibdata):
-        """returns dict with metadata from bibdata mapped according to standard mapping"""
-        mdHash={}
+    def getBibFields(self, bibdata):
+        """returns dict with metadata description for bibdata"""
         bibtype = bibdata['@type']
         # get mapping from main/meta/bib
         try:
@@ -283,17 +283,12 @@
             logging.error("getStdMappedHash: no mapping for type: %s"%bibtype)
             return mdHash
             
-        mds = mapping.generateMappingHash() # Hole  das Mapping generisches Feld --> Feld im entsprechenden Typ
+        # get field descriptions
+        bibFields = mapping.getFields.copy()
+        # add field list
+        bibFields['@fieldList'] = mapping.getFieldList()
         
-        for field in mds.keys():
-            # get mapped field name
-            mf = mds[field][0]
-            if not mf:
-                continue
-            logging.debug("mapping: %s = %s"%(field,mf))
-            mdHash[field] = bibdata.get(mf, '')
-            
-        return mdHash
+        return bibFields
 
     
     def getFormatted(self, template, path=None, dom=None, bibdata=None):
@@ -324,10 +319,10 @@
                     logging.error("getFormatted: no generic template either: %s"%(template))
                     return ""
             
-            # put mapped data in mdHash        
-            mdHash = self.getStdMappedHash(bibdata)
+            # put bib field descriptions in mdHash        
+            bibFields = self.getBibFields(bibdata)
                 
-            return tp(stdmd=mdHash, md=bibdata)
+            return tp(bibFields=bibFields, md=bibdata)
 
                 
     def getFormattedMetaData(self, path=None, dom=None, bibdata=None):
@@ -395,25 +390,3 @@
     if RESPONSE is not None:
         RESPONSE.redirect('manage_main')
         
-class MetaDataMapping(OSAS_MetadataMapping):
-    meta_type="MetadataMapping"
-    
-def manage_addMetaDataMappingForm(self):
-    """interface for adding the OSAS_root"""
-    pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMetadataMappingForm.zpt')).__of__(self)
-
-    return pt()
-
-def manage_addMetaDataMapping(self,idOfObject,titleOfObject,RESPONSE=None):
-    """add the OSAS_root"""
-    
-    argList={}
-    for arg in self.fieldList:
-        if not (arg in ['idOfObject','titleOfObject']):
-            argList[arg]=(self.REQUEST.form[arg],self.REQUEST.form['label_'+arg],self.REQUEST.form['explanation_'+arg],self.REQUEST.form['status_'+arg],self.REQUEST.form['values_'+arg])
-            
-    newObj=MetaDataMapping(idOfObject,titleOfObject,argList)
-    self._setObject(idOfObject,newObj)
-    if RESPONSE is not None:
-        RESPONSE.redirect('manage_main')
-    
--- a/MetaDataMapping.py	Mon Jul 25 16:50:48 2011 +0200
+++ b/MetaDataMapping.py	Mon Jul 25 20:59:15 2011 +0200
@@ -1,4 +1,5 @@
 from OFS.Folder import Folder
+from OFS.SimpleItem import SimpleItem
 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
 from Globals import package_home
 from AccessControl import ClassSecurityInfo
@@ -18,13 +19,16 @@
 #from Products.OSA_system2.OSAS_metadata import OSAS_Metadata,OSAS_MetadataMapping
 from OSAS_metadata import OSAS_MetadataMapping
 
+from SrvTxtUtils import getAt
+
 
 class MetaDataMapping(OSAS_MetadataMapping):
     """Metadata mapping object representing a fixed set of sub-elements."""
     
     meta_type="MetadataMapping"
     manage_options = SimpleItem.manage_options+(
-        {'label':'Main Config','action':'changeMetadataMappingForm'},
+        {'label':'Main Config','action':'manage_changeMetaDataMappingForm'},
+        {'label':'Field order','action':'manage_changeFieldOrderForm'},
         )
     
     fields = {}
@@ -33,7 +37,7 @@
     fieldList = []
     """list of element names in preferred order"""
     
-    def __init__(self,id,title,fields):
+    def __init__(self,id,title,fields,fieldList=None):
         """init
         @param id: id
         @param title: title fuer zope navigation
@@ -44,21 +48,108 @@
         for fieldName in fields.keys():
             self.fields[fieldName] = fields[fieldName]
         
-           
+        if fieldList is not None:
+            self.fieldList = fieldList
+        else:
+            # create fieldList
+            self.fieldList = fields.keys()
+    
+
+    def getFields(self):
+        """returns dict of field names and field description dicts"""
+        return fields
+    
+    def getFieldList(self):
+        """returns ordered list of fields"""
+        return fieldList
+    
+    
+    
+    
+    
+    
+    
+    
+    # convenience method
+    def getAt(self, array, idx, default=None):
+        """returns element from array or default"""
+        return getAt(array, idx, default)
 
     
+    ## editing forms and methods
     
+    manage_changeFieldOrderForm = PageTemplateFile('zpt/changeFieldOrder', globals())
+    
+    def manage_changeFieldOrder(self, fieldList=None, RESPONSE=None):
+        """change the order of fields"""
+        if fieldList is not None:
+            newFieldList = []
+            for f in fieldList.split(','):
+                newFieldList.append(f.strip())
+                
+            # check
+            if len(newFieldList) != len(self.fields.keys()):
+                raise ValueError("Incorrect length of new field list!")
+            
+            for f in newFieldList:
+                if f not in self.fields:
+                    raise ValueError("Unknown field '%s'!"%f)
+
+            self.fieldList = newFieldList
+
+        if RESPONSE is not None:
+            RESPONSE.redirect('manage_main')
+        
+    manage_changeMetaDataMappingForm = PageTemplateFile('zpt/changeMetadataMapping', globals())
+    
+    def manage_changeMetaDataMapping(self,titleOfObject,RESPONSE=None):
+        """change the MetapdataMapping"""
+        fields={}
+        fieldList=[]
+        for idx in range(0,20):
+            tag = self.REQUEST.form.get('field_'+str(idx), None)
+            if tag:
+                desc = {'tag': tag}
+                desc['label'] = self.REQUEST.form.get('label_'+str(idx), None)
+                desc['explanation'] = self.REQUEST.form.get('explanation_'+str(idx), None)
+                desc['status'] = self.REQUEST.form.get('status_'+str(idx), None)
+                desc['valuelist'] = self.REQUEST.form.get('valuelist_'+str(idx), None)
+                desc['dcmap'] = self.REQUEST.form.get('dcmap'+str(idx), None)
+                logging.debug("changeMetaDataMapping: desc=%s"%repr(desc))
+                fields[tag] = desc
+                fieldList.append(tag)
+                #(self.REQUEST.form[arg],self.REQUEST.form['label_'+arg],self.REQUEST.form['explanation_'+arg],self.REQUEST.form['status_'+arg],self.REQUEST.form['values_'+arg])
+            
+        # replace data with new
+        self.title = titleOfObject
+        self.fields = fields
+        self.fieldList = fieldList       
+    
+        if RESPONSE is not None:
+            RESPONSE.redirect('manage_main')
+
     
 manage_addMetaDataMappingForm = PageTemplateFile('zpt/addMetadataMappingForm', globals())
 
 def manage_addMetaDataMapping(self,idOfObject,titleOfObject,RESPONSE=None):
     """add the MetapdataMapping"""
-    argList={}
-    for arg in self.fieldList:
-        if not (arg in ['idOfObject','titleOfObject']):
-            argList[arg]=(self.REQUEST.form[arg],self.REQUEST.form['label_'+arg],self.REQUEST.form['explanation_'+arg],self.REQUEST.form['status_'+arg],self.REQUEST.form['values_'+arg])
+    fields={}
+    fieldList=[]
+    for idx in range(1,20):
+        tag = self.REQUEST.form.get('field_'+str(idx), None)
+        if tag:
+            desc = {'tag': tag}
+            desc['label'] = self.REQUEST.form.get('label_'+str(idx), None)
+            desc['explanation'] = self.REQUEST.form.get('explanation_'+str(idx), None)
+            desc['status'] = self.REQUEST.form.get('status_'+str(idx), None)
+            desc['valuelist'] = self.REQUEST.form.get('valuelist_'+str(idx), None)
+            desc['dcmap'] = self.REQUEST.form.get('dcmap'+str(idx), None)
+            logging.debug("addMetaDataMapping: desc=%s"%repr(desc))
+            fields[tag] = desc
+            fieldList.append(tag)
+            #(self.REQUEST.form[arg],self.REQUEST.form['label_'+arg],self.REQUEST.form['explanation_'+arg],self.REQUEST.form['status_'+arg],self.REQUEST.form['values_'+arg])
             
-    newObj=MetaDataMapping(idOfObject,titleOfObject,argList)
+    newObj=MetaDataMapping(idOfObject,titleOfObject,fields,fieldList)
     self._setObject(idOfObject,newObj)
     if RESPONSE is not None:
         RESPONSE.redirect('manage_main')
--- a/SrvTxtUtils.py	Mon Jul 25 16:50:48 2011 +0200
+++ b/SrvTxtUtils.py	Mon Jul 25 20:59:15 2011 +0200
@@ -6,7 +6,7 @@
 import logging
 
 
-srvTxtUtilsVersion = "1.0"
+srvTxtUtilsVersion = "1.1"
 
 def getInt(number, default=0):
     """returns always an int (0 in case of problems)"""
@@ -15,6 +15,13 @@
     except:
         return int(default)
 
+def getAt(array, idx, default=None):
+    """returns element idx from array or default (in case of problems)"""
+    try:
+        return array[idx]
+    except:
+        return default
+
 def getText(node):
     """returns all text content of a node and its subnodes"""
     if node is None:
--- a/zpt/addMetadataMappingForm.zpt	Mon Jul 25 16:50:48 2011 +0200
+++ b/zpt/addMetadataMappingForm.zpt	Mon Jul 25 20:59:15 2011 +0200
@@ -1,57 +1,45 @@
-<html>
-  <body>
-    <h2>Add a Metadata Mapping</h2>
-    <form name="form" action="manage_addMetaDataMapping" method="post">
-      <b> ID: </b><input type="text" name="idOfObject"><br><br>
-      <b> Title (=Reference Type): </b><input type="text" name="titleOfObject"><br><br>
-      <ul>
-	<li> <b>Tag-Name:</b> Should be lower case (e.g. author, number-of-pages, ...)
-	<li> <b>Label: </b> Human readable name for displaying purposes (Author, Number of Pages, ...)
-    <li> <b>Explanation:</b> Should contain a help text, what and how to enter in this field. Can contain HTML tags.
-     </ul>
-      <table border=1>
-	<tr>
-	  <th>
-	    Tag-Name
-	  </th>
-	  <th>
-	    Label
-	  </th>
-	  <th>
-	    Explanation
-	  </th>
-  	  <th>	  
-	    Optional
-	  </th>
-  	  <th>	  
-	    Valuelist
-	  </th>
-	</tr>
-      <tr tal:repeat="field here/getFields">
-	<td valign="top">
-	  <b tal:content="field"/>
-	</td>
-	<td valign="top">
-	  <input type="text" tal:attributes="name field" size=20>
-	</td>
-	<td valign="top">
-	  <input type="text" tal:attributes="name python:'label_'+field" size=20>
-	</td>
-	<td valign="top">
-	  <textarea rows="10" cols="40" tal:attributes="name python:'explanation_'+field" size=20></textarea>
-	</td>
-	<td valign="top">
-	  <input type="radio" tal:attributes="name python:'status_'+field" value="optional" checked>
-	</td>
-	<td valign="top">	
-	  <input type="radio" tal:attributes="name python:'status_'+field" value="required">
-	</td>
-	<td valign="top">
-	  <textarea rows="10" cols="15" tal:attributes="name python:'values_'+field" size=20></textarea>
-	</td>
-      </tr>
-      </table>
-      <input type="submit" value="Add"><br><br>
-    </form>
-  </body>
-</html>
+
+<div tal:replace="structure here/manage_page_header">Header</div>
+<h2>Add a Metadata Mapping</h2>
+<form name="form" action="manage_addMetaDataMapping" method="post">
+  <b> ID: </b><input type="text" name="idOfObject"><br/> <br/> <b>
+    Title (=Reference Type): </b><input type="text" name="titleOfObject"><br/> <br/>
+  <ul>
+    <li><b>Tag-Name:</b> Should be lower case (e.g. author, number-of-pages, ...)</li>
+    <li><b>Label:</b> Human readable name for displaying purposes (Author, Number of
+      Pages, ...)</li>
+    <li><b>Explanation:</b> Should contain a help text, what and how to enter in this
+      field. Can contain HTML tags.</li>
+  </ul>
+  <table border=1>
+    <tr>
+      <th>Tag-name</th>
+      <th>Label</th>
+      <th>Explanation</th>
+      <th>Optional</th>
+      <th>Valuelist</th>
+      <th>DC-map</th>
+    </tr>
+    <tr tal:repeat="idx python:range(0,20)">
+      <td valign="top">
+        <input type="text" tal:attributes="name string:field_$idx" size="10" />
+      </td>
+      <td valign="top">
+        <input type="text" tal:attributes="name string:label_$idx" size="20" />
+      </td>
+      <td valign="top">
+        <textarea rows="10" cols="40" tal:attributes="name string:explanation_$idx" size="20"></textarea>
+      </td>
+      <td valign="top">
+        <input type="checkbox" tal:attributes="name string:status_$idx" value="optional" checked="checked" />
+      </td>
+      <td valign="top">
+        <textarea rows="10" cols="15" tal:attributes="name string:valuelist_$idx" size="20"></textarea>
+      </td>
+      <td valign="top">
+        <input type="text" tal:attributes="name string:dcmap_$idx" size="10" />
+      </td>
+    </tr>
+  </table>
+  <input type="submit" value="Add"><br />
+</form>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zpt/changeFieldOrder.zpt	Mon Jul 25 20:59:15 2011 +0200
@@ -0,0 +1,11 @@
+<div tal:replace="structure here/manage_page_header">Header</div>
+<!-- ZOPE management tabs -->
+<div tal:define="manage_tabs_message options/manage_tabs_message | nothing"
+  tal:replace="structure here/manage_tabs">Tabs</div>
+<!-- end of ZOPE management tabs -->
+<form name="form" action="manage_changeFieldOrder" method="post">
+  <p>Fields (tag names) in order of appearance:<br/>
+  <input name="fieldList" tal:attributes="value python:', '.join(here.fieldList)" size="80"/><br/>
+  (edit list to change order)</p>
+  <input type="submit" value="Change"><br/>
+</form>
--- a/zpt/changeMetadata.zpt	Mon Jul 25 16:50:48 2011 +0200
+++ b/zpt/changeMetadata.zpt	Mon Jul 25 20:59:15 2011 +0200
@@ -1,5 +1,9 @@
-<html>
-  <body>
+<div tal:replace="structure here/manage_page_header">Header</div>
+<!-- ZOPE management tabs -->
+<div tal:define="manage_tabs_message options/manage_tabs_message | nothing"
+  tal:replace="structure here/manage_tabs">Tabs</div>
+<!-- end of ZOPE management tabs -->
+
     <form name="form" action="changeMetadata">
       <b> ShortDescription: </b><input type="text" name="shortDescription" size="100" tal:attributes="value python:here.shortDescription"><br><br>
       <b> Description: </b><textarea name="description" tal:content="python:here.description" cols=80 rows=20></textarea><br><br>
@@ -20,5 +24,3 @@
   
       <input type="submit" value="Change"><br><br>
     </form>
-  </body>
-</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zpt/changeMetadataMapping.zpt	Mon Jul 25 20:59:15 2011 +0200
@@ -0,0 +1,50 @@
+<div tal:replace="structure here/manage_page_header">Header</div>
+<!-- ZOPE management tabs -->
+<div tal:define="manage_tabs_message options/manage_tabs_message | nothing"
+  tal:replace="structure here/manage_tabs">Tabs</div>
+<!-- end of ZOPE management tabs -->
+<form name="form" action="manage_changeMetaDataMapping" method="post">
+  <p><b>Title (=Reference Type):</b> <input type="text" name="titleOfObject"
+    tal:attributes="value here/title"></p>
+  <ul>
+    <li><b>Tag-Name:</b> Should be lower case only (e.g. author, number_of_pages,
+      ...)
+    <li><b>Label: </b> Human readable name for display purposes (Author, Number of
+      Pages, ...)
+    <li><b>Explanation:</b> Should contain a help text, what and how to enter in this
+      field. Can contain HTML tags.
+  </ul>
+  <p>(Leave Tag-Name empty to delete field. Add Tag-Name in empty row to add field.)</p>
+  
+  <table border=1>
+    <tr>
+      <th>Tag-name</th>
+      <th>Label</th>
+      <th>Explanation</th>
+      <th>Optional</th>
+      <th>Valuelist</th>
+      <th>DC-map</th>
+    </tr>
+    <tal:block tal:repeat="idx python:range(0,20)">
+      <tr tal:define="name python:here.getAt(here.fieldList,idx); 
+        desc python:here.fields.get(name, {'tag':'','label':'','explanation':'','status':'optional','valuelist':'','dcmap':''})">
+        <td valign="top"><input type="text"
+          tal:attributes="name string:field_$idx; value desc/tag" size="10" /></td>
+        <td valign="top"><input type="text"
+          tal:attributes="name string:label_$idx; value desc/label" size="20" /></td>
+        <td valign="top"><textarea rows="3" cols="20"
+            tal:attributes="name string:explanation_$idx" tal:content="desc/explanation"></textarea>
+        </td>
+        <td valign="top"><input type="checkbox"
+          tal:attributes="name string:status_$idx; checked python:desc['status']=='optional'"
+          value="optional" /></td>
+        <td valign="top"><textarea rows="3" cols="20"
+            tal:attributes="name string:values_$idx" tal:content="desc/valuelist"></textarea>
+        </td>
+        <td valign="top"><input type="text"
+          tal:attributes="name string:dcmap_$idx; value desc/dcmap" size="10" /></td>
+      </tr>
+    </tal:block>
+  </table>
+  <input type="submit" value="Change"><br/>
+</form>