import cElementTree from Globals import Persistent, InitializeClass from OFS.SimpleItem import SimpleItem import time class Resource(Persistent, SimpleItem): id = None _acl = {'owner': '', 'viewUsers' : [], 'viewGroups' : [], 'editUsers' : [], 'editGroups' : [], 'downUsers' : [], 'downGroups' : []} all_langs = ['et', 'en'] _n_of_files = 0 _stash = {} _infos = {} _fetchTime = None def __init__(self, conn, data): self._data = data xml = cElementTree.fromstring(data) schema = xml.find('schema') id = xml.find('wid') self.id = id.text.strip() self._schema = int(schema.text.strip()) # get type description td, infos = conn.describeType(self._schema) self._td = td self._infos = infos self._parse() self._storeTime = time.time() def _parse(self): td = self._td xml = cElementTree.fromstring(self._data) s = {} all_langs = [] for fn, d in td.items(): vv = xml.findall('data/'+fn) for v in vv: if d.get('multiLingual') == '1': lang = v.get('lang') if lang not in all_langs: all_langs.append(lang) else: lang = None if d.get('type') == 'MultiString': if lang is None: s[fn] = [] else: if not s.has_key(fn): s[fn] = {} vo = v.findall('value') for onva in vo: val = onva.text if val is None: val = "" else: val = val.strip() if lang is None: s[fn].append(val) else: if not s[fn].has_key(lang): s[fn][lang] = [] s[fn][lang].append(val) else: val = v.text if val is None: val = "" else: val = val.strip() if lang is None: s[fn] = val else: if not s.has_key(fn): s[fn] = {} if not s[fn].has_key(lang): s[fn][lang] = "" s[fn][lang] = val self._stash = s self.all_langs = all_langs # read acl owner = xml.find('acl/owner').text.strip() self._acl['owner'] = owner view = xml.find('acl/view') edit = xml.find('acl/edit') down = xml.find('acl/down') for x, m in [view, 'view'], [edit, 'edit'], [down, 'down']: if x is None: continue users = x.find('users') groups = x.find('groups') if users: us = users.findall('value') for u in us: uname = u.text.strip() self._acl[m+'Users'].append(uname) if groups: gr = groups.findall('value') for g in gr: grname = g.text.strip() self._acl[m+'Groups'].append(grname) if self.isAttachable(): ats = xml.find('attachments') self._n_of_files = int(ats.text) def isAttachable(self): return self._infos.get('attachable', 0) def getCachedTime(self): return self._storeTime def getId(self): return self.id def getTitle(self): title = self._stash.get('title') if isinstance(title, dict): noopt = True gotTitle = False if self._stash.get('mdLanguage'): if title.has_key(self._stash.get('mdLanguage')): title = title.get(self._stash.get('mdLanguage')) gotTitle = True noopt = False if noopt: # XXX: need testing for x in title.keys(): if title.get(x) is not None and \ len(title.get(x)) > 0: title = title.get(x) gotTitle = True break if not gotTitle: title = None if title is None: # needs a fix if self._stash.has_key('k_title'): title = self._stash.get('k_title') if not title: title = "(untitled)" if self.isAttachable(): title += " ("+str(self._n_of_files)+") " return title def getLingualFields(self): return [ x.get('name') for x in self._td.values() if x.get('multiLingual') == '1' ] def getPlainFields(self): return [ x.get('name') for x in self._td.values() if x.get('multiLingual') == '0' ] def get(self, f, l=None): """ get """ v = self._stash.get(f) if l is not None: v = v.get(l) if self._td.get(f).get('type') == 'MultiString': return ', '.join(v) return v def printable(self, field): p = self._td.get(field).get('printable') return p InitializeClass(Resource)