import string import socket from zope.interface import implements from OFS.SimpleItem import SimpleItem from AccessControl import ClassSecurityInfo from AccessControl import getSecurityManager from zope.component import queryUtility, getUtility from SOAPpy.Errors import HTTPError import waramulib.Errors as wErrors from waramulib.interfaces import IWaramuClient from Resource import Resource from interfaces import IWaramu, ITranslator, IVocabulary from utils import generateString class Waramu(SimpleItem): implements(IWaramu) meta_type = "Waramu" security = ClassSecurityInfo() def __init__(self): SimpleItem.__init__(self) self._waramu_location = "" self._waramu_user = "" self._waramu_pass = "" def _saveSettings(self, loc, u, p): self._waramu_location = loc self._waramu_user = u self._waramu_pass = p def _getConnection(self, pullconnection=True): """ get connection to waramu """ wu = getUtility(IWaramuClient, name="waramuconnection") if not pullconnection: return wu if wu.isConnected(): return wu self._connect() return wu def getWaramuLocation(self): return self._waramu_location def getWaramuUser(self): return self._waramu_user def getWaramuPass(self): return self._waramu_pass def getWaramuSuffix(self): return self._getConnection()._getSuffix() def _getOpts(self): t = self.getTemp() return t.waramuopts def isConnected(self): try: return self._getConnection().isConnected() except wErrors.InternalServerError: return False def _connect(self): w = self._getConnection(pullconnection=False) w._init(self.getWaramuLocation()) w.newSession(self.getWaramuUser(), self.getWaramuPass()) w.Identify() t = self.getTemp() x = WaramuRuntimeOptions() if hasattr(t, 'waramuopts'): t._delObject('waramuopts') t._setObject('waramuopts', x) #except socket.gaierror: # t.waramuopts.setMessage('Waramu location is not valid.') #except HTTPError: # t.waramuopts.setMessage('Waramu location is not valid.') #except wErrors.InvalidCredentials: # t.waramuopts.setMessage('Invalid username or password.') #except socket.error: # t.waramuopts.setMessage('Connection refused.') def _disconnect(self): w = self._getConnection(pullconnection=False).closeSession() t = self.getTemp() t._delObject('waramuopts') def getMessage(self): t = self.getTemp() try: return t.waramuopts.getMessage() except AttributeError: return "" def listTypes(self): return self._getConnection().listTypes() def getTypeName(self, tid): o = self._getOpts() if o.getTypes() is None: self.listTypes() t = o.getTypes() return t.get(tid) def getTypeInfos(self, tid): o = self._getOpts() if o.getTypeInfos(tid) == None: self.getTypeDescription(tid) return o.getTypeInfos(tid) def getTypeDescription(self, tid): o = self._getOpts() if o.getTypeDescription(tid) == None: w = self._getConnection() fields, infos = w.describeType(tid) o.setTypeInfos(tid, infos) o.setTypeDescription(tid, fields) return o.getTypeDescription(tid) #def getVocabulary(self, tid, field, fieldType='String'): def getVocabulary(self, tid, field, value=None, lang=None): o = self._getOpts() #voc = o.getVocabulary(tid, field) w = self._getConnection() au = w._genAppUser(getSecurityManager().getUser()) voc = w.getVocabulary(au, tid, field, value, lang) #if voc is None: # # fetch vocabulary # w = self._getConnection() # au = w._genAppUser(getSecurityManager().getUser()) # o.setVocabulary(tid, field, w.getVocabulary(au, tid, field, fieldType)) # voc = o.getVocabulary(tid, field) return voc def newResource(self, data): w = self._getConnection() au = w._genAppUser(getSecurityManager().getUser()) rid = w.newResource(au, data) o = self._getOpts() o.resetVocabularyCache() return rid def updateResource(self, uid, data): w = self._getConnection() au = w._genAppUser(getSecurityManager().getUser()) res = w.updateResource(au, uid, data) o = self._getOpts() o.resetVocabularyCache() return res def getResource(self, uid): w = self._getConnection() au = w._genAppUser(getSecurityManager().getUser()) objXml = w.getResource(au, uid) obj = Resource(self._getConnection(), objXml) #obj._addData(objXml) #obj.setTypeDescription(self.getTypeDescription(obj.getTypeID())) #obj.setTypeInfos(self.getTypeInfos(obj.getTypeID())) #obj.parse() # TODO: store return obj def getField(self, fieldname): # return a field description types = self.listTypes() for t in types.keys(): td = self.getTypeDescription(t) if fieldname in td.keys(): return td.get(fieldname) return None def getCombinedFieldsList(self): # return a list of fields. list is combined from all content types types = self.listTypes() fields = {} seen = [] for t in types.keys(): td = self.getTypeDescription(t) for fn, fd in td.items(): if fn in seen: continue u = queryUtility(IVocabulary, fn) #if u is None and fd.get('vocabulary') != '1' and fd.get('fixedVocabulary') != '1': # continue seen.append(fn) fields[fn] = fd return fields def getCombinedVocabulary(self, field, currValue=None, lang=None): # return a vocabulary for a field types = self.listTypes() tmp = {} lvoc = queryUtility(IVocabulary, field) if (lvoc is not None and not lvoc.treeLike()) or (lvoc is None): try: v = self.getVocabulary(None, field, currValue, lang) #, td.get(field).get('type')) for x in v: if x[2] is not None: if x[2] != lang: continue if tmp.has_key(x[1]): tmp[x[1]] += x[0] else: tmp[x[1]] = x[0] except wErrors.InternalServerError: pass elif lvoc.treeLike(): lvv = [] if currValue is None: lvv = lvoc.getMain() else: lvv = lvoc.getChildren(currValue) for y in lvv: x = y['id'] if not tmp.has_key(x): tmp[x] = None res = [] u = queryUtility(ITranslator, field) if u is not None: if hasattr(u, 'get'): for x in tmp.items(): trans = u.get(x[0]) if trans: res.append([x[1], x[0], trans['t']]) else: [ res.append([x[1], x[0], u.translate(x[0], lang)]) for x in tmp.items() ] else: [ res.append([x[1], x[0]]) for x in tmp.items() ] res.sort(reverse=True) return res def listIdentifiers(self, query): # perform a query, return a list of matching resource ids w = self._getConnection() au = w._genAppUser(getSecurityManager().getUser()) return w.listIdentifiers(au, query) def listAttachments(self, rid): # list attachments for a resource w = self._getConnection() au = w._genAppUser(getSecurityManager().getUser()) return w.listAttachments(au, rid) def deleteAttachment(self, rid, fid): w = self._getConnection() au = w._genAppUser(getSecurityManager().getUser()) return w.removeAttachment(au, rid, fid) def deleteResource(self, rid): w = self._getConnection() au = w._genAppUser(getSecurityManager().getUser()) return w.deleteResource(au, rid) def addAttachment(self, rid, data, filename): w = self._getConnection() au = w._genAppUser(getSecurityManager().getUser()) attsList = w.addAttachment(au, rid, data, filename) return 0 def _mapValue(self, val): o = self._getOpts() emval = o.getMappedByVal(val) if emval is None: mval = '__mapped__' for x in val: if x not in string.letters and x not in string.digits: mval += '_' else: mval += x o._addMapping(val, mval) else: return emval return mval def _getMapped(self, mval): o = self._getOpts() return o.getMappedByMVal(mval) def getWaramuWebUrl(self): return self._getConnection()._getWebUrl() class WaramuRuntimeOptions(SimpleItem): meta_type = 'WaramuOptions' id = 'waramuopts' def __init__(self): SimpleItem.__init__(self) self._types_cache = None self._typeDescs = {} self._voc_cache = {} self._suffix = None self._type_infos = {} self._mappedByVal = {} self._mappedByMVal = {} def getMappedByVal(self, val): if self._mappedByVal.has_key(val): return self._mappedByVal.get(val) return None def getMappedByMVal(self, mval): if self._mappedByMVal.has_key(mval): return self._mappedByMVal.get(mval) return None def _addMapping(self, val, mval): self._mappedByMVal[mval] = val self._mappedByVal[val] = mval def getMessage(self): return getattr(self, '_message', '') def setMessage(self, val): self._message = val def getTypes(self): return self._types_cache def setTypes(self, val): self._types_cache = val def getTypeDescription(self, tid): if not self._typeDescs.has_key(tid): return None return self._typeDescs.get(tid) def setTypeDescription(self, tid, val): self._typeDescs[tid] = val def getVocabulary(self, tid, field): if not self._voc_cache.has_key(tid): return None if not self._voc_cache[tid].has_key(field): return None return self._voc_cache[tid][field] def setVocabulary(self, tid, field, val): if not self._voc_cache.has_key(tid): self._voc_cache[tid] = {} self._voc_cache[tid][field] = val def resetVocabularyCache(self): self._voc_cache = {} def setSuffix(self, val): self._suffix = val def getSuffix(self): return self._suffix def setTypeInfos(self, tid, inf): self._type_infos[tid] = inf def getTypeInfos(self, tid): if not self._type_infos.has_key(tid): return None return self._type_infos[tid] def setWaramuWebUrl(self, wurl): self._wurl = wurl def getWaramuWebUrl(self): return self._wurl