view MetaDataMapping.py @ 23:5ed16f971297

getDomFromPathOrUrl does not throw exception but returns None.
author casties
date Wed, 25 Jul 2012 12:53:17 +0200
parents 24214a828627
children
line wrap: on
line source

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
import os.path
import urllib
import logging
import urlparse

import xml.etree.ElementTree as ET

# TODO: do we need this?
#from Products.OSA_system2 import OSAS_helpers
#from Products.OSA_system2.OSAS_metadata import OSAS_Metadata,OSAS_MetadataMapping
#from OSAS_metadata import OSAS_MetadataMapping

from SrvTxtUtils import getAt


MAX_FIELDS = 30

class MetaDataMapping(SimpleItem):
    """Metadata mapping object representing a fixed set of sub-elements."""
    
    meta_type="MetadataMapping"
    manage_options = (
        {'label':'Configuration','action':'manage_changeMetaDataMappingForm'},
        {'label':'Field order','action':'manage_changeFieldOrderForm'},
        ) + SimpleItem.manage_options
    
    fields = {}
    """dict containing element descriptions"""
    
    fieldList = []
    """list of field names in preferred order"""
    
    def __init__(self,id,title,fields,fieldList=None):
        """init
        @param id: id (in Zope)
        @param title: real element name
        @param fields: dict with field description dicts
        @param fieldList: list of field names in preferred order
        """
        self.id=id
        self.title=title
        self.fields = fields.copy()
        
        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 self.fields
    
    def getFieldList(self):
        """returns ordered list of fields"""
        if self.fieldList is None:
            # create fieldList
            self.fieldList = fields.keys()
        return self.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, MAX_FIELDS):
            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')

    
def manage_addMetaDataMappingForm(self):
    """add MetapdataMapping form"""
    pt = PageTemplateFile('zpt/addMetadataMappingForm', globals()).__of__(self)
    return pt()

def manage_addMetaDataMapping(self,idOfObject,titleOfObject,RESPONSE=None):
    """add the MetapdataMapping"""
    fields={}
    fieldList=[]
    for idx in range(1, MAX_FIELDS):
        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,fields,fieldList)
    self._setObject(idOfObject,newObj)
    if RESPONSE is not None:
        RESPONSE.redirect('manage_main')