Exemplo n.º 1
0
def get_graph_data(search_query_set, x_axis, y_axes):
    """
    Obtain the graph data for the given axes extracted from
    the SearchQuerySet results.
    :param search_query_set: A SearchQuerySet object.
    :param x_axis: The X-Axis.
    :param y_axes: A list of axes that will be plotted along the Y-axis.
    :return: A dictionary containing an entry for each element in
    y_axes (with key equal to the axis description).
    The dictionary values are 2D point arrays, representing (x, y) values.
    """
    if len(y_axes) == 0:
        return {}
    # Fetch only the required data from the search query set.
    fields = [y_axis.var_name for y_axis in y_axes]
    fields.append(x_axis.var_name)
    data = get_values_from_haystack_results(search_query_set, fields)
    sorting = lambda d: x_axis.get_group_key(d)
    data = sorted(data, key=sorting)
    result = {y_axis.description: [] for y_axis in y_axes}
    for x_key, e in groupby(data, key=sorting):
        if x_key is None:
            continue
        lst = list(e)
        x_val = x_axis.get_value(lst[0])
        for y_axis in y_axes:
            y_val = y_axis.get_stat(lst)
            # We may omit groups which cannot compute their statistic.
            if y_val is None:
                continue
            result[y_axis.description].append((x_val, y_val))
    return result
Exemplo n.º 2
0
def get_graph_data(search_query_set, x_axis, y_axes):
    """
    Obtain the graph data for the given axes extracted from
    the SearchQuerySet results.
    :param search_query_set: A SearchQuerySet object.
    :param x_axis: The X-Axis.
    :param y_axes: A list of axes that will be plotted along the Y-axis.
    :return: A dictionary containing an entry for each element in
    y_axes (with key equal to the axis description).
    The dictionary values are 2D point arrays, representing (x, y) values.
    """
    if len(y_axes) == 0:
        return {}
    # Fetch only the required data from the search query set.
    fields = [y_axis.var_name for y_axis in y_axes]
    fields.append(x_axis.var_name)
    data = get_values_from_haystack_results(search_query_set, fields)
    sorting = lambda d: x_axis.get_group_key(d)
    data = sorted(data, key=sorting)
    result = {y_axis.description: [] for y_axis in y_axes}
    for x_key, e in groupby(data, key=sorting):
        if x_key is None:
            continue
        lst = list(e)
        x_val = x_axis.get_value(lst[0])
        for y_axis in y_axes:
            y_val = y_axis.get_stat(lst)
            # We may omit groups which cannot compute their statistic.
            if y_val is None:
                continue
            result[y_axis.description].append((x_val, y_val))
    return result
Exemplo n.º 3
0
def get_all_slaves(request):
    """
    Retrieve and return all slaves

    :param request: Request to serve
    :return: slaves to display
    """

    results_per_page_form = None
    results_per_page = 20
    current_page = 1
    sort_string = ""

    # Try to retrieve session
    try:
        results = request.session["names_results"]
    except KeyError:
        results = {}

    try:
        current_query = request.session["names_current_query"]
    except KeyError:
        current_query = {}

    try:
        query_dict = request.session["names_query_dict"];
    except KeyError:
        query_dict = {}

    try:
        sort_column = request.session["sort_column"]
    except KeyError:
        sort_column = "slave_id"
        request.session["sort_column"] = "slave_id"

    try:
        sort_mode = request.session["sort_mode"]
    except KeyError:
        sort_mode = "1"
        request.session["sort_mode"] = "1"

    try:
        opened_tabs = request.session["names_opened_tabs"]
    except KeyError:
        opened_tabs = {'section_1': True,
                       'section_2': False,
                       'section_3': False}
        request.session["names_opened_tabs"] = opened_tabs

    # If there is no requested page number, serve 1
    desired_page = request.POST.get('desired_page')
    if desired_page:
        current_page = desired_page

    # Collect Origins (it's now done by sql query, in the Haystack there is no easy way to
    # get this list (facet is similar, but not what we want to have)
    countries_from_names = AfricanName.objects.exclude(country__isnull=True).values('country__name')
    countries = Country.objects.filter(name__in=countries_from_names).order_by('name')

    # Collect places
    places_from_embarkation = AfricanName.objects.exclude(embarkation_port__isnull=True).\
        values('embarkation_port__place')
    places_from_disembarkation = AfricanName.objects.exclude(disembarkation_port__isnull=True).\
        values('disembarkation_port__place')
    places = Place.objects.all()

    used = []
    places_separated_embarkation = []
    places_separated_disembarkation = []

    # For embarkation and disembarkation collect ids of places
    for i in places_from_embarkation:
        if i['embarkation_port__place'] not in used:
            places_separated_embarkation.append(places.filter(place=i['embarkation_port__place']).
                                                 values('id')[0]['id'])
            used.append(i['embarkation_port__place'])
    used = []
    for i in places_from_disembarkation:
        if i['disembarkation_port__place'] not in used:
            places_separated_disembarkation.append(places.filter(place=i['disembarkation_port__place']).
                                                    values('id')[0]['id'])
            used.append(i['disembarkation_port__place'])

    # Retrieve structured places
    embarkation_list = structure_places(places_separated_embarkation)
    disembarkation_list = structure_places(places_separated_disembarkation)

    if request.method == "GET":
        results_per_page_form = ResultsPerPageOptionForm()

        # If no results in session, retrieve
        if len(results) == 0:
            if sort_mode is "1":
                sort_string = sort_column
            if sort_mode is "2":
                sort_string = "-" + sort_column

            # If this is ngram, sort by string field
            if sort_column in names_sort_fields:
                sort_string += "_sort"

            results = SearchQuerySet().models(AfricanName).order_by(sort_string)

            request.session['names_results'] = results
            request.session['sort_column'] = sort_column
            request.session['sort_mode'] = sort_mode

    if request.method == "POST":
        results_per_page_form = ResultsPerPageOptionForm(request.POST)

        if request.POST.get("action") is not None and request.POST.get("action") == "New Query":
            # Clicked "New Query", reset all session variables
            results = SearchQuerySet().models(AfricanName).order_by("slave_id")
            query_dict = {}
            current_query = {}
            request.session['names_results'] = results
            request.session['names_query_dict'] = {}
            request.session['names_current_query'] = {}
            request.session['sort_column'] = "slave_id"
            sort_column = "slave_id"
            sort_mode = "1"
            request.session['sort_mode'] = "1"
            results_per_page = 20
            request.session['slaves_per_page_choice'] = None
            request.session['slaves_per_page'] = results_per_page
            current_page = 1
            opened_tabs = {'section_1': True,
                           'section_2': False,
                           'section_3': False}
            request.session["names_opened_tabs"] = opened_tabs

        elif request.POST.get("sort_column") is not None:
            # One of the headers clicked, perform sort
            # If column has changed, reset the sort_mode
            if request.session["sort_column"] != request.POST.get("sort_column"):
                sort_column = request.POST.get("sort_column")
                sort_mode = "2"

            # If previously it was "2", set as "1" and vice versa
            if sort_mode == "1":
                sort_mode = "2"
                sort_string = "-" + sort_column
            else:
                sort_mode = "1"
                sort_string = sort_column

            # If this is ngram, sort by string field
            if sort_column in names_sort_fields:
                sort_string += "_sort"

            # Perform query and store results
            if len(query_dict) > 0:
                results = SearchQuerySet().filter(**query_dict).models(AfricanName).order_by(sort_string)
            else:
                results = SearchQuerySet().models(AfricanName).order_by(sort_string)

            request.session['names_results'] = results
            request.session["sort_column"] = sort_column
            request.session["sort_mode"] = sort_mode

        elif request.POST.get("action") == "Search":
            # Encode and store query dict/opened tabs/current query
            query_dict, opened_tabs, current_query = create_query_dict(request.POST, embarkation_list,
                                                                       disembarkation_list, countries)
            request.session['names_query_dict'] = query_dict
            request.session['names_opened_tabs'] = opened_tabs
            request.session['names_current_query'] = current_query

            # If any sorting option exist
            if sort_mode == "1":
                sort_string = sort_column
            if sort_mode == "2":
                sort_string = "-" + sort_column

            # If this is ngram, sort by string field
            if sort_column in names_sort_fields:
                sort_string += "_sort"

            # Filter only if query_dict is not empty
            if len(query_dict) > 0:
                results = SearchQuerySet().filter(**query_dict).models(AfricanName).order_by(sort_string)
            else:
                results = SearchQuerySet().models(AfricanName).order_by(sort_string)
            request.session['names_results'] = results
        elif request.POST.get("action") == "download_all":
            # Build download file.
            data = get_values_from_haystack_results(results, AFRICAN_NAME_SOLR_FIELDS)
            return download_slaves_helper(data)

        # Manage results per page
        if results_per_page_form.is_valid():
            results_per_page = results_per_page_form.cleaned_option()
            request.session['slaves_per_page_choice'] = results_per_page_form.cleaned_data['option']
            request.session['slaves_per_page'] = results_per_page
        elif 'results_per_page' in request.session and 'results_per_page_choice' in request.session:
            results_per_page = request.session['slaves_per_page']
            results_per_page_form.fields['option'].initial = request.session['slaves_per_page_choice']
            results_per_page_form = ResultsPerPageOptionForm({u'option': request.session['slaves_per_page_choice']})

    # Paginate results to pages
    paginator = Paginator(results, results_per_page)
    pagins = paginator.page(current_page)
    if request.method == "POST" and request.POST.get("action") == "download_current_view":
        data = [x.get_stored_fields() for x in pagins.object_list]
        return download_slaves_helper(data)

    # Get ranges and number of pages
    (paginator_range, pages_range) = prepare_paginator_variables(paginator, current_page, results_per_page)

    return render(request, 'resources/names-index.html',
                  {'results': pagins,
                   'paginator_range': paginator_range,
                   'pages_range': pages_range,
                   'options_results_per_page_form': results_per_page_form,
                   'sort_column': sort_column,
                   'sort_mode': sort_mode,
                   'origins': countries,
                   'embarkation_list': embarkation_list,
                   'disembarkation_list': disembarkation_list,
                   'query_dict': query_dict,
                   'opened_tabs': opened_tabs,
                   'current_query': current_query})