def search_facets(request, id=None, name=None, selected=False): collection = get_object_or_404(filter_by_access(request.user, Collection), id=id) if id else None # get parameters relevant for search criteria = request.GET.getlist("c") remove = request.GET.get("rem", None) if remove and remove in criteria: criteria.remove(remove) keywords = request.GET.get("kw", "") user = request.user if selected: selected = request.session.get("selected_records", ()) (hits, records, search_facets, orfacet, query, fields) = run_search( user, collection, criteria, keywords, selected=selected, remove=remove, produce_facets=True ) if collection: url = reverse("solr-search-collection", kwargs={"id": collection.id, "name": collection.name}) elif selected: url = reverse("solr-selected") else: url = reverse("solr-search") q = request.GET.copy() q = clean_record_selection_vars(q) q.pop("or", None) q.pop("rem", None) q.pop("action", None) q.pop("page", None) q.pop("op", None) q.setlist("c", criteria) qurl = q.urlencode() limit_url = "%s?%s%s" % (url, qurl, qurl and "&" or "") # sort facets by label facets = sorted(search_facets.values(), key=lambda f: f.label) # clean facet items for f in facets: f.clean_result(hits) # remove facets with only no filter options facets = filter(lambda f: len(f.facets) > 0, facets) html = render_to_string( "results_facets.html", {"limit_url": limit_url, "facets": facets}, context_instance=RequestContext(request) ) mode, ids = get_collection_visibility_preferences(user) hash = calculate_hash(getattr(user, "id", 0), collection, criteria, keywords, selected, remove, mode, str(ids)) cache.set("search_facets_html_%s" % hash, html, 300) return dict(html=html)
def collection_visibility_preferences(context): user = context.get('user') mode, ids = get_collection_visibility_preferences(user) cform = get_collection_visibility_prefs_form(user) form = cform(initial=dict(show_or_hide=mode, collections=ids)) return { 'form': form, 'request': context['request'], }
def search_facets(request, id=None, name=None, selected=False): collection = id and get_object_or_404(filter_by_access(request.user, Collection), id=id) or None # get parameters relevant for search criteria = request.GET.getlist('c') remove = request.GET.get('rem', None) if remove and remove in criteria: criteria.remove(remove) keywords = request.GET.get('kw', '') user = request.user if selected: selected = request.session.get('selected_records', ()) (hits, records, search_facets, orfacet, query, fields) = run_search(user, collection, criteria, keywords, selected=selected, remove=remove, produce_facets=True) if collection: url = reverse('solr-search-collection', kwargs={'id': collection.id, 'name': collection.name}) elif selected: url = reverse('solr-selected') else: url = reverse('solr-search') q = request.GET.copy() q = clean_record_selection_vars(q) q.pop('or', None) q.pop('rem', None) q.pop('action', None) q.pop('page', None) q.pop('op', None) q.setlist('c', criteria) qurl = q.urlencode() limit_url = "%s?%s%s" % (url, qurl, qurl and '&' or '') # sort facets by label facets = sorted(search_facets.values(), key=lambda f: f.label) # clean facet items for f in facets: f.clean_result(hits) # remove facets with only no filter options facets = filter(lambda f: len(f.facets) > 0, facets) html = render_to_string('results_facets.html', { 'limit_url': limit_url, 'facets': facets }, context_instance=RequestContext(request)) mode, ids = get_collection_visibility_preferences(user) hash = calculate_hash(getattr(user, 'id', 0), collection, criteria, keywords, selected, remove, mode, str(ids), ) cache.set('search_facets_html_%s' % hash, html, 300) return dict(html=html)
def search(request, id=None, name=None, selected=False, json=False): collection = id and get_object_or_404(filter_by_access(request.user, Collection), id=id) or None if request.method == "POST": update_record_selection(request) # redirect to get request with updated parameters q = request.GET.copy() q.update(request.POST) q = clean_record_selection_vars(q) for i, v in q.items(): if i != 'c': q[i] = v # replace multiple values with last one except for criteria ('c') q.pop('v.x', None) q.pop('v.y', None) q.pop('x', None) q.pop('y', None) return HttpResponseRedirect(request.path + '?' + q.urlencode()) # get parameters relevant for search criteria = request.GET.getlist('c') remove = request.GET.get('rem', None) if remove and remove in criteria: criteria.remove(remove) keywords = request.GET.get('kw', '') # get parameters relevant for view viewmode = request.GET.get('v', 'thumb') if viewmode == 'list': pagesize = max(min(safe_int(request.GET.get('ps', '50'), 50), 100), 5) else: pagesize = max(min(safe_int(request.GET.get('ps', '30'), 30), 50), 5) page = safe_int(request.GET.get('page', '1'), 1) sort = request.GET.get('s', 'title_sort').lower() if not sort.endswith(" asc") and not sort.endswith(" desc"): sort += " asc" orquery = request.GET.get('or', None) user = request.user if request.GET.has_key('action'): page = safe_int(request.GET.get('op', '1'), 1) if selected: selected = request.session.get('selected_records', ()) (hits, records, search_facets, orfacet, query, fields) = run_search(user, collection, criteria, keywords, sort, page, pagesize, orquery, selected, remove, produce_facets=False) if json: return (hits, records, viewmode) if collection: url = reverse('solr-search-collection', kwargs={'id': collection.id, 'name': collection.name}) furl = reverse('solr-search-collection-facets', kwargs={'id': collection.id, 'name': collection.name}) elif hasattr(selected, '__len__'): url = reverse('solr-selected') furl = reverse('solr-selected-facets') else: url = reverse('solr-search') furl = reverse('solr-search-facets') q = request.GET.copy() q = clean_record_selection_vars(q) q.pop('or', None) q.pop('rem', None) q.pop('action', None) q.pop('page', None) q.pop('op', None) q.pop('v.x', None) q.pop('v.y', None) q.pop('x', None) q.pop('y', None) q['s'] = q.get('s', sort) q['v'] = q.get('v', 'thumb') q.setlist('c', criteria) hiddenfields = [('op', page)] #for f in q: # if f != 'kw': # for l in q.getlist(f): # hiddenfields.append((f, l)) qurl = q.urlencode() q.setlist('c', filter(lambda c: c != orquery, criteria)) qurl_orquery = q.urlencode() limit_url = "%s?%s%s" % (url, qurl, qurl and '&' or '') limit_url_orquery = "%s?%s%s" % (url, qurl_orquery, qurl_orquery and '&' or '') facets_url = "%s?%s%s" % (furl, qurl, qurl and '&' or '') form_url = "%s?%s" % (url, q.urlencode()) prev_page_url = None next_page_url = None if page > 1: q['page'] = page - 1 prev_page_url = "%s?%s" % (url, q.urlencode()) if page < (hits - 1) / pagesize + 1: q['page'] = page + 1 next_page_url = "%s?%s" % (url, q.urlencode()) def readable_criteria(c): (f, o) = c.split(':', 1) negated = f.startswith('-') f = f[1 if negated else 0:] if search_facets.has_key(f): return dict(facet=c, term=search_facets[f].display_value(o), label=search_facets[f].label, negated=negated, or_available=not negated and search_facets[f].or_available()) else: return dict(facet=c, term=o, label='Unknown criteria', negated=negated, or_available=False) def reduce_federated_search_query(q, c): (f, o) = c.split(':', 1) if f.startswith('-') or not search_facets.has_key(f): # can't negate in federated search return q v = search_facets[f].federated_search_query(o) return v if not q else '%s %s' % (q, v) mode, ids = get_collection_visibility_preferences(user) hash = calculate_hash(getattr(user, 'id', 0), collection, criteria, keywords, selected, remove, mode, str(ids), ) print hash facets = cache.get('search_facets_html_%s' % hash) sort = sort.startswith('random') and 'random' or sort.split()[0] sort = sort.endswith('_sort') and sort[:-5] or sort federated_search_query = reduce(reduce_federated_search_query, criteria, keywords) federated_search = sidebar_api_raw( request, federated_search_query, cached_only=True) if federated_search_query else None return render_to_response('results.html', {'criteria': map(readable_criteria, criteria), 'query': query, 'keywords': keywords, 'hiddenfields': hiddenfields, 'records': records, 'hits': hits, 'page': page, 'pages': (hits - 1) / pagesize + 1, 'pagesize': pagesize, 'prev_page': prev_page_url, 'next_page': next_page_url, 'reset_url': url, 'form_url': form_url, 'limit_url': limit_url, 'limit_url_orquery': limit_url_orquery, 'facets': facets, 'facets_url': facets_url, 'orfacet': orfacet, 'orquery': orquery, 'sort': sort, 'random': random.random(), 'viewmode': viewmode, 'federated_sources': bool(available_federated_sources()), 'federated_search': federated_search, 'federated_search_query': federated_search_query, 'pagination_helper': [None] * hits, 'has_record_created_criteria': any(f.startswith('created:') for f in criteria), 'has_last_modified_criteria': any(f.startswith('modified:') for f in criteria), }, context_instance=RequestContext(request))
def _generate_query(search_facets, user, collection, criteria, keywords, selected, *exclude): # add owned tag facet search_facets['ownedtag'] = OwnedTagSearchFacet() fields = {} for c in criteria: if (c in exclude) or (':' not in c): continue (f, o) = c.split(':', 1) if f.startswith('-'): f = 'NOT ' + f[1:] fname = f.rsplit(' ',1)[-1] # create exact match criteria on the fly if needed if fname.endswith('_s') and not search_facets.has_key(fname): search_facets[fname] = ExactValueSearchFacet(fname) if search_facets.has_key(fname): o = search_facets[fname].process_criteria(o, user) fields.setdefault(f, []).append('(' + o.replace('|', ' OR ') + ')') fields = map(lambda (name, crit): '%s:(%s)' % (name, (name.startswith('NOT ') and ' OR ' or ' AND ').join(crit)), fields.iteritems()) def build_keywords(q, k): k = k.lower() if k == 'and' or k == 'or': return q + ' ' + k.upper() elif q.endswith(' AND') or q.endswith(' OR'): return q + ' ' + k else: return q + ' AND ' + k if keywords: keywords = reduce(build_keywords, keywords.split()) query = '' if fields: query = ' AND '.join(fields) if keywords: query = query and '%s AND (%s)' % (query, keywords) or '(%s)' % keywords if not query: query = '*:*' if collection: query = 'collections:%s AND %s' % (collection.id, query) if hasattr(selected, '__len__'): query = 'id:(%s) AND %s' % (' '.join(map(str, selected or [-1])), query) if not user.is_superuser: collections = ' '.join(map(str, filter_by_access(user, Collection).values_list('id', flat=True))) c = [] if collections: # access through readable collection when no record ACL set c.append('collections:(%s) AND acl_read:default' % collections) if user.id: # access through ownership c.append('owner:%s' % user.id) # access through record ACL groups = ' '.join( 'g%d' % id for id in user.groups.values_list('id', flat=True) ) if groups: groups = '((%s) AND NOT (%s)) OR ' % (groups, groups.upper()) c.append('acl_read:((%su%d) AND NOT U%d)' % (groups, user.id, user.id)) else: # access through record ACL c.append('acl_read:anon') if c: query = '((%s)) AND %s' % (') OR ('.join(c), query) else: query = 'id:"-1"' mode, ids = get_collection_visibility_preferences(user) if ids: query += ' AND %sallcollections:(%s)' % ( '-' if mode == 'show' else '', ' '.join(map(str, ids)), ) return query
def search(request, id=None, name=None, selected=False, json=False): collection = get_object_or_404(filter_by_access(request.user, Collection), id=id) if id else None if request.method == "POST": update_record_selection(request) # redirect to get request with updated parameters q = request.GET.copy() q.update(request.POST) q = clean_record_selection_vars(q) for i, v in q.items(): if i != "c": # replace multiple values with last one # except for criteria ('c') q[i] = v q.pop("v.x", None) q.pop("v.y", None) q.pop("x", None) q.pop("y", None) return HttpResponseRedirect(request.path + "?" + q.urlencode()) # get parameters relevant for search criteria = request.GET.getlist("c") remove = request.GET.get("rem", None) if remove and remove in criteria: criteria.remove(remove) keywords = request.GET.get("kw", "") # get parameters relevant for view viewmode = request.GET.get("v", "thumb") if viewmode == "list": pagesize = max(min(safe_int(request.GET.get("ps", "50"), 50), 100), 5) else: pagesize = max(min(safe_int(request.GET.get("ps", "30"), 30), 50), 5) page = safe_int(request.GET.get("page", "1"), 1) sort = request.GET.get("s", "title_sort").lower() if not sort.endswith(" asc") and not sort.endswith(" desc"): sort += " asc" orquery = request.GET.get("or", None) user = request.user if "action" in request.GET: page = safe_int(request.GET.get("op", "1"), 1) if selected: selected = request.session.get("selected_records", ()) hits, records, search_facets, orfacet, query, fields = run_search( user, collection, criteria, keywords, sort, page, pagesize, orquery, selected, remove, produce_facets=False ) if json: return (hits, records, viewmode) if collection: url = reverse("solr-search-collection", kwargs={"id": collection.id, "name": collection.name}) furl = reverse("solr-search-collection-facets", kwargs={"id": collection.id, "name": collection.name}) elif hasattr(selected, "__len__"): url = reverse("solr-selected") furl = reverse("solr-selected-facets") else: url = reverse("solr-search") furl = reverse("solr-search-facets") q = request.GET.copy() q = clean_record_selection_vars(q) q.pop("or", None) q.pop("rem", None) q.pop("action", None) q.pop("page", None) q.pop("op", None) q.pop("v.x", None) q.pop("v.y", None) q.pop("x", None) q.pop("y", None) q["s"] = q.get("s", sort) q["v"] = q.get("v", "thumb") q.setlist("c", criteria) hiddenfields = [("op", page)] qurl = q.urlencode() q.setlist("c", filter(lambda c: c != orquery, criteria)) qurl_orquery = q.urlencode() limit_url = "%s?%s%s" % (url, qurl, qurl and "&" or "") limit_url_orquery = "%s?%s%s" % (url, qurl_orquery, qurl_orquery and "&" or "") facets_url = "%s?%s%s" % (furl, qurl, qurl and "&" or "") form_url = "%s?%s" % (url, q.urlencode()) prev_page_url = None next_page_url = None if page > 1: q["page"] = page - 1 prev_page_url = "%s?%s" % (url, q.urlencode()) if page < (hits - 1) / pagesize + 1: q["page"] = page + 1 next_page_url = "%s?%s" % (url, q.urlencode()) def readable_criteria(c): (f, o) = c.split(":", 1) negated = f.startswith("-") f = f[1 if negated else 0 :] if f in search_facets: return dict( facet=c, term=search_facets[f].display_value(o), label=search_facets[f].label, negated=negated, or_available=not negated and search_facets[f].or_available(), ) else: return dict(facet=c, term=o, label="Unknown criteria", negated=negated, or_available=False) def reduce_federated_search_query(q, c): (f, o) = c.split(":", 1) if f.startswith("-") or f not in search_facets: # can't negate in federated search return q v = search_facets[f].federated_search_query(o) return v if not q else "%s %s" % (q, v) mode, ids = get_collection_visibility_preferences(user) hash = calculate_hash(getattr(user, "id", 0), collection, criteria, keywords, selected, remove, mode, str(ids)) facets = cache.get("search_facets_html_%s" % hash) sort = sort.startswith("random") and "random" or sort.split()[0] sort = sort.endswith("_sort") and sort[:-5] or sort federated_search_query = reduce(reduce_federated_search_query, criteria, keywords) federated_search = ( sidebar_api_raw(request, federated_search_query, cached_only=True) if federated_search_query else None ) return render_to_response( "results.html", { "criteria": map(readable_criteria, criteria), "query": query, "keywords": keywords, "hiddenfields": hiddenfields, "records": records, "hits": hits, "page": page, "pages": (hits - 1) / pagesize + 1, "pagesize": pagesize, "prev_page": prev_page_url, "next_page": next_page_url, "reset_url": url, "form_url": form_url, "limit_url": limit_url, "limit_url_orquery": limit_url_orquery, "facets": facets, "facets_url": facets_url, "orfacet": orfacet, "orquery": orquery, "sort": sort, "random": random.random(), "viewmode": viewmode, "federated_sources": bool(available_federated_sources(request.user)), "federated_search": federated_search, "federated_search_query": federated_search_query, "pagination_helper": [None] * hits, "has_record_created_criteria": any(f.startswith("created:") for f in criteria), "has_last_modified_criteria": any(f.startswith("modified:") for f in criteria), }, context_instance=RequestContext(request), )
def _generate_query(search_facets, user, collection, criteria, keywords, selected, *exclude): # add owned tag facet search_facets["ownedtag"] = OwnedTagSearchFacet() fields = {} for c in criteria: if (c in exclude) or (":" not in c): continue (f, o) = c.split(":", 1) if f.startswith("-"): f = "NOT " + f[1:] fname = f.rsplit(" ", 1)[-1] # create exact match criteria on the fly if needed if fname.endswith("_s") and not fname in search_facets: search_facets[fname] = ExactValueSearchFacet(fname) if fname in search_facets: o = search_facets[fname].process_criteria(o, user) fields.setdefault(f, []).append("(" + o.replace("|", " OR ") + ")") fields = map( lambda (name, crit): "%s:(%s)" % (name, (name.startswith("NOT ") and " OR " or " AND ").join(crit)), fields.iteritems(), ) def build_keywords(q, k): k = k.lower() if k == "and" or k == "or": return q + " " + k.upper() elif q.endswith(" AND") or q.endswith(" OR"): return q + " " + k else: return q + " AND " + k if keywords: keywords = reduce(build_keywords, keywords.split()) query = "" if fields: query = " AND ".join(fields) if keywords: if query: query = "%s AND (%s)" % (query, keywords) else: query = "(%s)" % keywords if not query: query = "*:*" if collection: query = "collections:%s AND %s" % (collection.id, query) if hasattr(selected, "__len__"): query = "id:(%s) AND %s" % (" ".join(map(str, selected or [-1])), query) if not user.is_superuser: collections = " ".join(map(str, filter_by_access(user, Collection).values_list("id", flat=True))) c = [] if collections: # access through readable collection when no record ACL set c.append("collections:(%s) AND acl_read:default" % collections) if user.id: # access through ownership c.append("owner:%s" % user.id) # access through record ACL groups = " ".join("g%d" % id for id in user.groups.values_list("id", flat=True)) if groups: groups = "((%s) AND NOT (%s)) OR " % (groups, groups.upper()) c.append("acl_read:((%su%d) AND NOT U%d)" % (groups, user.id, user.id)) else: # access through record ACL c.append("acl_read:anon") if c: query = "((%s)) AND %s" % (") OR (".join(c), query) else: query = 'id:"-1"' mode, ids = get_collection_visibility_preferences(user) if ids: query += " AND %sallcollections:(%s)" % ("-" if mode == "show" else "", " ".join(map(str, ids))) return query
def search_facets(request, id=None, name=None, selected=False): collection = get_object_or_404( filter_by_access(request.user, Collection), id=id) if id else None # get parameters relevant for search criteria = request.GET.getlist('c') remove = request.GET.get('rem', None) if remove and remove in criteria: criteria.remove(remove) keywords = request.GET.get('kw', '') user = request.user if selected: selected = request.session.get('selected_records', ()) (hits, records, search_facets, orfacet, query, fields) = run_search( user, collection, criteria, keywords, selected=selected, remove=remove, produce_facets=True) if collection: url = reverse('solr-search-collection', kwargs={'id': collection.id, 'name': collection.name}) elif selected: url = reverse('solr-selected') else: url = reverse('solr-search') q = request.GET.copy() q = clean_record_selection_vars(q) q.pop('or', None) q.pop('rem', None) q.pop('action', None) q.pop('page', None) q.pop('op', None) q.setlist('c', criteria) qurl = q.urlencode() limit_url = "%s?%s%s" % (url, qurl, qurl and '&' or '') # sort facets by label facets = sorted(search_facets.values(), key=lambda f: f.label) # clean facet items for f in facets: f.clean_result(hits) # remove facets with only no filter options facets = filter(lambda f: len(f.facets) > 0, facets) html = render_to_string( 'results_facets.html', { 'limit_url': limit_url, 'facets': facets }, context_instance=RequestContext(request)) mode, ids = get_collection_visibility_preferences(user) hash = calculate_hash(getattr(user, 'id', 0), collection, criteria, keywords, selected, remove, mode, str(ids), ) cache.set('search_facets_html_%s' % hash, html, 300) return dict(html=html)
def search(request, id=None, name=None, selected=False, json=False): collection = get_object_or_404( filter_by_access(request.user, Collection), id=id) if id else None if request.method == "POST": update_record_selection(request) # redirect to get request with updated parameters q = request.GET.copy() q.update(request.POST) q = clean_record_selection_vars(q) for i, v in q.items(): if i != 'c': # replace multiple values with last one # except for criteria ('c') q[i] = v q.pop('v.x', None) q.pop('v.y', None) q.pop('x', None) q.pop('y', None) return HttpResponseRedirect(request.path + '?' + q.urlencode()) # get parameters relevant for search criteria = request.GET.getlist('c') remove = request.GET.get('rem', None) if remove and remove in criteria: criteria.remove(remove) keywords = request.GET.get('kw', '') # get parameters relevant for view viewmode = request.GET.get('v', 'thumb') if viewmode == 'list': pagesize = max(min(safe_int(request.GET.get('ps', '50'), 50), 100), 5) else: pagesize = max(min(safe_int(request.GET.get('ps', '30'), 30), 50), 5) page = safe_int(request.GET.get('page', '1'), 1) sort = request.GET.get('s', 'title_sort').lower() if not sort.endswith(" asc") and not sort.endswith(" desc"): sort += " asc" orquery = request.GET.get('or', None) user = request.user if 'action' in request.GET: page = safe_int(request.GET.get('op', '1'), 1) if selected: selected = request.session.get('selected_records', ()) hits, records, search_facets, orfacet, query, fields = run_search( user, collection, criteria, keywords, sort, page, pagesize, orquery, selected, remove, produce_facets=False) if json: return (hits, records, viewmode) if collection: url = reverse('solr-search-collection', kwargs={'id': collection.id, 'name': collection.name}) furl = reverse('solr-search-collection-facets', kwargs={'id': collection.id, 'name': collection.name}) elif hasattr(selected, '__len__'): url = reverse('solr-selected') furl = reverse('solr-selected-facets') else: url = reverse('solr-search') furl = reverse('solr-search-facets') q = request.GET.copy() q = clean_record_selection_vars(q) q.pop('or', None) q.pop('rem', None) q.pop('action', None) q.pop('page', None) q.pop('op', None) q.pop('v.x', None) q.pop('v.y', None) q.pop('x', None) q.pop('y', None) q['s'] = q.get('s', sort) q['v'] = q.get('v', 'thumb') q.setlist('c', criteria) hiddenfields = [('op', page)] qurl = q.urlencode() q.setlist('c', filter(lambda c: c != orquery, criteria)) qurl_orquery = q.urlencode() limit_url = "%s?%s%s" % (url, qurl, qurl and '&' or '') limit_url_orquery = "%s?%s%s" % ( url, qurl_orquery, qurl_orquery and '&' or '') facets_url = "%s?%s%s" % (furl, qurl, qurl and '&' or '') form_url = "%s?%s" % (url, q.urlencode()) prev_page_url = None next_page_url = None if page > 1: q['page'] = page - 1 prev_page_url = "%s?%s" % (url, q.urlencode()) if page < (hits - 1) / pagesize + 1: q['page'] = page + 1 next_page_url = "%s?%s" % (url, q.urlencode()) def readable_criteria(c): (f, o) = c.split(':', 1) negated = f.startswith('-') f = f[1 if negated else 0:] if f in search_facets: return dict( facet=c, term=search_facets[f].display_value(o), label=search_facets[f].label, negated=negated, or_available=not negated and search_facets[f].or_available(), ) else: return dict( facet=c, term=o, label='Unknown criteria', negated=negated, or_available=False, ) def reduce_federated_search_query(q, c): (f, o) = c.split(':', 1) if f.startswith('-') or f not in search_facets: # can't negate in federated search return q v = search_facets[f].federated_search_query(o) return v if not q else '%s %s' % (q, v) mode, ids = get_collection_visibility_preferences(user) hash = calculate_hash(getattr(user, 'id', 0), collection, criteria, keywords, selected, remove, mode, str(ids), ) facets = cache.get('search_facets_html_%s' % hash) sort = sort.startswith('random') and 'random' or sort.split()[0] sort = sort.endswith('_sort') and sort[:-5] or sort federated_search_query = reduce( reduce_federated_search_query, criteria, keywords) federated_search = sidebar_api_raw( request, federated_search_query, cached_only=True ) if federated_search_query else None return render_to_response('results.html', { 'criteria': map(readable_criteria, criteria), 'query': query, 'keywords': keywords, 'hiddenfields': hiddenfields, 'records': records, 'hits': hits, 'page': page, 'pages': (hits - 1) / pagesize + 1, 'pagesize': pagesize, 'prev_page': prev_page_url, 'next_page': next_page_url, 'reset_url': url, 'form_url': form_url, 'limit_url': limit_url, 'limit_url_orquery': limit_url_orquery, 'facets': facets, 'facets_url': facets_url, 'orfacet': orfacet, 'orquery': orquery, 'sort': sort, 'random': random.random(), 'viewmode': viewmode, 'federated_sources': bool(available_federated_sources(request.user)), 'federated_search': federated_search, 'federated_search_query': federated_search_query, 'pagination_helper': [None] * hits, 'has_record_created_criteria': any( f.startswith('created:') for f in criteria), 'has_last_modified_criteria': any( f.startswith('modified:') for f in criteria), }, context_instance=RequestContext(request))
def _generate_query(search_facets, user, collection, criteria, keywords, selected, *exclude): # add owned tag facet search_facets['ownedtag'] = OwnedTagSearchFacet() fields = {} for c in criteria: if (c in exclude) or (':' not in c): continue (f, o) = c.split(':', 1) if f.startswith('-'): f = 'NOT ' + f[1:] fname = f.rsplit(' ', 1)[-1] # create exact match criteria on the fly if needed if fname.endswith('_s') and not fname in search_facets: search_facets[fname] = ExactValueSearchFacet(fname) if fname in search_facets: o = search_facets[fname].process_criteria(o, user) fields.setdefault(f, []).append('(' + o.replace('|', ' OR ') + ')') fields = map( lambda (name, crit): '%s:(%s)' % ( name, (name.startswith('NOT ') and ' OR ' or ' AND ').join(crit)), fields.iteritems()) def build_keywords(q, k): k = k.lower() if k == 'and' or k == 'or': return q + ' ' + k.upper() elif q.endswith(' AND') or q.endswith(' OR'): return q + ' ' + k else: return q + ' AND ' + k if keywords: keywords = reduce(build_keywords, keywords.split()) query = '' if fields: query = ' AND '.join(fields) if keywords: if query: query = '%s AND (%s)' % (query, keywords) else: query = '(%s)' % keywords if not query: query = '*:*' if collection: query = 'collections:%s AND %s' % (collection.id, query) if hasattr(selected, '__len__'): query = 'id:(%s) AND %s' % ( ' '.join(map(str, selected or [-1])), query) if not user.is_superuser: collections = ' '.join( map(str, filter_by_access(user, Collection).values_list( 'id', flat=True))) c = [] if collections: # access through readable collection when no record ACL set c.append('collections:(%s) AND acl_read:default' % collections) if user.id: # access through ownership c.append('owner:%s' % user.id) # access through record ACL groups = ' '.join( 'g%d' % id for id in user.groups.values_list('id', flat=True) ) if groups: groups = '((%s) AND NOT (%s)) OR ' % (groups, groups.upper()) c.append('acl_read:((%su%d) AND NOT U%d)' % ( groups, user.id, user.id)) else: # access through record ACL c.append('acl_read:anon') if c: query = '((%s)) AND %s' % (') OR ('.join(c), query) else: query = 'id:"-1"' mode, ids = get_collection_visibility_preferences(user) if ids: query += ' AND %sallcollections:(%s)' % ( '-' if mode == 'show' else '', ' '.join(map(str, ids)), ) return query
def search_facets(request, id=None, name=None, selected=False): collection = get_object_or_404( filter_by_access(request.user, Collection), id=id) if id else None # get parameters relevant for search criteria = request.GET.getlist('c') remove = request.GET.get('rem', None) if remove and remove in criteria: criteria.remove(remove) keywords = request.GET.get('kw', '') user = request.user if selected: selected = request.session.get('selected_records', ()) (hits, records, search_facets, orfacet, query, fields) = run_search( user, collection, criteria, keywords, selected=selected, remove=remove, produce_facets=True) if collection: url = reverse('solr-search-collection', kwargs={'id': collection.id, 'name': collection.name}) elif selected: url = reverse('solr-selected') else: url = reverse('solr-search') q = request.GET.copy() q = clean_record_selection_vars(q) q.pop('or', None) q.pop('rem', None) q.pop('action', None) q.pop('page', None) q.pop('op', None) q.setlist('c', criteria) qurl = q.urlencode() limit_url = "%s?%s%s" % (url, qurl, qurl and '&' or '') # sort facets by specified order, if any, then by label ordered_facets = ( FieldSetField.objects .filter(fieldset__name='facet-fields') .values_list('field__name', flat=True) .order_by('order', 'label', 'field__label', 'field__name') ) facets = [] for of in ordered_facets: try: facets.append(search_facets.pop(of + '_t')) except KeyError: pass facets.extend(sorted(list(search_facets.values()), key=lambda f: f.label)) # clean facet items for f in facets: f.clean_result(hits) # remove facets with only no filter options facets = [f for f in facets if len(f.facets) > 0] # remove facets that should be hidden hide_facets = getattr(settings, 'HIDE_FACETS', None) if hide_facets: facets = [f for f in facets if f.label not in hide_facets] html = render_to_string( 'results_facets.html', { 'limit_url': limit_url, 'facets': facets }, request=request) mode, ids = get_collection_visibility_preferences(user) hash = calculate_hash(getattr(user, 'id', 0), collection, criteria, keywords, selected, remove, mode, str(ids), ) cache.set('search_facets_html_%s' % hash, html, 300) return dict(html=html)
def search(request, id=None, name=None, selected=False, json=False): #print "solr search\n\n\n!!!!!!\n\n\n" collection = id and get_object_or_404(filter_by_access(request.user, Collection), id=id) or None """ #print "------------------------------------------------------view.search----------------------------" #print "request" #print request """ if request.method == "POST": update_record_selection(request) # redirect to get request with updated parameters q = request.GET.copy() q.update(request.POST) q = clean_record_selection_vars(q) for i, v in q.items(): if i != 'c': q[i] = v # replace multiple values with last one except for criteria ('c') q.pop('v.x', None) q.pop('v.y', None) q.pop('x', None) q.pop('y', None) return HttpResponseRedirect(request.path + '?' + q.urlencode()) # get parameters relevant for search criteria = request.GET.getlist('c') remove = request.GET.get('rem', None) if remove and remove in criteria: criteria.remove(remove) keywords = request.GET.get('kw', '') #print [ item.encode('ascii') for item in ast.literal_eval(keywords) ] # get parameters relevant for view viewmode = request.GET.get('v', 'thumb') if viewmode == 'list': pagesize = max(min(safe_int(request.GET.get('ps', '50'), 50), 100), 5) else: pagesize = max(min(safe_int(request.GET.get('ps', '30'), 30), 50), 5) page = safe_int(request.GET.get('page', '1'), 1) sort = request.GET.get('s', 'title_sort').lower() if not sort.endswith(" asc") and not sort.endswith(" desc"): sort += " asc" orquery = request.GET.get('or', None) user = request.user if request.GET.has_key('action'): page = safe_int(request.GET.get('op', '1'), 1) if selected: selected = request.session.get('selected_records', ()) (hits, records, search_facets, orfacet, query, fields) = run_search(user, collection, criteria, keywords, sort, page, pagesize, orquery, selected, remove, produce_facets=False) #print "solr.views 414 criteria %s keywords %s hits %s records %s" %(criteria, keywords, hits, records) if json: return (hits, records, viewmode) if collection: url = reverse('solr-search-collection', kwargs={'id': collection.id, 'name': collection.name}) furl = reverse('solr-search-collection-facets', kwargs={'id': collection.id, 'name': collection.name}) elif hasattr(selected, '__len__'): url = reverse('solr-selected') furl = reverse('solr-selected-facets') else: url = reverse('solr-search') furl = reverse('solr-search-facets') q = request.GET.copy() q = clean_record_selection_vars(q) q.pop('or', None) q.pop('rem', None) q.pop('action', None) q.pop('page', None) q.pop('op', None) q.pop('v.x', None) q.pop('v.y', None) q.pop('x', None) q.pop('y', None) q['s'] = q.get('s', sort) q['v'] = q.get('v', 'thumb') q.setlist('c', criteria) hiddenfields = [('op', page)] #for f in q: # if f != 'kw': # for l in q.getlist(f): # hiddenfields.append((f, l)) qurl = q.urlencode() q.setlist('c', filter(lambda c: c != orquery, criteria)) qurl_orquery = q.urlencode() limit_url = "%s?%s%s" % (url, qurl, qurl and '&' or '') limit_url_orquery = "%s?%s%s" % (url, qurl_orquery, qurl_orquery and '&' or '') facets_url = "%s?%s%s" % (furl, qurl, qurl and '&' or '') form_url = "%s?%s" % (url, q.urlencode()) prev_page_url = None next_page_url = None if page > 1: q['page'] = page - 1 prev_page_url = "%s?%s" % (url, q.urlencode()) if page < (hits - 1) / pagesize + 1: q['page'] = page + 1 next_page_url = "%s?%s" % (url, q.urlencode()) def readable_criteria(c): (f, o) = c.split(':', 1) negated = f.startswith('-') f = f[1 if negated else 0:] if search_facets.has_key(f): return dict(facet=c, term=search_facets[f].display_value(o), label=search_facets[f].label, negated=negated, or_available=not negated and search_facets[f].or_available()) else: return dict(facet=c, term=o, label='Unknown criteria', negated=negated, or_available=False) def reduce_federated_search_query(q, c): (f, o) = c.split(':', 1) if f.startswith('-') or not search_facets.has_key(f): # can't negate in federated search return q v = search_facets[f].federated_search_query(o) return v if not q else '%s %s' % (q, v) mode, ids = get_collection_visibility_preferences(user) hash = calculate_hash(getattr(user, 'id', 0), collection, criteria, keywords, selected, remove, mode, str(ids), ) #print hash facets = cache.get('search_facets_html_%s' % hash) sort = sort.startswith('random') and 'random' or sort.split()[0] sort = sort.endswith('_sort') and sort[:-5] or sort federated_search_query = reduce(reduce_federated_search_query, criteria, keywords) federated_search = sidebar_api_raw( request, federated_search_query, cached_only=True) if federated_search_query else None query_string = "" """if len(criteria)>0 : query_string += "search=advance " else : query_string += "search=simple " """ query_list = {} crit_pattern = re.compile("(?P<type>.*?(\_t)?):\"?(?P<value>.*?)\"?$") #crit_pattern = re.compile("(?P<type>.*?\_t):(?P<value>.*?)") for c in criteria : m = crit_pattern.match(c) add_to_dict(query_list,str(m.group('type')),str(m.group('value')).replace(" ",'+').replace("\"",'')) """ if query_list.has_key(str(m.group('type'))) : query_list.update({str(m.group('type')):query_list[str(m.group('type'))]+"%26\""+str(m.group('value')).replace(" ",'+').replace("\"",'')+"\""}) else: query_list.update({str(m.group('type')):"\""+str(m.group('value')).replace(" ",'+').replace("\"",'')+"\""}) """ """ #print "keywords is" #print keywords """ kws_list = re.split('(-?\".*?\")',keywords) while "" in kws_list: kws_list.remove("") while " " in kws_list: kws_list.remove(" ") kws="" kws_not = "" query_terms="" for kw in kws_list: kw = kw.replace("\"","") if kw is '': # empty search pass # nothing to do elif "=" in kw: if not query_terms == "": query_terms += ", " query_terms += kw elif kw[0]=='-': kw = kw.replace('\"','') if kws_not is "": kws_not += kw.replace('-','') else : kws_not += "+"+kw.replace('-','') else: kw = kw.replace('\"','') if kws is "": kws += kw else: kws += "+"+kw #print "kws = " #print kws #print "kws_not =" #print kws_not if kws.startswith("keywords="): kws = kws.replace("keywords=","") while kws.endswith('\\'): kws = kws[:-1] query_string += "keywords=" + kws query_string = query_string.replace('\"','') # query_string += ";"+str(query_list) for key in query_list.keys() : value = query_list[key] for v in value: while v.endswith('\\'): v = v[:-1] q = key+"="+v query_string = query_string+','+q.replace("\"", "").replace('_t','') if not query_terms is '': query_string += ', '+query_terms if not kws_not is '': query_string += ', '+"-="+kws_not #print "\n\n\n\n\n--------------------------Query String is:----s----------------------------" #print query_string+"\n\n\n\n\n" return render_to_response('results.html', {'criteria': map(readable_criteria, criteria), 'query': query_string, 'keywords': kws,#keywords, 'hiddenfields': hiddenfields, 'records': records, 'hits': hits, 'page': page, 'pages': (hits - 1) / pagesize + 1, 'pagesize': pagesize, 'prev_page': prev_page_url, 'next_page': next_page_url, 'reset_url': url, 'form_url': form_url, 'limit_url': limit_url, 'limit_url_orquery': limit_url_orquery, 'facets': facets, 'facets_url': facets_url, 'orfacet': orfacet, 'orquery': orquery, 'sort': sort, 'random': random.random(), 'viewmode': viewmode, 'federated_search': federated_search, 'federated_search_query':query_string, 'pagination_helper': [None] * hits, 'has_record_created_criteria': any(f.startswith('created:') for f in criteria), 'has_last_modified_criteria': any(f.startswith('modified:') for f in criteria), }, context_instance=RequestContext(request))
def search(request, id=None, name=None, selected=False, json=False): #print "solr search\n\n\n!!!!!!\n\n\n" collection = id and get_object_or_404( filter_by_access(request.user, Collection), id=id) or None """ #print "------------------------------------------------------view.search----------------------------" #print "request" #print request """ if request.method == "POST": update_record_selection(request) # redirect to get request with updated parameters q = request.GET.copy() q.update(request.POST) q = clean_record_selection_vars(q) for i, v in q.items(): if i != 'c': q[i] = v # replace multiple values with last one except for criteria ('c') q.pop('v.x', None) q.pop('v.y', None) q.pop('x', None) q.pop('y', None) return HttpResponseRedirect(request.path + '?' + q.urlencode()) # get parameters relevant for search criteria = request.GET.getlist('c') remove = request.GET.get('rem', None) if remove and remove in criteria: criteria.remove(remove) keywords = request.GET.get('kw', '') #print [ item.encode('ascii') for item in ast.literal_eval(keywords) ] # get parameters relevant for view viewmode = request.GET.get('v', 'thumb') if viewmode == 'list': pagesize = max(min(safe_int(request.GET.get('ps', '50'), 50), 100), 5) else: pagesize = max(min(safe_int(request.GET.get('ps', '30'), 30), 50), 5) page = safe_int(request.GET.get('page', '1'), 1) sort = request.GET.get('s', 'title_sort').lower() if not sort.endswith(" asc") and not sort.endswith(" desc"): sort += " asc" orquery = request.GET.get('or', None) user = request.user if request.GET.has_key('action'): page = safe_int(request.GET.get('op', '1'), 1) if selected: selected = request.session.get('selected_records', ()) (hits, records, search_facets, orfacet, query, fields) = run_search(user, collection, criteria, keywords, sort, page, pagesize, orquery, selected, remove, produce_facets=False) #print "solr.views 414 criteria %s keywords %s hits %s records %s" %(criteria, keywords, hits, records) if json: return (hits, records, viewmode) if collection: url = reverse('solr-search-collection', kwargs={ 'id': collection.id, 'name': collection.name }) furl = reverse('solr-search-collection-facets', kwargs={ 'id': collection.id, 'name': collection.name }) elif hasattr(selected, '__len__'): url = reverse('solr-selected') furl = reverse('solr-selected-facets') else: url = reverse('solr-search') furl = reverse('solr-search-facets') q = request.GET.copy() q = clean_record_selection_vars(q) q.pop('or', None) q.pop('rem', None) q.pop('action', None) q.pop('page', None) q.pop('op', None) q.pop('v.x', None) q.pop('v.y', None) q.pop('x', None) q.pop('y', None) q['s'] = q.get('s', sort) q['v'] = q.get('v', 'thumb') q.setlist('c', criteria) hiddenfields = [('op', page)] #for f in q: # if f != 'kw': # for l in q.getlist(f): # hiddenfields.append((f, l)) qurl = q.urlencode() q.setlist('c', filter(lambda c: c != orquery, criteria)) qurl_orquery = q.urlencode() limit_url = "%s?%s%s" % (url, qurl, qurl and '&' or '') limit_url_orquery = "%s?%s%s" % (url, qurl_orquery, qurl_orquery and '&' or '') facets_url = "%s?%s%s" % (furl, qurl, qurl and '&' or '') form_url = "%s?%s" % (url, q.urlencode()) prev_page_url = None next_page_url = None if page > 1: q['page'] = page - 1 prev_page_url = "%s?%s" % (url, q.urlencode()) if page < (hits - 1) / pagesize + 1: q['page'] = page + 1 next_page_url = "%s?%s" % (url, q.urlencode()) def readable_criteria(c): (f, o) = c.split(':', 1) negated = f.startswith('-') f = f[1 if negated else 0:] if search_facets.has_key(f): return dict(facet=c, term=search_facets[f].display_value(o), label=search_facets[f].label, negated=negated, or_available=not negated and search_facets[f].or_available()) else: return dict(facet=c, term=o, label='Unknown criteria', negated=negated, or_available=False) def reduce_federated_search_query(q, c): (f, o) = c.split(':', 1) if f.startswith('-') or not search_facets.has_key(f): # can't negate in federated search return q v = search_facets[f].federated_search_query(o) return v if not q else '%s %s' % (q, v) mode, ids = get_collection_visibility_preferences(user) hash = calculate_hash( getattr(user, 'id', 0), collection, criteria, keywords, selected, remove, mode, str(ids), ) #print hash facets = cache.get('search_facets_html_%s' % hash) sort = sort.startswith('random') and 'random' or sort.split()[0] sort = sort.endswith('_sort') and sort[:-5] or sort federated_search_query = reduce(reduce_federated_search_query, criteria, keywords) federated_search = sidebar_api_raw( request, federated_search_query, cached_only=True) if federated_search_query else None query_string = "" """if len(criteria)>0 : query_string += "search=advance " else : query_string += "search=simple " """ query_list = {} crit_pattern = re.compile("(?P<type>.*?(\_t)?):\"?(?P<value>.*?)\"?$") #crit_pattern = re.compile("(?P<type>.*?\_t):(?P<value>.*?)") for c in criteria: m = crit_pattern.match(c) add_to_dict(query_list, str(m.group('type')), str(m.group('value')).replace(" ", '+').replace("\"", '')) """ if query_list.has_key(str(m.group('type'))) : query_list.update({str(m.group('type')):query_list[str(m.group('type'))]+"%26\""+str(m.group('value')).replace(" ",'+').replace("\"",'')+"\""}) else: query_list.update({str(m.group('type')):"\""+str(m.group('value')).replace(" ",'+').replace("\"",'')+"\""}) """ """ #print "keywords is" #print keywords """ kws_list = re.split('(-?\".*?\")', keywords) while "" in kws_list: kws_list.remove("") while " " in kws_list: kws_list.remove(" ") kws = "" kws_not = "" query_terms = "" for kw in kws_list: kw = kw.replace("\"", "") if kw is '': # empty search pass # nothing to do elif "=" in kw: if not query_terms == "": query_terms += ", " query_terms += kw elif kw[0] == '-': kw = kw.replace('\"', '') if kws_not is "": kws_not += kw.replace('-', '') else: kws_not += "+" + kw.replace('-', '') else: kw = kw.replace('\"', '') if kws is "": kws += kw else: kws += "+" + kw #print "kws = " #print kws #print "kws_not =" #print kws_not if kws.startswith("keywords="): kws = kws.replace("keywords=", "") while kws.endswith('\\'): kws = kws[:-1] query_string += "keywords=" + kws query_string = query_string.replace('\"', '') # query_string += ";"+str(query_list) for key in query_list.keys(): value = query_list[key] for v in value: while v.endswith('\\'): v = v[:-1] q = key + "=" + v query_string = query_string + ',' + q.replace("\"", "").replace( '_t', '') if not query_terms is '': query_string += ', ' + query_terms if not kws_not is '': query_string += ', ' + "-=" + kws_not #print "\n\n\n\n\n--------------------------Query String is:----s----------------------------" #print query_string+"\n\n\n\n\n" return render_to_response( 'results.html', { 'criteria': map(readable_criteria, criteria), 'query': query_string, 'keywords': kws, #keywords, 'hiddenfields': hiddenfields, 'records': records, 'hits': hits, 'page': page, 'pages': (hits - 1) / pagesize + 1, 'pagesize': pagesize, 'prev_page': prev_page_url, 'next_page': next_page_url, 'reset_url': url, 'form_url': form_url, 'limit_url': limit_url, 'limit_url_orquery': limit_url_orquery, 'facets': facets, 'facets_url': facets_url, 'orfacet': orfacet, 'orquery': orquery, 'sort': sort, 'random': random.random(), 'viewmode': viewmode, 'federated_search': federated_search, 'federated_search_query': query_string, 'pagination_helper': [None] * hits, 'has_record_created_criteria': any(f.startswith('created:') for f in criteria), 'has_last_modified_criteria': any(f.startswith('modified:') for f in criteria), }, context_instance=RequestContext(request))
def collection_visibility_preferences(context): user = context.get("user") mode, ids = get_collection_visibility_preferences(user) cform = get_collection_visibility_prefs_form(user) form = cform(initial=dict(show_or_hide=mode, collections=ids)) return {"form": form, "request": context["request"]}
def search_facets(request, id=None, name=None, selected=False): collection = get_object_or_404( filter_by_access(request.user, Collection), id=id) if id else None # get parameters relevant for search criteria = request.GET.getlist('c') remove = request.GET.get('rem', None) if remove and remove in criteria: criteria.remove(remove) keywords = request.GET.get('kw', '') user = request.user if selected: selected = request.session.get('selected_records', ()) (hits, records, search_facets, orfacet, query, fields) = run_search( user, collection, criteria, keywords, selected=selected, remove=remove, produce_facets=True) if collection: url = reverse('solr-search-collection', kwargs={'id': collection.id, 'name': collection.name}) elif selected: url = reverse('solr-selected') else: url = reverse('solr-search') q = request.GET.copy() q = clean_record_selection_vars(q) q.pop('or', None) q.pop('rem', None) q.pop('action', None) q.pop('page', None) q.pop('op', None) q.setlist('c', criteria) qurl = q.urlencode() limit_url = "%s?%s%s" % (url, qurl, qurl and '&' or '') # sort facets by specified order, if any, then by label ordered_facets = (FieldSetField.objects .filter(fieldset__name='facet-fields') .values_list('field__name', flat=True) .order_by('order', 'label', 'field__label', 'field__name') ) facets = [] for of in ordered_facets: try: facets.append(search_facets.pop(of + '_t')) except KeyError: pass facets.extend(sorted(search_facets.values(), key=lambda f: f.label)) # clean facet items for f in facets: f.clean_result(hits) # remove facets with only no filter options facets = filter(lambda f: len(f.facets) > 0, facets) # remove facets that should be hidden hide_facets = getattr(settings, 'HIDE_FACETS', None) if hide_facets: facets = filter(lambda f: f.label not in hide_facets, facets) html = render_to_string( 'results_facets.html', { 'limit_url': limit_url, 'facets': facets }, context_instance=RequestContext(request)) mode, ids = get_collection_visibility_preferences(user) hash = calculate_hash(getattr(user, 'id', 0), collection, criteria, keywords, selected, remove, mode, str(ids), ) cache.set('search_facets_html_%s' % hash, html, 300) return dict(html=html)