コード例 #1
0
ファイル: views.py プロジェクト: hanleybrand/mdid_dj16
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))
コード例 #2
0
ファイル: views.py プロジェクト: GetHubMax/rooibos
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),
    )
コード例 #3
0
ファイル: views.py プロジェクト: Blanko2/vic-mdid
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))
コード例 #4
0
ファイル: views.py プロジェクト: gitter-badger/rooibos
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))
コード例 #5
0
ファイル: views.py プロジェクト: hanleybrand/vic-mdid
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))