def OnEndElement(self, sTagName): """ XmlAttrBuilder.OnEndElement """ # -- TAG-STACK tag = self.dTagStack.pop() name = standard.unencode(tag['name']) attrs = standard.unencode(tag['attrs']) cdata = standard.unencode(tag['cdata']) # Hack for nested CDATA cdata = re.compile(r'\<\!\{CDATA\{(.*?)\}\}\>').sub( r'<![CDATA[\1]]>', cdata) if name != sTagName: raise ParseError("Unmatching end tag (" + str(sTagName) + ") expected (" + str(name) + ")") # -- DATA if sTagName in ['data']: filename = attrs.get('filename') content_type = attrs.get('content_type') try: data = standard.hex2bin(cdata) except: data = bytes(cdata, 'utf-8') file = { 'data': data, 'filename': filename, 'content_type': content_type } objtype = attrs.get('type') item = _blobfields.createBlobField(None, objtype, file) for key in attrs: value = attrs.get(key) setattr(item, key, value) self.dValueStack.pop() self.dValueStack.append(item) # -- ITEM elif sTagName in ['item']: if tag['dValueStack'] < len(self.dValueStack): item = self.dValueStack.pop() else: item = cdata item = getXmlTypeSaveValue(item, attrs) value = self.dValueStack.pop() if isinstance(value, dict): key = attrs.get('key') value[key] = item if isinstance(value, list): value.append(item) self.dValueStack.append(value)
def recurse_importContent(self, folder): # Cleanup. for key in ['oRootTag', 'oCurrNode', 'oParent', 'dTagStack', 'dValueStack']: try: delattr(self, key) except: pass # Upload ressources. langs = self.getLangIds() prim_lang = self.getPrimaryLanguage() obj_attrs = self.getObjAttrs() for key in obj_attrs: obj_attr = self.getObjAttr(key) datatype = obj_attr['datatype_key'] if datatype in _globals.DT_BLOBS: for lang in langs: try: if obj_attr['multilang'] or lang==prim_lang: req = {'lang':lang,'preview':'preview'} obj_vers = self.getObjVersion(req) blob = self._getObjAttrValue(obj_attr, obj_vers, lang) if blob is not None: filename = _fileutil.getOSPath('%s/%s'%(folder, blob.filename)) standard.writeBlock( self, '[recurse_importContent]: filename=%s'%filename) # Backup properties (otherwise manage_upload sets it). bk = {} for __xml_attr__ in blob.__xml_attrs__: bk[__xml_attr__] = getattr(blob, __xml_attr__, '') # Read file to ZODB. f = open( filename, 'rb') try: blob = _blobfields.createBlobField( self, datatype, file={'data':f,'filename':filename}) finally: f.close() # Restore properties. for __xml_attr__ in blob.__xml_attrs__: if bk.get(__xml_attr__, '') not in ['', 'text/x-unknown-content-type']: setattr(blob, __xml_attr__, bk[__xml_attr__]) blob.getFilename() # Normalize filename self.setObjProperty(key, blob, lang) except: standard.writeError(self, "[recurse_importContent]") # Commit object. self.onChangeObj( self.REQUEST, forced=1) transaction.commit() # Process children. for ob in self.getChildNodes(): recurse_importContent(ob, folder)
def xmlOnUnknownEndTag(self, sTagName): # -- TAG-STACK tag = self.dTagStack.pop() skip = len([x for x in self.oCurrNode.dTagStack if x.get('skip')]) > 0 name = standard.unencode(tag['name']) if name != sTagName: return 0 # don't accept any unknown tag attrs = standard.unencode(tag['attrs']) cdata = standard.unencode(tag['cdata']) # -- ITEM (DICTIONARY|LIST) -- #---------------------------- if sTagName in ['dict', 'dictionary']: pass elif sTagName == 'list': pass elif sTagName == 'item': item = cdata if tag['dValueStack'] < len(self.dValueStack): item = self.dValueStack.pop() else: item = cdata item = getXmlTypeSaveValue(item, attrs) value = self.dValueStack.pop() if isinstance(value, dict): key = attrs.get('key') value[key] = item if isinstance(value, list): value.append(item) self.dValueStack.append(value) # -- DATA (IMAGE|FILE) -- #----------------------- elif sTagName == 'data': value = attrs if cdata is not None and len(cdata) > 0: filename = attrs.get('filename') content_type = attrs.get('content_type') try: data = standard.hex2bin(cdata) except: data = bytes(cdata, 'utf-8') value['data'] = data self.dValueStack.append(value) # -- LANGUAGE -- #-------------- elif sTagName == 'lang': lang = attrs.get('id', self.getPrimaryLanguage()) if len(self.dValueStack) == 1: item = cdata else: item = self.dValueStack.pop() values = self.dValueStack.pop() values[lang] = item self.dValueStack.append(values) # -- OBJECT-ATTRIBUTES -- #----------------------- elif sTagName in self.getObjAttrs(): if not skip: obj_attr = self.getObjAttr(sTagName) # -- DATATYPE datatype = obj_attr['datatype_key'] # -- Multi-Language Attributes. if obj_attr['multilang']: item = self.dValueStack.pop() if item is not None: if not isinstance(item, dict): item = {self.getPrimaryLanguage(): item} for s_lang in item: value = item[s_lang] # Data if datatype in _globals.DT_BLOBS: if isinstance(value, dict) and len(value.keys()) > 0: ob = _blobfields.createBlobField( self, datatype) for key in value: setattr(ob, key, value[key]) xmlInitObjProperty(self, sTagName, ob, s_lang) # Others else: # -- Init Properties. xmlInitObjProperty(self, sTagName, value, s_lang) else: # -- Complex Attributes (Blob|Dictionary|List). value = None if len(self.dValueStack) > 0: value = self.dValueStack.pop() if value is not None and \ (datatype in _globals.DT_BLOBS or \ datatype == _globals.DT_LIST or \ datatype == _globals.DT_DICT): # Data if datatype in _globals.DT_BLOBS: if isinstance(value, dict) and len(value.keys()) > 0: ob = _blobfields.createBlobField(self, datatype) for key in value: setattr(ob, key, value[key]) xmlInitObjProperty(self, sTagName, ob) # Others else: if self.getType() == 'ZMSRecordSet': if isinstance(value, list): for item in value: if isinstance(item, dict): for key in item: item_obj_attr = self.getObjAttr( key) item_datatype = item_obj_attr[ 'datatype_key'] if item_datatype in _globals.DT_BLOBS: item_data = item[key] if isinstance(item_data, dict): blob = _blobfields.createBlobField( self, item_datatype, item_data) blob.on_setobjattr() item[key] = blob # -- Convert multilingual to monolingual attributes. if obj_attr['multilang'] == 0 and \ isinstance(value, dict) and \ len(value.keys()) == 1 and \ value.keys()[0] == self.getPrimaryLanguage(): value = value[value.keys()[0]] xmlInitObjProperty(self, sTagName, value) if len(self.dValueStack) > 0: raise "Items on self.dValueStack=%s" % self.dValueStack # -- Simple Attributes (String, Integer, etc.) else: if value is not None: standard.writeBlock( self, "[xmlOnUnknownEndTag]: WARNING - Skip %s" % sTagName) value = cdata # -- OPTIONS if 'options' in obj_attr: options = obj_attr['options'] if isinstance(options, list): try: i = options.index(int(value)) if i % 2 == 1: value = options[i - 1] except: try: i = options.index(value) if i % 2 == 1: value = options[i - 1] except: pass xmlInitObjProperty(self, sTagName, value) # Clear value stack. self.dValueStack.clear() # -- OTHERS -- #------------ else: value = None if len(self.dTagStack): value = self.dTagStack.pop() if value is None: value = {'cdata': ''} cdata = value.get('cdata', '') cdata += '<' + tag['name'] for attr_name in attrs: attr_value = attrs.get(attr_name) cdata += ' ' + attr_name + '="' + attr_value + '"' cdata += '>' + tag['cdata'] cdata += '</' + tag['name'] + '>' value['cdata'] = cdata self.dTagStack.append(value) return 1 # accept matching end tag
def manage_changeFilter(self, lang, btn='', key='', REQUEST=None, RESPONSE=None): """ FilterManager.manage_changeFilter """ message = '' id = REQUEST.get('id', '') index = REQUEST.get('index', -1) # Change. # ------- if btn == 'BTN_SAVE': cp = self.getFilter(id) # Filter. newId = REQUEST.get('inpId').strip() newAcquired = 0 newName = REQUEST.get('inpName').strip() newFormat = REQUEST.get('inpFormat').strip() newContentType = REQUEST.get('inpContentType').strip() newDescription = REQUEST.get('inpDescription').strip() newRoles = REQUEST.get('inpRoles', []) newMetaTypes = REQUEST.get('inpMetaTypes', []) id = self.setFilter(id, newId, newAcquired, newName, newFormat, newContentType, newDescription, newRoles, newMetaTypes) # Filter Processes. index = 0 for filterProcess in cp.get('processes', []): newProcessId = REQUEST.get('newFilterProcessId_%i'%index, '').strip() newProcessFile = REQUEST.get('newFilterProcessFile_%i'%index) if isinstance(newProcessFile, ZPublisher.HTTPRequest.FileUpload): if len(getattr(newProcessFile, 'filename', ''))==0: newProcessFile = filterProcess.get('file', None) else: newProcessFile = _blobfields.createBlobField(self, _blobfields.MyFile, newProcessFile) self.setFilterProcess(id, index, newProcessId, newProcessFile) index += 1 # New Filter Process? newProcessId = REQUEST.get('newFilterProcessId_%i'%index, '').strip() newProcessFile = REQUEST.get('newFilterProcessFile_%i'%index) if newProcessId: self.setFilterProcess(id, newProcessId, newProcessFile) # Return with message. message = self.getZMILangStr('MSG_CHANGED') # Delete. # ------- elif btn == 'BTN_DELETE': if key == 'obj': ids = REQUEST.get('ids', []) for id in ids: self.delFilter(id) message = self.getZMILangStr('MSG_DELETED')%len(ids) elif key == 'attr': ids = [REQUEST.get('id')] for id in ids: if id is not None: self.delFilterProcess(id, index) message = self.getZMILangStr('MSG_DELETED')%len(ids) # Export. # ------- elif btn == 'BTN_EXPORT': return self.exportXml(REQUEST, RESPONSE) # Import. # ------- elif btn == 'BTN_IMPORT': f = REQUEST['file'] if f: filename = f.filename self.importXml(xml=f) else: filename = REQUEST['init'] self.importConf(filename) message = self.getZMILangStr('MSG_IMPORTED')%('<em>%s</em>'%filename) # Insert. # ------- elif btn == 'BTN_INSERT': newId = REQUEST.get('newId').strip() newAcquired = 0 newName = REQUEST.get('newName').strip() newFormat = REQUEST.get('newFormat').strip() newContentType = REQUEST.get('newContentType').strip() id = self.setFilter(None, newId, newAcquired, newName, newFormat, newContentType) message = self.getZMILangStr('MSG_INSERTED')%id # Move to. # -------- elif btn == 'move_to': pos = REQUEST['pos'] self.moveFilterProcess(id, index, pos) message = self.getZMILangStr('MSG_MOVEDOBJTOPOS')%(("<em>%s</em>"%index), (pos+1)) # Return with message. message = standard.url_quote(message) return RESPONSE.redirect('manage_main?id=%s&index:int=%i&lang=%s&manage_tabs_message=%s'%(id, index, lang, message))
def manage_changeActivities(self, lang, btn='', REQUEST=None, RESPONSE=None): """ ZMSWorkflowActivitiesManager.manage_changeActivities """ message = '' id = REQUEST.get('id', '') # Cancel. # ------- if btn in ['BTN_CANCEL', 'BTN_BACK']: id = '' # Change. # ------- if btn == 'BTN_SAVE': item = self.getActivity(id, for_export=True) newId = REQUEST.get('inpId').strip() newName = REQUEST.get('inpName').strip() newIconClazz = REQUEST.get('inpIconClazz', '').strip() newIcon = None if len(newIconClazz) == 0: newIcon = REQUEST.get('inpIcon', '') if isinstance(newIcon, ZPublisher.HTTPRequest.FileUpload): if len(getattr(newIcon, 'filename', '')) == 0: newIcon = item.get('icon', None) else: newIcon = _blobfields.createBlobField( self, _blobfields.MyImage, newIcon) id = self.setActivity(item.get('id', None), newId, newName, newIconClazz, newIcon) message = self.getZMILangStr('MSG_CHANGED') # Delete. # ------- elif btn == 'BTN_DELETE': id = self.delItem(id, 'activities') message = self.getZMILangStr('MSG_CHANGED') # Insert. # ------- elif btn == 'BTN_INSERT': item = {} newId = REQUEST.get('newId').strip() newName = REQUEST.get('newName').strip() newIconClazz = REQUEST.get('newIconClazz', '').strip() newIcon = None if len(newIconClazz) == 0: newIcon = REQUEST.get('newIcon', '') if isinstance(newIcon, ZPublisher.HTTPRequest.FileUpload): if len(getattr(newIcon, 'filename', '')) == 0: newIcon = item.get('icon', None) else: newIcon = _blobfields.createBlobField( self, _blobfields.MyImage, newIcon) id = self.setActivity(item.get('id', None), newId, newName, newIconClazz, newIcon) message = self.getZMILangStr('MSG_INSERTED') % id # Move to. # -------- elif btn == 'move_to': pos = REQUEST['pos'] self.moveItem(id, pos, 'activities') message = self.getZMILangStr('MSG_MOVEDOBJTOPOS') % ( ("<i>%s</i>" % id), (pos + 1)) id = '' # Return with message. message = standard.url_quote(message) return RESPONSE.redirect('manage_main?lang=%s&manage_tabs_message=%s' % (lang, message))