Пример #1
0
def queries_query_results(request, graph_slug, query_id):
    graph = get_object_or_404(Graph, slug=graph_slug)
    query = graph.queries.get(pk=query_id)

    # Breadcrumbs variables
    redirect_url = reverse("queries_list", args=[graph.slug])
    queries_link = (redirect_url, _("Queries"))
    queries_name = (reverse("queries_query_edit", args=[graph.slug, query.id]),
                    query.name)
    # We declare the form to get the options for the results table
    form = QueryOptionsForm()
    # We get the information to mantain the last query in the builder
    query_dict = request.POST.get("query", "").strip()
    query_aliases = request.POST.get("query_aliases", "").strip()
    query_fields = request.POST.get("query_fields", "").strip()
    # We get the order
    order_by_field = request.GET.get('order_by', 'default')
    order_dir = request.GET.get('dir', 'desc')
    page_dir = request.GET.get('page_dir', 'desc')
    # We get the modal variable
    as_modal = bool(request.POST.get("asModal", False))

    # Variable to control if the query has changed to show the save
    # buttons
    query_has_changed = False

    # The next comparisions are because we can execute the queries
    # directly from the list of queries. So, we could obtain empty
    # query dicts, empty query fields...

    # We check if the query saved and the query edited are different
    # In that case, we use the query edited
    if query_dict == '' or query_dict is None:
        query_dict = request.session.get('query')
        if query_dict is None:
            query_dict = query.query_dict
    else:
        query_dict = json.loads(query_dict)
    different_queries = query.query_dict != query_dict

    if query_aliases == '' or query_aliases is None:
        query_aliases = request.session.get('query_aliases')
        if query_aliases is None:
            query_aliases = query.query_aliases

    # We compare the session query fields and the new query fields.
    # This is because we can change the sorting params.
    if query_fields == '' or query_fields is None:
        query_fields = request.session.get('query_fields')
        if query_fields is None:
            query_fields = query.query_fields
    else:
        query_fields = json.loads(query_fields)
    different_sorting_params = query.query_fields != query_fields

    # We get the default sorting params values, in case that we execute
    # the query from the list of queries.
    # Older queries has not the field sortingParams, we need to control it.
    try:
        sorting_params = query.query_fields["sortingParams"]
        rows_number = sorting_params["rows_number"]
        # We need to transform it to integer to avoid exceptions
        rows_number = int(rows_number)
        show_mode = sorting_params["show_mode"]
        select_order_by = sorting_params["select_order_by"]
        dir_order_by = sorting_params["dir_order_by"]
    except KeyError:
        # We set the default values
        rows_number = DEFAULT_ROWS_NUMBER
        show_mode = DEFAULT_SHOW_MODE
        select_order_by = DEFAULT

    # We check if we have options in the form
    if request.POST:
        data = request.POST.copy()
        request.session['data'] = data
        # We add the new choice in the form
        new_choice_order_by = data.get('select_order_by')
        form = QueryOptionsForm(data=data,
                                new_choice=new_choice_order_by)
        if form.is_valid():
            rows_number = form.cleaned_data["rows_number"]
            show_mode = form.cleaned_data["show_mode"]
            select_order_by = form.cleaned_data["select_order_by"]
            dir_order_by = form.cleaned_data["dir_order_by"]
    elif not request.POST and (
            query.id == request.session.get('query_id', None)):
        data = request.session.get('data', None)
        # We add the new choice in the form
        new_choice_order_by = None
        if data:
            new_choice_order_by = data.get('select_order_by', None)
        form = QueryOptionsForm(data=data,
                                new_choice=new_choice_order_by)
        if form.is_valid():
            rows_number = form.cleaned_data["rows_number"]
            show_mode = form.cleaned_data["show_mode"]
            select_order_by = form.cleaned_data["select_order_by"]
            dir_order_by = form.cleaned_data["dir_order_by"]

    headers = True
    # This part of the code is similar to the same part in the
    # "queries_new_results". The main difference are the blocks
    # to check if we have modify the query or the sorting params
    # to change the correspondent values in the session.
    if order_by_field == DEFAULT and select_order_by == DEFAULT:
        if different_queries and (
                query.id == request.session.get('query_id', None)):
            # We check if the type of query_dict is appropiate
            if not isinstance(query_dict, dict):
                query_dict = json.loads(query_dict)
            query_results = graph.query(query_dict, headers=headers)
            request.session['query'] = json.dumps(query_dict)
            request.session['query_aliases'] = query_aliases
            request.session['query_fields'] = query_fields
            # Let's check if the sorting params are different
            if different_sorting_params:
                # We check if the type of query_field is appropiate
                if isinstance(query_fields, dict):
                    query_fields = json.dumps(query_fields)
                request.session['query_fields'] = query_fields
            query_has_changed = True
        else:
            query_results = query.execute(headers=headers)
            request.session['query'] = query.query_dict
            request.session['query_aliases'] = query.query_aliases
            request.session['query_fields'] = query.query_fields
    elif order_by_field == NO_ORDER:
        if different_queries and (
                query.id == request.session.get('query_id', None)):
            # We check if the type of query_dict is appropiate
            if not isinstance(query_dict, dict):
                query_dict = json.loads(query_dict)
            query_results = graph.query(query_dict, headers=headers)
            request.session['query'] = json.dumps(query_dict)
            request.session['query_aliases'] = query_aliases
            request.session['query_fields'] = query_fields
            # Let's check if the sorting params are different
            if different_sorting_params:
                # We check if the type of query_field is appropiate
                if isinstance(query_fields, dict):
                    query_fields = json.dumps(query_fields)
                request.session['query_fields'] = query_fields
            query_has_changed = True
        else:
            query_results = query.execute(headers=headers)
            request.session['query'] = query.query_dict
            request.session['query_aliases'] = query.query_aliases
            request.session['query_fields'] = query.query_fields
    else:
        if order_by_field == DEFAULT and select_order_by != DEFAULT:
            order_by_field = select_order_by
            order_dir = dir_order_by
        page_dir = order_dir
        # We check the properties of the results to see if we have
        # aggregates. This is for a special treatment in the order_by.
        aggregate = order_by_field.split('(')[0]
        # We check if the aggregate has distinct clause
        aggregate = aggregate.split(' ')[0]
        has_aggregate = aggregate in AGGREGATES.keys()
        if has_aggregate:
            alias = AGGREGATE
            # We check the '!' symbol for a correct order
            if order_by_field[-2] != '!':
                order_by_field = (
                    order_by_field[0:-1] + '!' + order_by_field[-1])
            value = order_by_field.replace('`', '')
            order_by = (alias, value, order_dir)
        else:
            # We split the header to get the alias and the property
            order_by_values = order_by_field.split('.')
            alias = order_by_values[0]
            prop = order_by_values[1]
            if prop[-1] == '!':
                prop = prop[:-1]
            order_by = (alias, prop, order_dir)
        if different_queries and (
                query.id == request.session.get('query_id', None)):
            # We check if the type of query_dict is appropiate
            if not isinstance(query_dict, dict):
                query_dict = json.loads(query_dict)
            query_results = graph.query(query_dict, order_by=order_by,
                                        headers=headers)
            request.session['query'] = json.dumps(query_dict)
            if query_aliases == "" or query_fields == "":
                query_aliases = request.session['query_aliases']
                query_fields = request.session['query_fields']
            else:
                request.session['query_aliases'] = query_aliases
                # We check if the type of query_field is appropiate
                if isinstance(query_fields, dict):
                    query_fields = json.dumps(query_fields)
                request.session['query_fields'] = query_fields
            query_has_changed = True
        else:
            query_results = query.execute(order_by=order_by, headers=headers)
            # Let's check if the sorting params are different
            if different_sorting_params:
                # We check if the type of query_field is appropiate
                if isinstance(query_fields, dict):
                    query_fields = json.dumps(query_fields)
                request.session['query_fields'] = query_fields
                query_has_changed = True
        if not query_results:
            messages.error(request,
                           _("Error: You are trying to sort a \
                              column with some none values"))
            query_results = query.execute(headers=headers)
        if order_dir == ASC:
            order_dir = DESC
        elif order_dir == DESC:
            order_dir = ASC

    # We assign the query id to the query_id of the session
    request.session['query_id'] = query.id
    # We save the values to export as CSV
    request.session["csv_results"] = query_results
    request.session["query_name"] = query.name

    # We store the results count in the session variable.
    query_results_length = len(query_results)
    request.session['results_count'] = query_results_length
    # We store the datetime of execution
    query.last_run = datetime.now()

    headers_final_results = dict()
    headers_query_results = []
    if query_results_length > 1:
        # We treat the headers
        if headers:
            # If the results have headers, we create a dictionary to have
            # the headers showed to the user and the headers using in the
            # backend (this is for the order by click in the header)
            # We check if query_aliases is empty
            if query_aliases is '':
                query_aliases = request.session['query_aliases']
            # We check if the type of query_aliases is appropiate to
            # manipulate it
            if not isinstance(query_aliases, dict):
                aliases = json.loads(query_aliases)
                aliases = aliases['types']
            else:
                aliases = query_aliases['types']
            headers_query_results = query_results[0]
            headers_final_results = _treat_headers(headers_query_results,
                                                   aliases)
        request.session['results_count'] = query_results_length - 1
        query_results = query_results[1:]
    else:
        query_results = []

    # We add the headers to the session to use them if we export the results
    request.session["headers_formatted"] = headers_final_results
    request.session["headers_raw"] = headers_query_results

    # We save the new changes of the query
    query.save()

    # We add pagination for the list of queries
    page = request.GET.get('page', 1)
    page_size = rows_number
    try:
        if show_mode == DEFAULT_SHOW_MODE:
            page_size = rows_number
            paginator = Paginator(query_results, page_size)
        else:
            page_size = settings.DATA_PAGE_SIZE
            query_results = query_results[:rows_number]
            paginator = Paginator(query_results, page_size)
        paginated_results = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        paginated_results = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        paginated_results = paginator.page(paginator.num_pages)

    if as_modal:
        base_template = 'empty.html'
        render = render_to_string
    else:
        base_template = 'base.html'
        render = render_to_response
    add_url = reverse("queries_query_results", args=[graph.slug, query.id])
    broader_context = {"graph": graph,
                       "query": query,
                       "query_has_changed": query_has_changed,
                       "queries_link": queries_link,
                       "queries_name": queries_name,
                       "headers_results": headers_query_results,
                       "headers": headers_final_results,
                       "results": paginated_results,
                       "order_by": order_by_field,
                       "dir": order_dir,
                       "page_dir": page_dir,
                       "csv_results": query_results,
                       "base_template": base_template,
                       "as_modal": as_modal,
                       "add_url": add_url,
                       "queries_list_url": redirect_url}
    response = render('queries/queries_new_results.html', broader_context,
                      context_instance=RequestContext(request))
    if as_modal:
        response = {'type': 'html',
                    'action': 'queries_results',
                    'html': response}
        return HttpResponse(json.dumps(response), status=200,
                            content_type='application/json')
    else:
        return response
Пример #2
0
def queries_query_results(request, graph_slug, query_id):
    graph = get_object_or_404(Graph, slug=graph_slug)
    query = graph.queries.get(pk=query_id)
    # Breadcrumbs variables
    queries_link = (reverse("queries_list", args=[graph.slug]),
                    _("Queries"))
    queries_name = (reverse("queries_query_edit", args=[graph.slug, query.id]),
                    query.name)
    # We declare the form to get the options for the results table
    form = QueryOptionsForm()
    # We get the information to mantain the last query in the builder
    query_dict = request.POST.get("query", "").strip()
    query_aliases = request.POST.get("query_aliases", "").strip()
    query_fields = request.POST.get("query_fields", "").strip()
    # We check if the query saved and the query edited are different
    # In that case, we use the query edited
    if query_dict == '' or query_dict is None:
        query_dict = request.session.get('query', query.query_dict)
    else:
        query_dict = json.loads(query_dict)
    different_queries = query.query_dict != query_dict
    # We add order for the list of queries
    order_by_field = request.GET.get('order_by', 'default')
    order_dir = request.GET.get('dir', 'desc')
    page_dir = request.GET.get('page_dir', 'desc')
    # query = "notas of autor with notas that start with lista"
    # see https://gist.github.com/versae/9241069
    # We check if we have options in the form
    rows_number = 100
    show_mode = "per_page"
    if request.POST:
        data = request.POST.copy()
        request.session['data'] = data
        form = QueryOptionsForm(data=data)
        if form.is_valid():
            rows_number = form.cleaned_data["rows_number"]
            show_mode = form.cleaned_data["show_mode"]
    elif not request.POST and (
            query.id == request.session.get('query_id', None)):
        data = request.session.get('data', None)
        form = QueryOptionsForm(data=data)
        if form.is_valid():
            rows_number = form.cleaned_data["rows_number"]
            show_mode = form.cleaned_data["show_mode"]
    headers = True
    # We need the order_dir for the icons in the frontend
    if order_by_field == 'default':
        if different_queries and (
                query.id == request.session.get('query_id', None)):
            query_results = graph.query(query_dict, headers=headers)
            request.session['query'] = json.dumps(query_dict)
            request.session['query_aliases'] = query_aliases
            request.session['query_fields'] = query_fields
        else:
            query_results = query.execute(headers=headers)
            request.session['query'] = query.query_dict
            request.session['query_aliases'] = query.query_aliases
            request.session['query_fields'] = query.query_fields
    else:
        page_dir = order_dir
        # We check the properties of the results to see if we have
        # aggregates. This is for a special treatment in the order_by.
        aggregate = order_by_field.split('(')[0]
        has_aggregate = aggregate in AGGREGATES
        if has_aggregate:
            alias = 'aggregate'
            value = order_by_field.replace('`', '')
            order_by = (alias, value, order_dir)
        else:
            # We split the header to get the alias and the property
            order_by_values = order_by_field.split('.')
            alias = order_by_values[0]
            prop = order_by_values[1]
            order_by = (alias, prop, order_dir)
        if different_queries and (
                query.id == request.session.get('query_id', None)):
            # We check if the type of query_results is appropiate
            if type(query_dict) != 'dict':
                query_dict = json.loads(query_dict)
            query_results = graph.query(query_dict, order_by=order_by,
                                        headers=headers)
        else:
            query_results = query.execute(order_by=order_by, headers=headers)
        if not query_results:
            messages.error(request,
                           _("Error: You are trying to sort a \
                              column with some none values"))
            query_results = query.execute(headers=headers)
        if order_dir == ASC:
            order_dir = DESC
        elif order_dir == DESC:
            order_dir = ASC
    # We assign the query id to the query_id of the session
    request.session['query_id'] = query.id
    # We save the values to export as CSV
    request.session["csv_results"] = query_results
    request.session["query_name"] = query.name
    # We store the results count in the session variable.
    query_results_length = len(query_results)
    request.session['results_count'] = query_results_length
    # We store the datetime of execution
    query.last_run = datetime.now()
    # And then the results
    headers_results = []
    if query_results_length > 1:
        # We treat the headers
        if headers:
            # If the results have headers, we get the position 0
            headers_results = query_results[0]
        request.session['results_count'] = query_results_length - 1
        query_results = query_results[1:]
        query.results_count = request.session.get('results_count', None)
    else:
        query_results = []
    # We save the new changes of the query
    query.save()
    # We add pagination for the list of queries
    page = request.GET.get('page', 1)
    page_size = rows_number
    try:
        if show_mode == "per_page":
            page_size = rows_number
            paginator = Paginator(query_results, page_size)
        else:
            page_size = settings.DATA_PAGE_SIZE
            query_results = query_results[:rows_number]
            paginator = Paginator(query_results, page_size)
        paginated_results = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        paginated_results = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        paginated_results = paginator.page(paginator.num_pages)
    # TODO: Try to make the response streamed
    return render_to_response('queries/queries_new_results.html',
                              {"graph": graph,
                               "queries_link": queries_link,
                               "queries_name": queries_name,
                               "headers": headers_results,
                               "results": paginated_results,
                               "order_by": order_by_field,
                               "dir": order_dir,
                               "page_dir": page_dir,
                               "csv_results": query_results},
                              context_instance=RequestContext(request))
Пример #3
0
def queries_new_results(request, graph_slug):
    graph = get_object_or_404(Graph, slug=graph_slug)
    # Breadcrumbs variables
    queries_link = (reverse("queries_list", args=[graph.slug]),
                    _("Queries"))
    queries_new = (reverse("queries_new", args=[graph.slug]),
                   _("New"))
    # We declare the form with the results options
    form = QueryOptionsForm()
    # We add order for the list of queries
    order_by_field = request.GET.get('order_by', 'default')
    order_dir = request.GET.get('dir', 'desc')
    page_dir = request.GET.get('page_dir', 'desc')
    # We get the information to mantain the last query in the builder
    query = request.POST.get("query", "").strip()
    query_aliases = request.POST.get("query_aliases", "").strip()
    query_fields = request.POST.get("query_fields", "").strip()
    # query = "notas of autor with notas that start with lista"
    # see https://gist.github.com/versae/9241069
    request.session['query_id'] = 'new'
    if query is not '':
        request.session['query'] = query
        request.session['query_aliases'] = query_aliases
        request.session['query_fields'] = query_fields
        query_dict = json.loads(query)
    else:
        query_dict = json.loads(request.session.get('query', None))
    # We check if we have options in the form or we use saved options
    if request.POST:
        data = request.POST.copy()
        request.session['data'] = data
        form = QueryOptionsForm(data=data)
        if form.is_valid():
            rows_number = form.cleaned_data["rows_number"]
            show_mode = form.cleaned_data["show_mode"]
    else:
        data = request.session.get('data', None)
        form = QueryOptionsForm(data=data)
        if form.is_valid():
            rows_number = form.cleaned_data["rows_number"]
            show_mode = form.cleaned_data["show_mode"]
    headers = True
    if order_by_field == 'default':
        query_results = graph.query(query_dict, headers=headers)
    else:
        page_dir = order_dir
        # We check the properties of the results to see if we have
        # aggregates. This is for a special treatment in the order_by.
        aggregate = order_by_field.split('(')[0]
        has_aggregate = aggregate in AGGREGATES
        if has_aggregate:
            alias = 'aggregate'
            value = order_by_field.replace('`', '')
            order_by = (alias, value, order_dir)
        else:
            # We split the header to get the alias and the property
            order_by_values = order_by_field.split('.')
            alias = order_by_values[0]
            prop = order_by_values[1]
            order_by = (alias, prop, order_dir)
        query_results = graph.query(query_dict,
                                    order_by=order_by, headers=headers)
        if not query_results:
            messages.error(request,
                           _("Error: You are trying to sort a \
                              column with some none values"))
            query_results = graph.query(query_dict, headers=headers)
        # We need the order_dir for the icons in the frontend
        if order_dir == ASC:
            order_dir = DESC
        elif order_dir == DESC:
            order_dir = ASC
    # We save the values to export as CSV
    request.session["csv_results"] = query_results
    request.session["query_name"] = NEW_QUERY
    # We store the results count in the session variable.
    query_results_length = len(query_results)
    request.session['results_count'] = query_results_length
    # We need to substract 1 to the results count
    headers_results = []
    if query_results_length > 1:
        # We treat the headers
        if headers:
            # If the results have headers, we get the position 0
            # and then the results.
            headers_results = query_results[0]
        request.session['results_count'] = query_results_length - 1
        query_results = query_results[1:]
    else:
        query_results = []
    # We add pagination for the list of queries
    page = request.GET.get('page', 1)
    try:
        if show_mode == "per_page":
            page_size = rows_number
            paginator = Paginator(query_results, page_size)
        else:
            page_size = settings.DATA_PAGE_SIZE
            query_results = query_results[:rows_number]
            paginator = Paginator(query_results, page_size)
        paginated_results = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        paginated_results = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        paginated_results = paginator.page(paginator.num_pages)
    return render_to_response('queries/queries_new_results.html',
                              {"graph": graph,
                               "queries_link": queries_link,
                               "queries_new": queries_new,
                               "headers": headers_results,
                               "results": paginated_results,
                               "order_by": order_by_field,
                               "dir": order_dir,
                               "page_dir": page_dir,
                               "csv_results": query_results},
                              context_instance=RequestContext(request))
Пример #4
0
def queries_query_results(request, graph_slug, query_id):
    graph = get_object_or_404(Graph, slug=graph_slug)
    query = graph.queries.get(pk=query_id)

    # Breadcrumbs variables
    redirect_url = reverse("queries_list", args=[graph.slug])
    queries_link = (redirect_url, _("Queries"))
    queries_name = (reverse("queries_query_edit", args=[graph.slug,
                                                        query.id]), query.name)
    # We declare the form to get the options for the results table
    form = QueryOptionsForm()
    # We get the information to mantain the last query in the builder
    query_dict = request.POST.get("query", "").strip()
    query_aliases = request.POST.get("query_aliases", "").strip()
    query_fields = request.POST.get("query_fields", "").strip()
    # We get the order
    order_by_field = request.GET.get('order_by', 'default')
    order_dir = request.GET.get('dir', 'desc')
    page_dir = request.GET.get('page_dir', 'desc')
    # We get the modal variable
    as_modal = bool(request.POST.get("asModal", False))

    # Variable to control if the query has changed to show the save
    # buttons
    query_has_changed = False

    # The next comparisions are because we can execute the queries
    # directly from the list of queries. So, we could obtain empty
    # query dicts, empty query fields...

    # We check if the query saved and the query edited are different
    # In that case, we use the query edited
    if query_dict == '' or query_dict is None:
        query_dict = request.session.get('query')
        if query_dict is None:
            query_dict = query.query_dict
    else:
        query_dict = json.loads(query_dict)
    different_queries = query.query_dict != query_dict

    if query_aliases == '' or query_aliases is None:
        query_aliases = request.session.get('query_aliases')
        if query_aliases is None:
            query_aliases = query.query_aliases

    # We compare the session query fields and the new query fields.
    # This is because we can change the sorting params.
    if query_fields == '' or query_fields is None:
        query_fields = request.session.get('query_fields')
        if query_fields is None:
            query_fields = query.query_fields
    else:
        query_fields = json.loads(query_fields)
    different_sorting_params = query.query_fields != query_fields

    # We get the default sorting params values, in case that we execute
    # the query from the list of queries.
    # Older queries has not the field sortingParams, we need to control it.
    try:
        sorting_params = query.query_fields["sortingParams"]
        rows_number = sorting_params["rows_number"]
        # We need to transform it to integer to avoid exceptions
        rows_number = int(rows_number)
        show_mode = sorting_params["show_mode"]
        select_order_by = sorting_params["select_order_by"]
        dir_order_by = sorting_params["dir_order_by"]
    except KeyError:
        # We set the default values
        rows_number = DEFAULT_ROWS_NUMBER
        show_mode = DEFAULT_SHOW_MODE
        select_order_by = DEFAULT

    # We check if we have options in the form
    if request.POST:
        data = request.POST.copy()
        request.session['data'] = data
        # We add the new choice in the form
        new_choice_order_by = data.get('select_order_by')
        form = QueryOptionsForm(data=data, new_choice=new_choice_order_by)
        if form.is_valid():
            rows_number = form.cleaned_data["rows_number"]
            show_mode = form.cleaned_data["show_mode"]
            select_order_by = form.cleaned_data["select_order_by"]
            dir_order_by = form.cleaned_data["dir_order_by"]
    elif not request.POST and (query.id == request.session.get(
            'query_id', None)):
        data = request.session.get('data', None)
        # We add the new choice in the form
        new_choice_order_by = None
        if data:
            new_choice_order_by = data.get('select_order_by', None)
        form = QueryOptionsForm(data=data, new_choice=new_choice_order_by)
        if form.is_valid():
            rows_number = form.cleaned_data["rows_number"]
            show_mode = form.cleaned_data["show_mode"]
            select_order_by = form.cleaned_data["select_order_by"]
            dir_order_by = form.cleaned_data["dir_order_by"]

    headers = True
    # This part of the code is similar to the same part in the
    # "queries_new_results". The main difference are the blocks
    # to check if we have modify the query or the sorting params
    # to change the correspondent values in the session.
    if order_by_field == DEFAULT and select_order_by == DEFAULT:
        if different_queries and (query.id == request.session.get(
                'query_id', None)):
            # We check if the type of query_dict is appropiate
            if not isinstance(query_dict, dict):
                query_dict = json.loads(query_dict)
            query_results = graph.query(query_dict, headers=headers)
            request.session['query'] = json.dumps(query_dict)
            request.session['query_aliases'] = query_aliases
            request.session['query_fields'] = query_fields
            # Let's check if the sorting params are different
            if different_sorting_params:
                # We check if the type of query_field is appropiate
                if isinstance(query_fields, dict):
                    query_fields = json.dumps(query_fields)
                request.session['query_fields'] = query_fields
            query_has_changed = True
        else:
            query_results = query.execute(headers=headers)
            request.session['query'] = query.query_dict
            request.session['query_aliases'] = query.query_aliases
            request.session['query_fields'] = query.query_fields
    elif order_by_field == NO_ORDER:
        if different_queries and (query.id == request.session.get(
                'query_id', None)):
            # We check if the type of query_dict is appropiate
            if not isinstance(query_dict, dict):
                query_dict = json.loads(query_dict)
            query_results = graph.query(query_dict, headers=headers)
            request.session['query'] = json.dumps(query_dict)
            request.session['query_aliases'] = query_aliases
            request.session['query_fields'] = query_fields
            # Let's check if the sorting params are different
            if different_sorting_params:
                # We check if the type of query_field is appropiate
                if isinstance(query_fields, dict):
                    query_fields = json.dumps(query_fields)
                request.session['query_fields'] = query_fields
            query_has_changed = True
        else:
            query_results = query.execute(headers=headers)
            request.session['query'] = query.query_dict
            request.session['query_aliases'] = query.query_aliases
            request.session['query_fields'] = query.query_fields
    else:
        if order_by_field == DEFAULT and select_order_by != DEFAULT:
            order_by_field = select_order_by
            order_dir = dir_order_by
        page_dir = order_dir
        # We check the properties of the results to see if we have
        # aggregates. This is for a special treatment in the order_by.
        aggregate = order_by_field.split('(')[0]
        # We check if the aggregate has distinct clause
        aggregate = aggregate.split(' ')[0]
        has_aggregate = aggregate in AGGREGATES.keys()
        if has_aggregate:
            alias = AGGREGATE
            # We check the '!' symbol for a correct order
            if order_by_field[-2] != '!':
                order_by_field = (order_by_field[0:-1] + '!' +
                                  order_by_field[-1])
            value = order_by_field.replace('`', '')
            order_by = (alias, value, order_dir)
        else:
            # We split the header to get the alias and the property
            order_by_values = order_by_field.split('.')
            alias = order_by_values[0]
            prop = order_by_values[1]
            if prop[-1] == '!':
                prop = prop[:-1]
            order_by = (alias, prop, order_dir)
        if different_queries and (query.id == request.session.get(
                'query_id', None)):
            # We check if the type of query_dict is appropiate
            if not isinstance(query_dict, dict):
                query_dict = json.loads(query_dict)
            query_results = graph.query(query_dict,
                                        order_by=order_by,
                                        headers=headers)
            request.session['query'] = json.dumps(query_dict)
            if query_aliases == "" or query_fields == "":
                query_aliases = request.session['query_aliases']
                query_fields = request.session['query_fields']
            else:
                request.session['query_aliases'] = query_aliases
                # We check if the type of query_field is appropiate
                if isinstance(query_fields, dict):
                    query_fields = json.dumps(query_fields)
                request.session['query_fields'] = query_fields
            query_has_changed = True
        else:
            query_results = query.execute(order_by=order_by, headers=headers)
            # Let's check if the sorting params are different
            if different_sorting_params:
                # We check if the type of query_field is appropiate
                if isinstance(query_fields, dict):
                    query_fields = json.dumps(query_fields)
                request.session['query_fields'] = query_fields
                query_has_changed = True
        if not query_results:
            messages.error(
                request,
                _("Error: You are trying to sort a \
                              column with some none values"))
            query_results = query.execute(headers=headers)
        if order_dir == ASC:
            order_dir = DESC
        elif order_dir == DESC:
            order_dir = ASC

    # We assign the query id to the query_id of the session
    request.session['query_id'] = query.id
    # We save the values to export as CSV
    request.session["csv_results"] = query_results
    request.session["query_name"] = query.name

    # We store the results count in the session variable.
    query_results_length = len(query_results)
    request.session['results_count'] = query_results_length
    # We store the datetime of execution
    query.last_run = datetime.now()

    headers_final_results = dict()
    headers_query_results = []
    if query_results_length > 1:
        # We treat the headers
        if headers:
            # If the results have headers, we create a dictionary to have
            # the headers showed to the user and the headers using in the
            # backend (this is for the order by click in the header)
            # We check if query_aliases is empty
            if query_aliases is '':
                query_aliases = request.session['query_aliases']
            # We check if the type of query_aliases is appropiate to
            # manipulate it
            if not isinstance(query_aliases, dict):
                aliases = json.loads(query_aliases)
                aliases = aliases['types']
            else:
                aliases = query_aliases['types']
            headers_query_results = query_results[0]
            headers_final_results = _treat_headers(headers_query_results,
                                                   aliases)
        request.session['results_count'] = query_results_length - 1
        query_results = query_results[1:]
    else:
        query_results = []

    # We add the headers to the session to use them if we export the results
    request.session["headers_formatted"] = headers_final_results
    request.session["headers_raw"] = headers_query_results

    # We save the new changes of the query
    query.save()

    # We add pagination for the list of queries
    page = request.GET.get('page', 1)
    page_size = rows_number
    try:
        if show_mode == DEFAULT_SHOW_MODE:
            page_size = rows_number
            paginator = Paginator(query_results, page_size)
        else:
            page_size = settings.DATA_PAGE_SIZE
            query_results = query_results[:rows_number]
            paginator = Paginator(query_results, page_size)
        paginated_results = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        paginated_results = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        paginated_results = paginator.page(paginator.num_pages)

    if as_modal:
        base_template = 'empty.html'
        render = render_to_string
    else:
        base_template = 'base.html'
        render = render_to_response
    add_url = reverse("queries_query_results", args=[graph.slug, query.id])
    broader_context = {
        "graph": graph,
        "query": query,
        "query_has_changed": query_has_changed,
        "queries_link": queries_link,
        "queries_name": queries_name,
        "headers_results": headers_query_results,
        "headers": headers_final_results,
        "results": paginated_results,
        "order_by": order_by_field,
        "dir": order_dir,
        "page_dir": page_dir,
        "csv_results": query_results,
        "base_template": base_template,
        "as_modal": as_modal,
        "add_url": add_url,
        "queries_list_url": redirect_url
    }
    response = render('queries/queries_new_results.html',
                      broader_context,
                      context_instance=RequestContext(request))
    if as_modal:
        response = {
            'type': 'html',
            'action': 'queries_results',
            'html': response
        }
        return HttpResponse(json.dumps(response),
                            status=200,
                            content_type='application/json')
    else:
        return response
Пример #5
0
def queries_query_edit(request, graph_slug, query_id):
    graph = get_object_or_404(Graph, slug=graph_slug)
    nodetypes = NodeType.objects.filter(schema__graph__slug=graph_slug)
    reltypes = RelationshipType.objects.filter(schema__graph__slug=graph_slug)
    redirect_url = reverse("queries_list", args=[graph.slug])
    query = graph.queries.get(pk=query_id)

    # Breadcrumbs variable
    queries_link = (redirect_url, _("Queries"))
    # We get the modal variable
    as_modal = bool(request.GET.get("asModal", False))
    # We declare the form for the results options
    query_options_form = QueryOptionsForm()
    # We get the query_dicts
    query_dict = json.dumps(query.query_dict)
    query_aliases = json.dumps(query.query_aliases)
    query_fields = json.dumps(query.query_fields)

    # We check if we have the variables in the request.session
    if 'query_id' not in request.session \
            or request.session['query_id'] is None:
        request.session['query_id'] = query.id
    if 'query' not in request.session \
            or request.session['query'] is None:
        request.session['query'] = query_dict
    if 'query_aliases' not in request.session \
            or request.session['query_aliases'] is None:
        request.session['query_aliases'] = query_aliases
    if 'query_fields' not in request.session \
            or request.session['query_fields'] is None:
        request.session['query_fields'] = query_fields

    # We get the session query id to check if the dicts saved in the session
    # are for this query
    session_query_id = request.session.get('query_id', None)
    # We check if the query saved and the query edited are different
    # In that case, we use the query edited
    different_queries = query.query_dict != request.session.get('query', None)
    # We compare the session query fields and the new query fields.
    # This is because we can change the sorting params.
    different_sorting_params = (query.query_fields != request.session.get(
        'query_fields', None))

    # We check if we are going to edit the query
    if request.POST:
        data = request.POST.copy()
        as_modal = bool(data.get("asModal", False))
        form = SaveQueryForm(data=data, instance=query)
        if form.is_valid():
            with transaction.atomic():
                query = form.save(commit=False)
                # We treat the results_count
                results_count = request.session.get('results_count', None)
                if results_count:
                    query.results_count = results_count
                    query.last_run = datetime.now()
                else:
                    query.results_count = 0
                query.save()
                if as_modal:
                    response = {
                        'type': 'redirect',
                        'action': 'queries_list',
                        'url': redirect_url
                    }
                    return HttpResponse(json.dumps(response),
                                        status=200,
                                        content_type='application/json')
                else:
                    return redirect(redirect_url)

    # We have to get the values of the query to introduce them into the form
    form = SaveQueryForm(instance=query)

    # If the queries have changed and the id is the same, we use the dicts
    # of the session
    if different_queries and session_query_id == query.id:
        query_dict = request.session.get('query', None)
        query_aliases = request.session.get('query_aliases', None)
        query_fields = request.session.get('query_fields', None)
    if different_sorting_params and session_query_id == query.id:
        query_fields = request.session.get('query_fields', None)

    if as_modal:
        base_template = 'empty.html'
        render = render_to_string
    else:
        base_template = 'base.html'
        render = render_to_response
    add_url = reverse("queries_query_edit", args=[graph_slug, query_id])
    broader_context = {
        "graph": graph,
        "node_types": nodetypes,
        "relationship_types": reltypes,
        "queries_link": queries_link,
        "query": query,
        "form": form,
        "query_options_form": query_options_form,
        "query_dict": query_dict,
        "query_aliases": query_aliases,
        "query_fields": query_fields,
        "base_template": base_template,
        "as_modal": as_modal,
        "add_url": add_url,
        "queries_list_url": redirect_url
    }
    response = render('queries/queries_new.html',
                      broader_context,
                      context_instance=RequestContext(request))
    if as_modal:
        response = {'type': 'html', 'action': 'queries_new', 'html': response}
        return HttpResponse(json.dumps(response),
                            status=200,
                            content_type='application/json')
    else:
        return response
Пример #6
0
def queries_new(request, graph_slug):
    graph = get_object_or_404(Graph, slug=graph_slug)
    nodetypes = NodeType.objects.filter(schema__graph__slug=graph_slug)
    reltypes = RelationshipType.objects.filter(schema__graph__slug=graph_slug)
    redirect_url = reverse("queries_list", args=[graph.slug])

    # We declare the forms needed
    form = SaveQueryForm()
    query_options_form = QueryOptionsForm()
    # Breadcrumbs variable
    queries_link = (redirect_url, _("Queries"))
    # We get the modal variable
    as_modal = bool(request.GET.get("asModal", False))
    # We get the query_dicts of the session variable if they exist
    query_id = request.session.get('query_id', None)
    query_dict = None
    query_aliases = None
    query_fields = None
    # Variable to control the save as new to show the form
    save_as_new = False

    # We check if we have the variables in the request.session
    if 'query_id' not in request.session:
        request.session['query_id'] = None
    if 'query' not in request.session:
        request.session['query'] = None
    if 'query_aliases' not in request.session:
        request.session['query_aliases'] = None
    if 'query_fields' not in request.session:
        request.session['query_fields'] = None
    if 'results_count' not in request.session:
        request.session['results_count'] = None

    # If query_id is 'new' means that we need to get the variables of the
    # session to load the query
    if query_id == NEW:
        query_dict = request.session.get('query', None)
        query_aliases = request.session.get('query_aliases', None)
        query_fields = request.session.get('query_fields', None)
        # We need to check the name and the description for the save as new
        query_name = request.session.get('query_name', None)
        query_description = request.session.get('query_description', None)
        # Variable save as new equals to True
        save_as_new = True

        # We create the data dictionary for the form for the save as new
        data = dict()
        data['graph'] = graph
        data['name'] = query_name
        data['description'] = query_description
        data['query_dict'] = query_dict
        data['query_fields'] = query_fields
        data['query_aliases'] = query_aliases
        data['results_count'] = 0
        data['last_run'] = datetime.now()

        # And we add the data to the form
        form = SaveQueryForm(data=data)

    if request.POST:
        data = request.POST.copy()
        as_modal = bool(data.get("asModal", False))
        form = SaveQueryForm(data=data)
        if form.is_valid():
            with transaction.atomic():
                query = form.save(commit=False)
                graph.queries.add(query)
                # We treat the results_count
                results_count = request.session.get('results_count', None)
                if results_count:
                    query.results_count = results_count
                    query.last_run = datetime.now()
                else:
                    query.results_count = 0
                query.save()
                graph.save()
                if as_modal:
                    response = {
                        'type': 'redirect',
                        'action': 'queries_list',
                        'url': redirect_url
                    }
                    return HttpResponse(json.dumps(response),
                                        status=200,
                                        content_type='application/json')
                else:
                    return redirect(redirect_url)
    else:
        if as_modal:
            base_template = 'empty.html'
            render = render_to_string
        else:
            base_template = 'base.html'
            render = render_to_response
        add_url = reverse("queries_new", args=[graph_slug])
        broader_context = {
            "graph": graph,
            "queries_link": queries_link,
            "node_types": nodetypes,
            "relationship_types": reltypes,
            "form": form,
            "query_options_form": query_options_form,
            "save_as_new": save_as_new,
            "query_dict": query_dict,
            "query_aliases": query_aliases,
            "query_fields": query_fields,
            "base_template": base_template,
            "as_modal": as_modal,
            "add_url": add_url,
            "queries_list_url": redirect_url
        }
        response = render('queries/queries_new.html',
                          broader_context,
                          context_instance=RequestContext(request))
        if as_modal:
            response = {
                'type': 'html',
                'action': 'queries_new',
                'html': response
            }
            return HttpResponse(json.dumps(response),
                                status=200,
                                content_type='application/json')
        else:
            return response