174 term = nodes.SortTerm(sortfunc, sortasc) |
174 term = nodes.SortTerm(sortfunc, sortasc) |
175 rqlst.add_sort_term(term) |
175 rqlst.add_sort_term(term) |
176 |
176 |
177 def insert_attr_select_relation(rqlst, mainvar, rtype, role, attrname, |
177 def insert_attr_select_relation(rqlst, mainvar, rtype, role, attrname, |
178 sortfuncname=None, sortasc=True): |
178 sortfuncname=None, sortasc=True): |
179 """modify a syntax tree to retrieve only relevant attribute `attr` of `var`""" |
179 """modify a syntax tree to : |
|
180 * link a new variable to `mainvar` through `rtype` (where mainvar has `role`) |
|
181 * retrieve only the newly inserted variable and its `attrname` |
|
182 |
|
183 Sorting: |
|
184 * on `attrname` ascendant (`sortasc`=True) or descendant (`sortasc`=False) |
|
185 * on `sortfuncname`(`attrname`) if `sortfuncname` is specified |
|
186 * no sort if `sortasc` is None |
|
187 """ |
180 _cleanup_rqlst(rqlst, mainvar) |
188 _cleanup_rqlst(rqlst, mainvar) |
181 var = _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role) |
189 var = _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role) |
182 # not found, create one |
190 # not found, create one |
183 attrvar = rqlst.make_variable() |
191 attrvar = rqlst.make_variable() |
184 rqlst.add_relation(var, attrname, attrvar) |
192 rqlst.add_relation(var, attrname, attrvar) |
185 # if query is grouped, we have to add the attribute variable |
193 # if query is grouped, we have to add the attribute variable |
186 if rqlst.groupby: |
194 if rqlst.groupby: |
187 if not attrvar in rqlst.groupby: |
195 if not attrvar in rqlst.groupby: |
188 rqlst.add_group_var(attrvar) |
196 rqlst.add_group_var(attrvar) |
189 _set_orderby(rqlst, attrvar, sortasc, sortfuncname) |
197 if sortasc is not None: |
|
198 _set_orderby(rqlst, attrvar, sortasc, sortfuncname) |
190 # add attribute variable to selection |
199 # add attribute variable to selection |
191 rqlst.add_selected(attrvar) |
200 rqlst.add_selected(attrvar) |
192 # add is restriction if necessary |
201 # add is restriction if necessary |
193 if not mainvar.stinfo['typerels']: |
202 if not mainvar.stinfo['typerels']: |
194 etypes = frozenset(sol[mainvar.name] for sol in rqlst.solutions) |
203 etypes = frozenset(sol[mainvar.name] for sol in rqlst.solutions) |
356 def vocabulary(self): |
365 def vocabulary(self): |
357 """return vocabulary for this facet, eg a list of 2-uple (label, value) |
366 """return vocabulary for this facet, eg a list of 2-uple (label, value) |
358 """ |
367 """ |
359 rqlst = self.rqlst |
368 rqlst = self.rqlst |
360 rqlst.save_state() |
369 rqlst.save_state() |
|
370 if self.label_vid is not None and self.sortfunc is None: |
|
371 sort = None # will be sorted on label |
|
372 else: |
|
373 sort = self.sortasc |
361 try: |
374 try: |
362 mainvar = self.filtered_variable |
375 mainvar = self.filtered_variable |
363 insert_attr_select_relation(rqlst, mainvar, self.rtype, self.role, |
376 insert_attr_select_relation(rqlst, mainvar, self.rtype, self.role, |
364 self.target_attr, self.sortfunc, self.sortasc) |
377 self.target_attr, self.sortfunc, sort) |
365 try: |
378 try: |
366 rset = self.rqlexec(rqlst.as_string(), self.rset.args, self.rset.cachekey) |
379 rset = self.rqlexec(rqlst.as_string(), self.rset.args, self.rset.cachekey) |
367 except: |
380 except: |
368 self.exception('error while getting vocabulary for %s, rql: %s', |
381 self.exception('error while getting vocabulary for %s, rql: %s', |
369 self, rqlst.as_string()) |
382 self, rqlst.as_string()) |
576 var = self.rqlst.make_variable() |
590 var = self.rqlst.make_variable() |
577 if self.role == 'subject': |
591 if self.role == 'subject': |
578 self.rqlst.add_relation(self.filtered_variable, self.rtype, var) |
592 self.rqlst.add_relation(self.filtered_variable, self.rtype, var) |
579 else: |
593 else: |
580 self.rqlst.add_relation(var, self.rtype, self.filtered_variable) |
594 self.rqlst.add_relation(var, self.rtype, self.filtered_variable) |
|
595 |
581 |
596 |
582 ## html widets ################################################################ |
597 ## html widets ################################################################ |
583 |
598 |
584 class FacetVocabularyWidget(HTMLWidget): |
599 class FacetVocabularyWidget(HTMLWidget): |
585 |
600 |
728 % (cssclass, xml_escape(unicode(self.value)))) |
743 % (cssclass, xml_escape(unicode(self.value)))) |
729 self.w(u'<img src="%s" alt="%s"/> ' % (imgsrc, imgalt)) |
744 self.w(u'<img src="%s" alt="%s"/> ' % (imgsrc, imgalt)) |
730 self.w(u'<a href="javascript: {}">%s</a>' % xml_escape(self.label)) |
745 self.w(u'<a href="javascript: {}">%s</a>' % xml_escape(self.label)) |
731 self.w(u'</div>') |
746 self.w(u'</div>') |
732 |
747 |
|
748 |
733 class CheckBoxFacetWidget(HTMLWidget): |
749 class CheckBoxFacetWidget(HTMLWidget): |
734 selected_img = "black-check.png" |
750 selected_img = "black-check.png" |
735 unselected_img = "black-uncheck.png" |
751 unselected_img = "black-uncheck.png" |
736 |
752 |
737 def __init__(self, req, facet, value, selected): |
753 def __init__(self, req, facet, value, selected): |