def make_search_breadcumbs(attrs, values): attrs_values = get_pairs(attrs, values) search_breadcumbs = [] search_url = urlresolvers.reverse('ssearch:frontend:index') attrs_prepare = [] values_prepare = [] for attr, value in attrs_values: attr_url_part = u'attr=' + attr value_url_part = u'q=' + urlquote(value) search_breadcumbs.append({ 'attr': attr, 'title': get_attr_title(attr), 'href': search_url + u'?' + u'&'.join(attrs_prepare) + u'&' + attr_url_part + u'&' + u'&'.join( values_prepare) + u'&' + value_url_part, 'value': get_attr_value_title(attr, value) }) attrs_prepare.append(attr_url_part) values_prepare.append(value_url_part) return search_breadcumbs
def replace_facet_values(facets, key=None, replacer=None): titled_facets = {} for facet in facets.keys(): titled_facets[facet] = { 'title': get_attr_title(facet) } titled_facets[facet]['values'] = [] titled_valuses = titled_facets[facet]['values'] for fvalue in facets[facet]: titled_valuses.append( (fvalue[0], fvalue[1], get_attr_value_title(facet, fvalue[0])) ) return titled_facets
def index(request, catalog='uc'): # sc = SearchCriteria(u"AND") # sc.add_attr(u'name', u'zi zi') # sc.add_attr(u'surname', u'do do') # # sc2 = SearchCriteria(u"OR") # sc2.add_attr(u'category', u'phis') # sc2.add_attr(u'category', u'math') # sc.add_search_criteria(sc2) # # print sc.to_lucene_query() # print sc.to_dict() # print SearchCriteria.from_dict(sc.to_dict()).to_human_read() uc = init_solr_collection(catalog) faset_params = FacetParams() faset_params.fields = get_facet_attrs() faset_params.facet_sort = FACET_SORT attrs, values = extract_request_query_attrs(request) search_breadcumbs = make_search_breadcumbs(attrs, values) attrs = reverse_search_attrs(attrs) sort = request.GET.get('sort', u'relevance') order = request.GET.get('order', u'asc') solr_sort = [] if sort != u'relevance': solr_sort.append("%s %s" % (sort, order)) if not values or not attrs: (colls, pivote_root) = collections() coll_stat = [] all_documents_count = 0 for coll in colls: coll_info = { 'name': coll[0], 'docs': coll[1], 'views': ViewLog.get_view_count(coll[0]) } all_documents_count += int(coll[1]) coll_stat.append(coll_info) # now = datetime.date.today() past = now - datetime.timedelta(30) RecordContent.objects.filter(create_date_time__gte=past, create_date_time__lte=now) stat = { 'all_documents_count': all_documents_count, 'coll_stat': coll_stat } return render(request, 'ssearch/frontend/project.html', { 'attrs': get_search_attrs(), 'pattr': request.GET.getlist('pattr', None), 'stat': stat, 'pivot_tree': pivote_root.to_html(), }) query = construct_query(attrs=attrs, values=values) hl = [] if request.GET.get('attr', u'') == 'full_text_tru': hl.append('full_text_tru') result = uc.search(query=query, fields=['id'], faset_params=faset_params, hl=hl, sort=solr_sort) paginator = Paginator(result, 15) page = request.GET.get('page') try: result_page = paginator.page(page) except PageNotAnInteger: result_page = paginator.page(1) except EmptyPage: result_page = paginator.page(paginator.num_pages) docs = result.get_docs() record_ids = [] for doc in docs: record_ids.append(doc['id']) view = request.GET.get('view', u'table') highlighting = result.get_highlighting() records = get_records(record_ids) for record in records: record_obj = junimarc.chel_json_schema.record_from_json(record['dict']) marc_query = junimarc.marc_query.MarcQuery(record_obj) ft_links = record_templates.get_full_text_links(marc_query) record_template = record_templates.RusmarcTemplate(record.get('dict', {})) record['tpl'] = record_template record['extended'] = { 'subject_heading': subject_render(record['dict']) } content_tree = record['tree'] if view == 'card': record['library_cadr'] = get_library_card(content_tree) else: record['dict'] = get_content_dict(content_tree) # record['library_cadr'] = get_library_card(content_tree) if highlighting and record['id'] in highlighting: record['highlighting'] = highlighting[record['id']] if 'dict' in record: for attribute in record['dict']: titled_values = [] values = record['dict'][attribute] for value in values: titled_values.append(get_attr_value_title(attribute, value)) record['dict'][attribute] = titled_values record['ft_links'] = ft_links facets = result.get_facets() info = { # 'qtime': result.get_qtime() / 1000.0, 'num_found': result.get_num_found(), } facets = replace_facet_values(facets) request_params = { 'q': request.GET.getlist('q', None), 'attr': request.GET.getlist('attr', None), 'pq': request.GET.getlist('pq', None), 'pattr': request.GET.getlist('pattr', None), } facets = get_orderd_facets(facets) return render(request, 'ssearch/frontend/index.html', { 'records': records, 'facets': facets, 'info': info, 'attrs': get_search_attrs(), 'search_breadcumbs': search_breadcumbs, 'request_params': simplejson.dumps(request_params, ensure_ascii=False), 'result_page': result_page, 'sort_attrs': sort_attrs })
def get_pivot_titles(pivot_facets): for pivot_facet in pivot_facets: pivot_field = pivot_facet.get('field', '') if pivot_field: pivot_facet['title'] = titles.get_attr_value_title(pivot_field, pivot_facet.get('value', '')) get_pivot_titles(pivot_facet.get('pivot', []))