from zope.interface import implements from zope.component import queryUtility from Products.MetadataPortal.interfaces import IVocabulary, ITranslator from Products.MetadataPortal.waramulib.interfaces import IWidget from Products.MetadataPortal.widgets import Default from Products.MetadataPortal.utils import getLanguage, translate from data import curriculum import time def cmp(l, r): if l['id'] > r['id']: return 1 if l['id'] < r['id']: return -1 return 0 class Curriculum(object): """ curriculum vocabulary """ implements(IVocabulary, ITranslator) _mapp = curriculum.mapp _oldies = curriculum.oldies def treeLike(self): return True def fixedVocabulary(self): return True def get(self, v): if not isinstance(v, int): try: v = int(v) except ValueError: raise 'oopsi, not an in' return self._mapp.get(v) def getTree(self, v, lang=None): if not isinstance(v, int): try: v = int(v) except ValueError: return v vmat = self._mapp.get(v) if vmat is None: return u""+str(v) r = [ x for x in self._mapp.values() if v in x['ran'] ] r.append(vmat) r.sort(cmp) return r def translate(self, v, lang=None): if not isinstance(v, int): try: v = int(v) except ValueError: return v vmat = self._mapp.get(v) if vmat is None: return u""+str(v) r = [ x for x in self._mapp.values() if v in x['ran'] ] r.append(vmat) r.sort(cmp) st = u"" for i in range(len(r)): x = r[i] if x is None: continue # old, invalid values are haunting st += x['t'] if i != len(r)-1: st += " >> " return st def getChildren(self, v): if not isinstance(v, int): try: v = int(v) except ValueError: return v o = self._mapp.get(v) if o is None: raise 'oopsi, '+str(v)+' not found' lev = o.get('lev') matlev = lev + 1 children = [] for x in o.get('ran'): if not self._mapp.has_key(x): continue if self._mapp.get(x).get('lev') == matlev: children.append(x) r = [self._mapp.get(x) for x in children ] r.sort(cmp) return r def getMain(self): r = [ x for x in self._mapp.values() if x.get('lev') == 1 ] r.sort(cmp) return r class CurriculumWidget(Default): implements(IWidget) def __repr__(self): return "%s%r" % (self.__class__.__name__, self.context) def edit(self, f, id, cls, w, tid, lang=None): r = u"" r += self._fieldLabel(cls, id, f.get('printable')) fval = self.context.get(f.get('name'), lang) ijoined_val = "" if (f.get('type') == 'MultiString' or f.get('type') == 'MultiInteger') and fval is not None: ijoined_val = ', '.join(fval) u = queryUtility(IVocabulary, f.get('name')) r += '\t\n' #if u is not None or (f.get('vocabulary') == '1' and f.get('fixedVocabulary') == '0'): # r += '\tV\n' r += '' translator = queryUtility(ITranslator, f.get('name')) r += '
\n' for x in fval: if not x: continue r += '
\n' r += 'X\n' r += '' if translator is None: r += x else: r += translator.translate(x) r += '' r += '
\n' r += '
\n' r += '\t
'+translate(self, "add a value", getLanguage(self.context.request))+'
\n' r += "\n" r += '\n' return r def view(self, f, id, cls, w, tid, lang=None): """ curriculum field view. it should always be multivalued! """ r = u"" r += self._fieldLabel(cls, id, f.get('printable')) fval = self.context.get(f.get('name'), lang) u = queryUtility(ITranslator, f.get('name')) if u is not None: trans = [] for x in fval: trans.append([x, u.translate(x)]) #fval = ', '.join(trans) fval = trans if u is None: fval = ', '.join(fval) else: # translator applied cc = u'' for x in range(len(fval)): e = fval[x] cc += e[1] if x != len(fval)-1: cc += '
' fval = cc r += fval r += "\n" return r curriculum = Curriculum()