Skip to content
Snippets Groups Projects
Commit 70db85e13042 authored by Arthur Lutz's avatar Arthur Lutz
Browse files

[views] pagination & updated tests

parent 9360d782237b
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,7 @@
web = 'http://www.cubicweb.org/project/%s' % distname
__depends__ = {'cubicweb': '>= 3.22.2', 'six': '>= 1.4.0',
'cwtags': None,
'elasticsearch': None,
'elasticsearch-dsl': None,
'bs4': None,
......
......@@ -7,7 +7,7 @@
from mock import patch
from elasticsearch_dsl.result import Response
from elasticsearch_dsl.faceted_search import FacetedResponse
from cubicweb.devtools import testlib
from cubicweb.cwconfig import CubicWebConfiguration
......@@ -112,7 +112,7 @@
'title': 'test'},
'_type': 'BaseContent',
'_score': 1}
return Response({'hits': {'hits': [result],
return FacetedResponse({'hits': {'hits': [result],
'total': 1
}})
......
......@@ -16,5 +16,4 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""elasticsearch search views"""
from elasticsearch import Elasticsearch
from elasticsearch.exceptions import NotFoundError
......@@ -20,8 +19,7 @@
from elasticsearch.exceptions import NotFoundError
from elasticsearch_dsl import Search
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import FacetedSearch, TermsFacet, DateHistogramFacet
from bs4 import BeautifulSoup
from logilab.mtconverter import xml_escape
......@@ -22,10 +20,11 @@
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import FacetedSearch, TermsFacet, DateHistogramFacet
from bs4 import BeautifulSoup
from logilab.mtconverter import xml_escape
import cwtags.tag as t
from cubicweb.view import StartupView
from cubes.elasticsearch.es import indexable_types
......@@ -28,7 +27,8 @@
from cubicweb.view import StartupView
from cubes.elasticsearch.es import indexable_types
def get_connection(config):
try:
connections.get_connection()
......@@ -40,4 +40,5 @@
index=index_name,
timeout=20)
class CWFacetedSearch(FacetedSearch):
......@@ -43,3 +44,2 @@
class CWFacetedSearch(FacetedSearch):
#doc_types = ['NewsContent', 'CommemorationItem', 'Circular',]
# fields that should be searched
......@@ -45,5 +45,5 @@
# fields that should be searched
fields = ["title^3", "name^3", "content^2", 'content', '_all',]
fields = ["title^3", "name^3", "content^2", 'content', '_all']
facets = {
# use bucket aggregations to define facets
......@@ -52,6 +52,7 @@
'commemoration_year': DateHistogramFacet(field='commemoration_year', interval='year'),
'year': DateHistogramFacet(field='year', interval='year'),
}
def __init__(self, query=None, filters={}, doc_types=None):
if doc_types:
self.doc_types = doc_types
......@@ -62,9 +63,10 @@
Add custom highlighting
"""
return search.highlight(*self.fields) \
.highlight_options(pre_tags="",
post_tags="",
fragment_size=150)
.highlight_options(pre_tags="",
post_tags="",
fragment_size=150)
class ElasticSearchView(StartupView):
__regid__ = "esearch"
......@@ -104,6 +106,7 @@
def display_results(self, response):
self.w(u'<div id="main-center" class="col-xs-10" role="main">')
self.pagination(response)
self.w(u'<ul>')
for result in response:
self.w(u'<li>')
......@@ -125,5 +128,6 @@
pass
self.w(u'</li>')
self.w(u'</ul>')
self.pagination(response)
self.w(u'</div>')
......@@ -128,5 +132,41 @@
self.w(u'</div>')
def pagination(self, response):
if response.hits.total < 10:
return
url_params = self._cw.form.copy()
with t.ul(self.w, klass="pagination") as ul:
current_page = int(url_params.get('page', 1))
url_params['page'] = current_page - 1
if current_page - 1 >= 1:
ul(t.li(t.a('<<<',
href=self._cw.build_url(**url_params))))
else:
ul(t.li(t.a('<<<')))
for i in range(0, min((response.hits.total / 10) + 1, 10)):
page = i + 1
url_params['page'] = page
url = self._cw.build_url(**url_params)
if page == current_page:
ul(t.li(t.a(t.b(page),
href=url)))
else:
ul(t.li(t.a(page,
href=url)))
if response.hits.total / 10 > 10:
ul(t.li(t.a("...")))
for i in range((response.hits.total / 10) - 3, response.hits.total / 10):
url_params['page'] = i
url = self._cw.build_url(**url_params)
ul(t.li(t.a(i,
href=url)))
url_params['page'] = current_page + 1
if current_page + 1 >= (response.hits.total / 10):
ul(t.li(t.a('>>>')))
else:
ul(t.li(t.a('>>>',
href=self._cw.build_url(**url_params))))
def display_facets(self, response):
self.w(u'''<aside id="aside-main-left" class="col-xs-2 cwjs-aside">
......@@ -137,6 +177,8 @@
''')
for attribute in ('cw_etype', 'creation_date'):
url_params = self._cw.form.copy()
if 'page' in url_params:
del url_params['page']
self.w(u'<div class="facetBody vocabularyFacet">')
self.w(u'<div class="facetTitle">{}</div>'.format(attribute))
for (tag, count, selected) in response.facets[attribute]:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment