Пример #1
0
def search_ajax(request, search_data_domain, data_url):
    show_analytics = False
    chart = None
    paginator = []
    paginate = False

    # Search Request
    qp = QueryParser()
    q = request.GET.get('q')
    
    params = qp.convert_to_query(request.GET.copy(), q)
    search_form = get_form_for_domain(search_data_domain)(params, user=request.user)

    # Return the Search results with pagination
    if search_form.is_valid():
        sqs = search_form.get_query()

        # Create a control based on the params
        control = create_control_query(params)
        control_form = get_form_for_domain(search_data_domain)(control, user=request.user)

        if control_form.is_valid():
            control_sqs = control_form.get_query()

            # Paginate Results
            paginate = is_paginate(search_form)
            rpp = calculate_results_per_page(search_form)

            paginator = get_paginated_results(sqs, request, rpp)

            if show_analytics:
                chart = get_analytics_for_domain(sqs, control_sqs, search_data_domain)

    else:
        paginator =  get_fake_paginator(0, 0, 1)
        paginate = False
        show_analytics = False

    response = {
        'results_paginator': paginator,
        'max_score': sqs.max_score(),
        'paginate': paginate,
        'chart': chart,
        'show_analytics': show_analytics
    }

    # Redirect to the right page based on the data_url
    return response, data_url
Пример #2
0
def search(request, search_data_domain, data_url, ajax_url):

    # For filtering, we will need to hide the following fields from the form
    fields_to_hide = ('q', 'state', 'state_abbrv', 'naics_code', 'fsc_code', 'nigp_code', 'cfda_code', 'start_amount', 'end_amount', 'city', 'county', 'industries', 'start_date', 'end_date', 'industry', 'place_of_performance', 'sector', 'last_name', 'first_name', 'none_of_these_words', 'exact_phrase', 'element_status', 'procurement_status', 'grant_status', 'no_amount', 'any_of_these_words', 'county_names', 'wo_elements', 'current_status_only', 'org_id', 'person_id')
    domain_facet = {}
    charts = {}
    entity_id = None
    quarters = []
    if len(request.GET) > 0:
        # Retrieve the Form based on the domain
        # Quick Search
        qp = QueryParser()
        q = request.GET.get('q')
        params = qp.convert_to_query(request.GET.copy(), q)
        
        #TODO: Hack. Fix Later
        if 'no_amount' not in params:
            params['no_amount'] = True
        search_form = get_form_for_domain(search_data_domain)(params, fields_to_hide=fields_to_hide, user=request.user, page_type='results')
        _log.debug('Search Form Data = %s' % search_form.data)

        # For Overview and Advance Search, we will need to run the query and return the results and facets
        if  search_form.is_valid():
            sqs = search_form.get_query()
            quarters = quarters_range(search_form._default_query_fields.get('maxDate'),
                                      search_form._default_query_fields.get('minDate'))

            # Create a control based on the params
            control = create_control_query(params)
            control_form = get_form_for_domain(search_data_domain)(control, fields_to_hide=fields_to_hide, user=request.user, page_type='results')

            if control_form.is_valid():
                control_sqs = control_form.get_query()
                buyer_seller_results = sqs.facet('buyer_relevance_profile').facet('seller_relevance_profile')
                if search_data_domain == 3:
                    buyer_table, buyer_table_avg = get_fomrated_table(buyer_seller_results.facet_counts()['fields']['buyer_relevance_profile'])
                    seller_table, seller_table_avg = get_fomrated_table(buyer_seller_results.facet_counts()['fields']['seller_relevance_profile'])
                else:
                    buyer_table = []
                    buyer_table_avg = []
                    seller_table = []
                    seller_table_avg = []

                # log query
                tasks.log_query.delay(q, request.user, search_data_domain)

                # Results Per Page
                paginate = is_paginate(search_form)
                rpp = calculate_results_per_page(search_form)

                # Facets
                sqs = sqs.facet('sector_id').facet('loc_state')
                domain = request.GET.get('domain', '1')
                qp = QueryParser()
                q = request.GET.get('q')
                params = qp.convert_to_query(request.GET.copy(), q)

                new_search_form = get_form_for_domain_facet()(params, user=request.user)
                domain_facet = get_domain_facet(new_search_form, request.user, search_data_domain)
                paginator = get_paginated_results(sqs, request, rpp)
                charts = get_query_analytics(sqs, control_sqs, search_data_domain)

        else:
            # For errors, no results will be displayed with empty charts
            _log.error('Search: Form is not valid. Error = %s' % search_form.errors)

            paginator = get_fake_paginator(0, 0, 1)
            paginate = False
            charts = {
                'sector_chart': get_sector_analytics([]),
                'sector_chart_bar': get_sector_analytics_bar([]),
                'state_chart': get_state_bar_graph([])
            }

        try:
            max_score = sqs.max_score()
        except:
            max_score = 1

        response = {
                    'search_form': search_form,
                    'max_score': max_score,
                    'results_paginator': paginator,
                    'paginate': paginate,
                    'search_data_domain': search_data_domain,
                    'ajax_url': ajax_url,
                    'quarters': quarters,
                    'domain_facet': domain_facet,
                    'domain': "%s" % search_data_domain,
            }

        # Append the charts to the response
        response.update(charts)

        return response, data_url


    else:
        # Redirect back to the Home Page
        return redirect(default_view)