# HG changeset patch # User Arthur Lutz <arthur.lutz@logilab.fr> # Date 1464954137 -7200 # Fri Jun 03 13:42:17 2016 +0200 # Node ID af48c8eb3adb45811d0812bf4b984bf9ffdb5a86 # Parent 4cc49ddcc09455e6f391b0bcf0d1aa4b15dd61db [views] move things around so they can be customized in another cube (and remove specific code) diff --git a/views.py b/views.py --- a/views.py +++ b/views.py @@ -44,16 +44,12 @@ class CWFacetedSearch(FacetedSearch): # fields that should be searched - fields = ["unitid^6", "title^3", "unittitle^3", - "name^3", "content^2", 'content', '_all'] + fields = ["title^3", "description^2", '_all'] facets = { # use bucket aggregations to define facets 'cw_etype': TermsFacet(field='cw_etype'), - 'unitid': TermsFacet(field='unitid'), 'creation_date': DateHistogramFacet(field='creation_date', interval='month'), - 'commemoration_year': DateHistogramFacet(field='commemoration_year', interval='year'), - 'year': DateHistogramFacet(field='year', interval='year'), } def __init__(self, query=None, filters={}, doc_types=None): @@ -90,7 +86,7 @@ self.w(u'<h1>%s</h1>' % self._cw._('Recherche')) query_string = xml_escape(self._cw.form.get('search', '')) - self.w(u'<h2>Résultats pour : <em>%s</em></h2>' % query_string) + self.w(u'<h2>Resultats pour : <em>%s</em></h2>' % query_string) get_connection(self._cw.vreg.config) facet_selections = {} start, stop = 0, 10 @@ -100,24 +96,39 @@ if key == 'page': start = (max(int(value) - 1, 0)) * 10 stop = start + 10 - indexable = indexable_types(self._cw.vreg.schema) - if query_string.startswith('cote:'): - query_string = query_string.split(':')[1] - facet_selections['unitid'] = query_string - search = CWFacetedSearch(query_string, - facet_selections, - doc_types=indexable)[start:stop] + search = self.customize_search(query_string, facet_selections, + start, stop) try: response = search.execute() except NotFoundError: self.w(u'index not found in elasticsearch') return - self.w(u'Résultats: %s' % response.hits.total) + self.w(u'Resultats: %s' % response.hits.total) if hasattr(response, 'facets'): self.display_facets(response) self.display_results(response) + def customize_search(self, query_string, facet_selections, + start=0, stop=10): + ''' + This is where one can customize the search by modifying the + query string and facet selection in an inherited class. + + For example : + * add specific keywords sur as id:text and + add them the facet_selection + * use your own CWFacetedSearch class to modify fields + and facets + ''' + indexable = indexable_types(self._cw.vreg.schema) + return CWFacetedSearch(query_string, + facet_selections, + doc_types=indexable)[start:stop] + def display_results(self, response): + ''' + Display results obtained from elasticsearch + ''' self.w(u'<div id="main-center" class="col-xs-10" role="main">') self.pagination(response) self.w(u'<ul>') @@ -128,8 +139,7 @@ infos['keys'] = result.to_dict().keys() infos['url'] = infos['cwuri'].startswith( '_auto_generated') and infos['eid'] or infos['cwuri'] - infos.setdefault( - 'title', infos.get('name', infos.get('reference', infos.get('unittitle', u'n/a')))) + self.customize_infos(infos) try: self.w( u'<a href="%(url)s">%(title)s</a> (%(_score).2f)<br/>' % (infos)) @@ -149,7 +159,18 @@ self.pagination(response) self.w(u'</div>') + def customize_infos(self, infos): + ''' + This is where one can customize the infos being displayed + + For example : set the title according to your rules and data set + ''' + pass + def pagination(self, response): + ''' + Pagination HTML generation + ''' if response.hits.total < 10: return url_params = self._cw.form.copy() @@ -200,6 +221,13 @@ href=url))) return url + @property + def facets_to_display(self): + ''' + Method to list facets to display (can be customized) + ''' + return ('cw_etype', ) + def display_facets(self, response): self.w(u'''<aside id="aside-main-left" class="col-xs-2 cwjs-aside"> <div class="panel panel-default contextFreeBox facet_filterbox"> @@ -207,7 +235,7 @@ <div class="panel-title">Facettes</div> </div> ''') - for attribute in ('cw_etype', 'creation_date'): + for attribute in self.facets_to_display: url_params = self._cw.form.copy() if 'page' in url_params: del url_params['page']