proper facet
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 31 Aug 2009 17:10:59 +0200
changeset 58 ff3b6ff260f3
parent 57 81ade529d1b0
child 59 51e2024dcfc9
proper facet
__init__.py
views.py
--- a/__init__.py	Mon Aug 31 17:10:28 2009 +0200
+++ b/__init__.py	Mon Aug 31 17:10:59 2009 +0200
@@ -5,29 +5,3 @@
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 """
 __docformat__ = "restructuredtext en"
-
-
-try:
-    from cubicweb.web import FACETTES
-except ImportError:
-    # cubicweb.web not available
-    pass
-else:
-    def kwvocab_from_rset(rset):
-        vocabbyscheme = {}
-        for e in rset.entities():
-            # take care, rset may contains entities which are not Keyword
-            try:
-                svocab = vocabbyscheme.setdefault(e.classification, [])
-            except AttributeError:
-                continue
-            svocab.append( (e.eid, e.view('combobox')) )
-        vocab = []
-        for i, (scheme, svocab) in enumerate(vocabbyscheme.iteritems()):
-            vocab += sorted(svocab, key=lambda x: x[1])
-            if i < len(vocabbyscheme) - 1:
-                # insert separators
-                vocab.append( (None, '----') )
-        return  'eid', vocab
-
-    FACETTES.add( ('applied_to', 'object', 'name', kwvocab_from_rset) )
--- a/views.py	Mon Aug 31 17:10:28 2009 +0200
+++ b/views.py	Mon Aug 31 17:10:59 2009 +0200
@@ -14,7 +14,7 @@
 from cubicweb.selectors import implements, rql_condition, relation_possible
 from cubicweb.view import EntityView
 from cubicweb.common.mixins import TreePathMixIn
-from cubicweb.web import stdmsgs, uicfg, component
+from cubicweb.web import stdmsgs, uicfg, component, facet
 from cubicweb.web.views import primary, baseviews, basecontrollers
 
 # displayed by the above component
@@ -140,6 +140,27 @@
         self.w(u'<td><div id="kwformholder"></div>')
         self.w(u'</td></tr></table>')
 
+
+# applied_to relation facet ####################################################
+
+class AppliedToFacet(facet.RelationFacet):
+    id = 'applied-to-facet'
+    rtype = 'applied_to'
+    role = 'object'
+    target_attr = 'name'
+
+    def rset_vocabulary(self, rset):
+        _ = self.req._
+        vocab = []
+        scheme = None
+        for e in sorted(rset.entities(),
+                        key=lambda e: (e.classification.name, e.view('combobox'))):
+            if scheme != e.classification.name:
+                vocab.append( (_(e.classification.name), None) )
+            vocab.append( (e.view('combobox'), e.eid) )
+        return vocab
+
+
 # add some classification schema related methods to the Jsoncontroller ########
 
 @monkeypatch(basecontrollers.JSonController)