Exemplo n.º 1
0
def apply_facets_and_filters(request, user_solr=None, facet_solr=None):
    """
    Applies facets to solr based on filters currently applied and creates
    a dictionary of removable terms and the resulting url with the term removed.

    inputs:
    :user_solr: A Solr instance used for retrieving SavedSearch documents
        from solr.
    :facet_solr: A Solr instance used for retrieving facets based on
        ProfileUnits data.
    :loc_solr: A solr instance used for retrieving facets based on
        location slabs.

    outputs:
    user_solr, facet_solr, and loc_solr appropriately filtered as well as a
    dictionary of
    {'the applied filter': 'resulting url if the filter were removed'}
    """
    url = request.build_absolute_uri()
    date_start = request.REQUEST.get('date_start', None)
    date_end = request.REQUEST.get('date_end', None)
    if date_start:
        url = update_url_param(url, 'date_start',
                               request.REQUEST.get('date_start'))
    if date_end:
        url = update_url_param(url, 'date_end',
                               request.REQUEST.get('date_end'))

    filters = {}
    user_solr = Solr() if not user_solr else user_solr
    facet_solr = Solr() if not facet_solr else facet_solr

    # The location parameter should be "Country-Region-City". Faceting
    # is determined on what is already in the location parameter.
    # No location facets on country, country parameter facets on state, and
    # country-state facets on city.
    if not 'location' in request.GET:
        facet_solr = facet_solr.add_facet_field('Address_country_code')
        facet_solr = facet_solr.add_facet_field('Address_region')
    else:
        fieldname = 'Address_full_location'

        term = urllib.unquote(request.GET.get('location'))
        search_term = term.replace("-", "##").replace(" ", "\ ")
        if len(term.split("-")) == 3:
            q = '%s:%s' % (fieldname, search_term)
        else:
            q = '%s:%s##*' % (fieldname, search_term)
        user_solr = user_solr.add_query(q)
        facet_solr = facet_solr.add_filter_query(q)

        term_list = term.split("-")
        term_len = len(term_list)

        prefix = '%s##' % term.replace('-', '##')
        if term_len == 3:
            # Country, Region, City included. No reason to facet on location.
            city = term_list[2] if term_list[2] else 'None'
            region = term_list[1] if term_list[1] else 'None'
            country = term_list[0]
            remove_term = "%s, %s, %s" % (city, region, country)
            new_val = "%s-%s" % (term_list[0], term_list[1])
            filters[remove_term] = update_url_param(url, 'location', new_val)
        elif term_len == 2:
            # Country, Region included.
            region = term_list[1] if term_list[1] else 'None'
            country = term_list[0]
            remove_term = "%s, %s" % (region, country)
            filters[remove_term] = update_url_param(url, 'location', term_list[0])
            facet_solr = facet_solr.add_facet_field(fieldname)
            facet_solr = facet_solr.add_facet_prefix(prefix, fieldname=fieldname)
        elif term_len == 1:
            # Country included.
            country = country_codes.get(term_list[0], term_list[0])
            filters[country] = remove_param_from_url(url, 'location')
            facet_solr = facet_solr.add_facet_field('Address_region')
            facet_solr = facet_solr.add_facet_prefix(prefix, fieldname=fieldname)

    if not 'education' in request.GET:
        facet_solr = facet_solr.add_facet_field('Education_education_level_code')
    else:
        term = urllib.unquote(request.GET.get('education'))
        term_name = edu_codes.get(int(term))
        filters[term_name] = remove_param_from_url(url, 'education')

        q = 'Education_education_level_code:%s' % term
        user_solr = user_solr.add_query(q)
        facet_solr = facet_solr.add_filter_query(q)

    if not 'license' in request.GET:
        facet_solr = facet_solr.add_facet_field('License_license_name')
    else:
        term = urllib.unquote(request.GET.get('license'))
        filters[term] = remove_param_from_url(url, 'license')

        q = 'License_license_name:"%s"' % term
        user_solr = user_solr.add_query(q)
        facet_solr = facet_solr.add_filter_query(q)

    return user_solr, facet_solr, filters