def search(self, environ, start_response): """WSGI method, called by the wsgi app for requests that matches ``searchendpoint``.""" queryparams = self._search_parse_query(environ['QUERY_STRING']) res, pager = self._search_run_query(queryparams) if pager['totalresults'] == 1: title = "1 match" else: title = "%s matches" % pager['totalresults'] title += " for '%s'" % queryparams.get("q") body = html.Body() for r in res: if not 'dcterms_title' in r or r['dcterms_title'] is None: r['dcterms_title'] = r['uri'] if r.get('dcterms_identifier', False): r['dcterms_title'] = r['dcterms_identifier'] + ": " + r[ 'dcterms_title'] body.append( html.Div([ html.H2([elements.Link(r['dcterms_title'], uri=r['uri'])]), r.get('text', '') ], **{'class': 'hit'})) pagerelem = self._search_render_pager(pager, queryparams, environ['PATH_INFO']) body.append( html.Div([ html.P([ "Results %(firstresult)s-%(lastresult)s " "of %(totalresults)s" % pager ]), pagerelem ], **{'class': 'pager'})) data = self._transform(title, body, environ, template="xsl/search.xsl") return self._return_response(data, start_response)
def search(self, environ, start_response): """WSGI method, called by the wsgi app for requests that matches ``searchendpoint``.""" queryparams = self._search_parse_query(environ['QUERY_STRING']) # massage queryparams['issued'] if present, then restore it y = None if 'issued' in queryparams: y = int(queryparams['issued']) queryparams['issued'] = Between(datetime(y, 1, 1), datetime(y, 12, 31, 23, 59, 59)) boost_types = [("sfs", 10)] res, pager = self._search_run_query(queryparams, boost_types=boost_types) if y: queryparams['issued'] = str(y) if pager['totalresults'] == 1: title = "1 träff" else: title = "%s träffar" % pager['totalresults'] title += " för '%s'" % queryparams.get("q") body = html.Body() if hasattr(res, 'aggregations'): body.append( self._search_render_facets(res.aggregations, queryparams, environ)) for r in res: if 'label' not in r: label = r['uri'] elif isinstance(r['label'], list): label = str(r['label']) # flattens any nested element # structure, eg # <p><strong><em>foo</em></strong></p> # -> foo else: label = r['label'] rendered_hit = html.Div([ html.B([elements.Link(label, uri=r['uri'])], ** {'class': 'lead'}) ], **{'class': 'hit'}) if r.get('text'): rendered_hit.append(html.P([r.get('text', '')])) if 'innerhits' in r: for innerhit in r['innerhits']: rendered_hit.append(self._search_render_innerhit(innerhit)) body.append(rendered_hit) pagerelem = self._search_render_pager(pager, queryparams, environ['PATH_INFO']) body.append( html.Div([ html.P([ "Träff %(firstresult)s-%(lastresult)s " "av %(totalresults)s" % pager ]), pagerelem ], **{'class': 'pager'})) data = self._transform(title, body, environ, template="xsl/search.xsl") return self._return_response(data, start_response)
def handle_search(self, request, **values): # return Response("<h1>Hello search: " + request.args.get("q") +" </h1>", mimetype="text/html") res, pager = self._search_run_query(request.args) if pager['totalresults'] == 1: title = "1 match" else: title = "%s matches" % pager['totalresults'] title += " for '%s'" % request.args.get("q") body = html.Body() for r in res: if not 'dcterms_title' in r or r['dcterms_title'] is None: r['dcterms_title'] = r['uri'] if r.get('dcterms_identifier', False): r['dcterms_title'] = r['dcterms_identifier'] + ": " + r[ 'dcterms_title'] body.append( html.Div([ html.H2([elements.Link(r['dcterms_title'], uri=r['uri'])]), r.get('text', '') ], **{'class': 'hit'})) pagerelem = self._search_render_pager(pager, dict(request.args), request.path) body.append( html.Div([ html.P([ "Results %(firstresult)s-%(lastresult)s " "of %(totalresults)s" % pager ]), pagerelem ], **{'class': 'pager'})) data = self._transform(title, body, request.environ, template="xsl/search.xsl") return Response(data, mimetype="text/html")
def test_nested(self): # Make sure nested sections have the expected dcterms:isPartOf # relation if they have a @about property (could be from a # .uri property or dynamically constructed like # SectionalElement.as_xhtml) class MySection(el.CompoundElement): tagname = "div" classname = "mysection" partrelation = Namespace(util.ns['schema']).isPartOf body = el.Body([ el.Section([ el.Paragraph(['Some text']), el.Link("txt", uri="http://ex.org/ext"), el.Subsection([ el.Paragraph(['More text']), el.Subsubsection([el.Paragraph(['Even more text'])], ordinal="1.1.1", title="First subsubsection") ], ordinal="1.1", title="First subsection") ], ordinal="1", title="First section"), MySection([el.Paragraph(['Even more text'])], uri="http://example.org/s2") ]) want = """ <body xmlns="http://www.w3.org/1999/xhtml" about="http://localhost:8000/res/base/basefile"> <div content="First section" property="dcterms:title" about="http://localhost:8000/res/base/basefile#S1" typeof="bibo:DocumentPart" class="section"> <span rel="dcterms:isPartOf" href="http://localhost:8000/res/base/basefile"/> <span content="1" about="http://localhost:8000/res/base/basefile#S1" property="bibo:chapter"/> <p>Some text</p> <a href="http://ex.org/ext">txt</a> <div content="First subsection" property="dcterms:title" about="http://localhost:8000/res/base/basefile#S1.1" typeof="bibo:DocumentPart" class="subsection"> <span rel="dcterms:isPartOf" href="http://localhost:8000/res/base/basefile#S1"/> <span content="1.1" about="http://localhost:8000/res/base/basefile#S1.1" property="bibo:chapter"/> <p>More text</p> <div content="First subsubsection" property="dcterms:title" about="http://localhost:8000/res/base/basefile#S1.1.1" typeof="bibo:DocumentPart" class="subsubsection"> <span rel="dcterms:isPartOf" href="http://localhost:8000/res/base/basefile#S1.1"/> <span content="1.1.1" about="http://localhost:8000/res/base/basefile#S1.1.1" property="bibo:chapter"/> <p>Even more text</p> </div> </div> </div> <div about="http://example.org/s2" class="mysection"> <span rel="schema:isPartOf" href="http://localhost:8000/res/base/basefile"/> <p>Even more text</p> </div> </body>""" self._test_asxhtml(want, body)