Ejemplo n.º 1
0
    def getSortedKeys(self):
        """ returns a list of keys sorted accordingly to the
        selected sort method (may be unsorted if method = no sort)
        """
        sortMethod = self.getSortMethod()
        keys, values = zip(*self.getTermItems())
        keys = list(keys)

        if not hasattr(self, 'sortMethod'):
            # smooth upgrade from previous releases
            return keys
        if sortMethod == atvm_config.SORT_METHOD_LEXICO_KEYS:
            keys.sort()
            return keys
        if sortMethod == atvm_config.SORT_METHOD_LEXICO_VALUES:
            # returns keys sorted by lexicogarphic order of VALUES
            sm = getSecurityManager()
            terms = [
                t for t in self.contentValues()
                if IPleiadesVocabularyTerm.providedBy(t)
                and sm.checkPermission(View, t)
            ]
            terms.sort(
                lambda x, y: cmp(
                    x.getVocabularyValue(), y.getVocabularyValue()))
            return [term.getVocabularyKey() for term in terms]
        if sortMethod == atvm_config.SORT_METHOD_FOLDER_ORDER:
            return keys
        if sortMethod == SORT_METHOD_TEMPORAL:
            sm = getSecurityManager()
            kvdict = dict(
                (k, t) for k, t in self.contentItems()
                if IPleiadesVocabularyTerm.providedBy(t)
                and sm.checkPermission(View, t)
            )

            def range(key):
                term = kvdict[key]
                descr = term.Description()
                m = re.search(
                    r"\[\[(-{0,1}\d*\.{0,1}\d*)\s*,\s*(-{0,1}\d*\.{0,1}\d*)\]\]",
                    descr)
                if m is not None:
                    min = float(m.group(1))
                    max = float(m.group(2))
                    return min, max
                else:
                    return None
            keys.sort(lambda x, y: cmp(range(x), range(y)))
        # fallback
        return keys
Ejemplo n.º 2
0
 def getTermItems(self, all=False):
     """Securely get (key, value) tuples of published terms."""
     sm = getSecurityManager()
     return [
         (r.getTermKey(), r.getTermValue())
         for r in self.values()
         if IPleiadesVocabularyTerm.providedBy(r)
         and sm.checkPermission(View, r)
     ]