web/facet.py
changeset 3369 7b88d12b4ee2
parent 3023 7864fee8b4ec
parent 3333 c61a526b530e
child 3451 6b46d73823f5
--- a/web/facet.py	Thu Sep 17 15:52:46 2009 +0200
+++ b/web/facet.py	Tue Sep 22 12:11:12 2009 +0200
@@ -176,7 +176,15 @@
 
 def insert_attr_select_relation(rqlst, mainvar, rtype, role, attrname,
                                 sortfuncname=None, sortasc=True):
-    """modify a syntax tree to retrieve only relevant attribute `attr` of `var`"""
+    """modify a syntax tree to :
+    * link a new variable to `mainvar` through `rtype` (where mainvar has `role`)
+    * retrieve only the newly inserted variable and its `attrname`
+
+    Sorting:
+    * on `attrname` ascendant (`sortasc`=True) or descendant (`sortasc`=False)
+    * on `sortfuncname`(`attrname`) if `sortfuncname` is specified
+    * no sort if `sortasc` is None
+    """
     _cleanup_rqlst(rqlst, mainvar)
     var = _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role)
     # not found, create one
@@ -186,7 +194,8 @@
     if rqlst.groupby:
         if not attrvar in rqlst.groupby:
             rqlst.add_group_var(attrvar)
-    _set_orderby(rqlst, attrvar, sortasc, sortfuncname)
+    if sortasc is not None:
+        _set_orderby(rqlst, attrvar, sortasc, sortfuncname)
     # add attribute variable to selection
     rqlst.add_selected(attrvar)
     # add is restriction if necessary
@@ -346,6 +355,8 @@
     sortfunc = None
     # ascendant/descendant sorting
     sortasc = True
+    # if you want to call a view on the entity instead of using `target_attr`
+    label_vid = None
 
     @property
     def title(self):
@@ -356,10 +367,14 @@
         """
         rqlst = self.rqlst
         rqlst.save_state()
+        if self.label_vid is not None and self.sortfunc is None:
+            sort = None # will be sorted on label
+        else:
+            sort = self.sortasc
         try:
             mainvar = self.filtered_variable
             insert_attr_select_relation(rqlst, mainvar, self.rtype, self.role,
-                                        self.target_attr, self.sortfunc, self.sortasc)
+                                        self.target_attr, self.sortfunc, sort)
             try:
                 rset = self.rqlexec(rqlst.as_string(), self.rset.args, self.rset.cachekey)
             except:
@@ -384,8 +399,14 @@
             rqlst.recover()
 
     def rset_vocabulary(self, rset):
-        _ = self.req._
-        return [(_(label), eid) for eid, label in rset]
+        if self.label_vid is None:
+            _ = self.req._
+            return [(_(label), eid) for eid, label in rset]
+        if self.sortfunc is None:
+            return sorted((entity.view(self.label_vid), entity.eid)
+                          for entity in rset.entities())
+        return [(entity.view(self.label_vid), entity.eid)
+                for entity in rset.entities()]
 
     @cached
     def support_and(self):
@@ -531,6 +552,7 @@
                                             self.formatvalue(supvalue),
                                             self.attrtype, '<=')
 
+
 class DateRangeFacet(RangeFacet):
     attrtype = 'Date' # only date types are supported
 
@@ -571,6 +593,7 @@
         else:
             self.rqlst.add_relation(var, self.rtype, self.filtered_variable)
 
+
 ## html widets ################################################################
 
 class FacetVocabularyWidget(HTMLWidget):
@@ -656,7 +679,7 @@
         facet = self.facet
         facet.req.add_js('ui.slider.js')
         facet.req.add_css('ui.all.css')
-        sliderid = make_uid('the slider')
+        sliderid = make_uid('theslider')
         facetid = xml_escape(self.facet.id)
         facet.req.html_headers.add_onload(self.onload % {
             'sliderid': sliderid,
@@ -722,6 +745,7 @@
         self.w(u'<a href="javascript: {}">%s</a>' % xml_escape(self.label))
         self.w(u'</div>')
 
+
 class CheckBoxFacetWidget(HTMLWidget):
     selected_img = "black-check.png"
     unselected_img = "black-uncheck.png"
@@ -753,6 +777,7 @@
         self.w(u'</div>\n')
         self.w(u'</div>\n')
 
+
 class FacetSeparator(HTMLWidget):
     def __init__(self, label=None):
         self.label = label or u'&#160;'