import urllib from cStringIO import StringIO import cElementTree as ET from AccessControl.SecurityInfo import ClassSecurityInfo from Globals import InitializeClass from zope.component import queryUtility from Products.MetadataPortal.interfaces import IVocabulary from Products.MetadataPortal.utils import getLanguage, translate from mdportal import MetadataPortalView RESOURES_ON_DESKTOP = 15 class BrowseView(MetadataPortalView): """ browse view """ security = ClassSecurityInfo() field = None val = None batchSize = 20 start = 0 addCrits = [] sortby = None sortorder = 'ascending' def __init__(self, context, request): self.context = context self.request = request self.addCrits = [] self.sortby = None self.sortorder = 'ascending' def setField(self, val): self.field = val def getField(self): return self.field def setValue(self, val): self.val = val def getRawValue(self): return self.val def getValue(self): if self.val is None: return self.val if self.val == '__md__null__': return '' if self.val.startswith('__mapped__'): w = self._getWaramu() return w._getMapped(self.val) return self.val def addCrit(self, field, val): """ add additional criterion """ self.addCrits.append([field, val]) def setSortBy(self, sb): """ add a sort criterion """ self.sortby = sb def getSortBy(self): """ return a sort criterion or None """ return self.sortby def setSortOrder(self, so): """ set sort order """ if so != 'ascending' and so != 'descending': so = 'ascending' self.sortorder = so def getSortOrder(self): """ return a sorting order: ascending or descending """ return self.sortorder def getValuesForField(self): """ get a list of field values to browse further """ if self.field is None: return None lang = getLanguage(self.request) w = self._getWaramu() if not w.isConnected(): return None res = w.getCombinedVocabulary(self.field, self.getValue(), lang=lang) #u = queryUtility(ITranslator, field) total = len(res) if total == 0: return [] rr = {} for x in res: elems = len(x) cou = x[0] id = x[1] titl = x[len(x)-1] if id is None: id = '__md__null__' elif isinstance(id, int): pass try: rr[id]['count'] += cou except KeyError: rr[id] = {} rr[id]['id'] = id rr[id]['titl'] = titl rr[id]['count'] = cou #for (i=0; i\n' #q += '' #q += "<"+self.getField()+">"+self.getValue().decode('utf-8')+"" #q += "" llist = w.listIdentifiers(q) self._resource_list = llist if self.start > len(self._resource_list): self.start = len(self._resource_list)-self.batchSize if self.start < 0: self.start = 0 return llist[self.start:self.start+self.batchSize] def getResultNumbers(self): numb = str(self.start + len(self._resource_list[self.start:self.start+self.batchSize])) return str(self.start+1) + " - " + numb + " / " + str(len(self._resource_list)) def getResultsTotal(self): return str(len(self._resource_list)) def _getCurrLink(self): au = self.request.ACTUAL_URL parts = au.split('/') linkb = '' linka = '' bfore = True collect = False strip_next = False for p in parts: if not p: continue if strip_next: continue if p == 'browse': collect = True linkb = "browse/" continue if p == 'start': bfore = False strip_next = True continue if collect: if bfore: linkb += p + "/" else: linka += p + "/" link = linkb+linka if link.endswith('/'): link = link[:-1] return link def getPages(self): t = len(self._resource_list) ps = t/self.batchSize link = self._getCurrLink() if self.start > 0: r = '1 ' else: r = '1 ' if ps == 1: ps += 1 for x in range(2, ps+1): if (x-1)*self.batchSize == self.start: r += ''+str(x)+ ' ' else: r += ''+str(x) + " " try: if (x-1)*self.batchSize+self.batchSize < t: if x*self.batchSize == self.start: r += ''+str(x+1) + " " else: r += ''+str(x+1) + " " except UnboundLocalError: pass return r def getNextLink(self): if self.start+self.batchSize >= len(self._resource_list): return None link = self._getCurrLink() next = self.start+self.batchSize return link+"/start/"+str(next) def getPrevLink(self): if self.start == 0: return None link = self._getCurrLink() prev = self.start-self.batchSize if prev < 0 or prev == 0: return link return link+"/start/"+str(prev) def getSortLink(self, fname): """ get a sort link for a link """ link = ['browse', 'by', self.getField()] link.append(self.getRawValue()) for f, v in self.addCrits: link.append(f) link.append(v) link.append("_sort") link.append(fname) if self.getSortBy(): # asc or desc if self.getSortBy() == fname: if self.getSortOrder() == 'ascending': link.append('descending') else: link.append('ascending') return '/'.join(link) def getSortIcon(self, fname): """ returns a sort link or None """ if self.getSortBy() == fname: return "++resource++rc/arrow_"+self.getSortOrder()+".gif" return None def wrap(self, val): # XXX: not used if isinstance(val, int): val = str(val) if val is None: return "__md__null__" if '/' in val or '?' in val: # TODO: map values here. w = self._getWaramu() return w._mapValue(val) #val = val.encode('utf-8') #quoted = urllib.quote_plus(val) return val def breadcrumbs(self): lang = getLanguage(self.request) b = MetadataPortalView.breadcrumbs(self, True) b += " > " + translate(self.context, 'browse', lang) + " > " field = self.getField() if field is None: return b w = self._getWaramu() if not w.isConnected(): return b fd = w.getField(field) fieldName = field if fd is not None: fieldName = translate(self.context, fd.get('printable'), lang) else: fieldName = translate(self.context, 'field_'+field, lang) if fieldName == 'field_'+field: fieldName = field vu = queryUtility(IVocabulary, field) b += ''+fieldName+'' if self.getValue(): if vu is None or not vu.treeLike(): b += ' > '+self.getValue().decode('utf-8') else: tree = vu.getTree(self.getValue()) for t in tree: b += ' > '+t['t'] + '' if self.addCrits: for f, v in self.addCrits: cf_name = f cf = w.getField(f) if cf is not None: cf_name = translate(self.context, cf.get('printable'), lang) b += ' > ' + cf_name b += ' > '+v.decode('utf-8') return b InitializeClass(BrowseView)