from zope.interface import implements
from zope.component import queryUtility
from waramulib.interfaces import IWidget
from interfaces import IVocabulary, ITranslator
from utils import getLanguage, translate
class Default(object):
implements(IWidget)
def __init__(self, context):
self.context = context
def __repr__(self):
return "%s%r" % (self.__class__.__name__, self.context)
def _fieldLabel(self, cls, id, title):
return u"""
""" % (cls, id, translate(self, title, getLanguage(self.context.request)))
def view(self, f, id, cls, w, tid, lang=None):
""" view form """
translatorLang = getLanguage(self.context.request)
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:
if not x: continue # omit empty values
trans.append([x, u.translate(x, translatorLang)])
#fval = ', '.join(trans)
fval = trans
if f.get('name') == 'url':
# http://ait.opetaja.ee/WaramuWeb/metadata?catalog=1&id=9b0cebeb9c76be497c22d87d2448c82accbcc18d
wurl = w.getWaramuWebUrl()
# TODO: add cid - catalog id here
cid = None
if cid is None: cid = ""
else: cid = 'catalog='+cid+'&'
wid = self.context.getId()
flink = '%smetadata?%sid=%s' % (wurl, cid, wid)
r += ''+fval+''
elif f.get('vocabulary') == '1':
if (f.get('type') == 'MultiString' or f.get('type') == 'MultiInteger') and fval is not None:
for nr in range(len(fval)):
k = fval[nr]
if not k:
continue
linkv = ''
linkt = ''
if isinstance(k, list):
linkv = k[0]
linkt = k[1]
else:
linkv = linkt = k
r += ''+linkt+''
if nr != len(fval)-1:
if len(fval) > nr+1:
if fval[nr+1]:
r += ', '
else:
r += fval
else:
if (f.get('type') == 'MultiString' or f.get('type') == 'MultiInteger') and fval is not None:
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
def edit(self, f, id, cls, w, tid, lang=None):
""" edit form """
r = u""
r += self._fieldLabel(cls, id, f.get('printable'))
fval = self.context.get(f.get('name'), lang)
u = queryUtility(IVocabulary, f.get('name'))
selb = f.get('fixedVocabulary') == '1'
vocSource = None
if selb:
vocSource = w
else:
if u is not None:
if u.restricts():
selb = True
vocSource = u
if selb:
r += ''
else:
if (f.get('type') == 'MultiString' or f.get('type') == 'MultiInteger') and fval is not None:
fval = ', '.join(fval)
r += '\t\n'
if u is not None or f.get('vocabulary') == '1':
r += '\tV\n'
r += "\n"
return r