Example #1
0
    def post(self, request, *args, **kw):
        sortmap = {
            'name': 'database_name_t',
            'type_name': 'type_t',
            'id': 'id',
            'last_activity': 'date_last_modification_t',
            'date': 'created_t',
        }
        #defaults
        rows=20
        offset=0
        sort_field='name'
        sort_order='asc'

        sortFilter = None

        if request.user.is_authenticated() and (request.user.is_staff or request.user.emif_profile.has_group('exporters')):
            search = request.DATA.get('search', None)
            crows = request.DATA.get('rows', None)
            coffset = request.DATA.get('offset', None)
            csortf = request.DATA.get('sort_field', None)
            csorto = request.DATA.get('sort_order', None)

            if search == None or len(search.strip()) == 0:
                return Response({'status': 'Authenticated', 'method': 'POST', 'Error': 'Must specify a search text filter'}, status=status.HTTP_400_BAD_REQUEST)

            if crows != None:
                rows = crows

            if coffset != None:
                offset = coffset

            if csortf != None:
                sort_field = csortf

            if csorto != None:
                sort_order = csorto

            if sort_order != 'asc' and sort_order != 'desc':
                return Response({'status': 'Authenticated', 'method': 'POST', 'Error': 'Available sort orders are "asc" and "desc"'}, status=status.HTTP_400_BAD_REQUEST)

            try:
                sortFilter = sortmap[sort_field] + " " + sort_order
            except:
                return Response({'status': 'Authenticated', 'method': 'POST', 'Error': 'sort_field can only be name, type_name, id, last_activity or date.'}, status=status.HTTP_400_BAD_REQUEST)



            c = CoreEngine()
            (list_databases,hits) = get_databases_from_solr_v2(request, "text_t:\"" +escapeSolrArg(search)+'"', sort=sortFilter, rows=rows, start=offset)

            return Response({'link': {'status': 'Authenticated', 'method': 'POST'}, 'filters':{'search': search, 'rows': rows,
                'offset':offset}, 'result': {'count': len(list_databases),
                'databases': [d.__dict__ for d in list_databases]}}, status=status.HTTP_200_OK)

        return Response({'status': 'NOT authenticated', 'method': 'POST'}, status=status.HTTP_401_UNAUTHORIZED)
def results_fulltext(request, page=1, full_text=True,template_name='results.html', isAdvanced=False, query_reference=None, advparams=None):
    query = ""
    in_post = True
    try:
        query = request.POST['query']
        request.session['query'] = query
    except:
        in_post = False

    if not in_post:
        query = request.session.get("query","")

    if isAdvanced == False:
        query = "text_t:"+escapeSolrArg(query)
        #print query

    return results_fulltext_aux(request, query, page, template_name, isAdvanced=isAdvanced, query_reference=query_reference, advparams=advparams)
def results_fulltext_aux(request, query, page=1, template_name='results.html', isAdvanced=False, force=False, query_reference=None, advparams=None):

    rows = define_rows(request)
    if request.POST and "page" in request.POST and not force:
        page = request.POST["page"]

    if page == None:
        page = 1

    if query == "" or query == "text_t:" or query.strip()=="text_t:*" :
        return render(request, "results.html", {'request': request, 'breadcrumb': True,  'isSearch': True,
                                                'results': True, 'hide_add': True,
                                                'num_results': 0, 'page_obj': None})

    query = query + " AND " + typeFilter(request.user)

    (sortString, filterString, sort_params, range) = paginator_process_params(request.POST, page, rows)
    sort_params["base_filter"] = query;
    query_filtered=query
    if len(filterString) > 0:
        query_filtered += " AND " + filterString

    try:
        hlfl = ",".join(advparams)
    except:
        hlfl = None

    if isAdvanced:
        (list_databases, hits, hi) = get_databases_from_solr_with_highlight(request, query_filtered, sort=sortString, rows=rows, start=range, hlfl=hlfl)
    else:
        (list_databases, hits, hi) = get_databases_from_solr_with_highlight(request, query_filtered, sort=sortString, rows=rows, start=range)

    if not isAdvanced:
        hi = merge_highlight_results( '"'+escapeSolrArg(request.session["query"])+'"' , hi)
    else:
        hi = merge_highlight_results( None , hi)


    if range > hits and not force:
        return results_fulltext_aux(request, query, 1, isAdvanced=isAdvanced, force=True)

    request.session["highlight_results"] = hi

    if len(list_databases) == 0 :
        query_old = request.session.get('query', "")
        if isAdvanced == True:
            return render(request, "results.html", {'request': request, 'breadcrumb': True,  'isSearch': True,
                                                'results': True, 'hide_add': True,
                                                'num_results': 0, 'page_obj': None, 'isAdvanced': True})
        else:
            return render(request, "results.html", {'request': request, 'breadcrumb': True, 'isSearch': True,
                                                'results': True, 'hide_add': True,
                                                'num_results': 0, 'page_obj': None, 'search_old': query_old, 'isAdvanced': False})

    list_databases = paginator_process_list(list_databases, hits, range)

    # only execute if this if we are not posting back, we dont want to do this on changing page or applying filters
    if request.POST.get('page') == None and query_reference != None:
        # anotate the databases appearing on results
        anotateshowonresults.delay(query_filtered, request.user, isAdvanced, query_reference)

    myPaginator = Paginator(list_databases, rows)
    try:
        pager =  myPaginator.page(page)
    except PageNotAnInteger, e:
        pager =  myPaginator.page(page)
def results_diff(request, page=1, template_name='results.html'):

    # in case the request come's from a advanced search

    if request.POST.get("qid") != None:

        request.session['isAdvanced'] = True
        qlist = []
        jsinclude = []      # js files to include
        cssinclude = []     # css files to include
        jstriggers = []
        qvalues = {}
        qexpression = None  # boolean expression
        qserialization = None   # boolean expression serialization to show on results
        qid = None  #questionary id
        if request.POST:
            for k, v in request.POST.items():

                if (len(v)==0):
                    continue
                if k.startswith("question_"):
                    s = k.split("_")
                    if len(s) == 4:
                        #qvalues[s[1]+'_'+v] = '1' # evaluates true in JS
                        if (qvalues.has_key(s[1])):
                            qvalues[s[1]] += " " + v # evaluates true in JS
                        else:
                            qvalues[s[1]] = v # evaluates true in JS
                    elif len(s) == 3 and s[2] == 'comment':
                        qvalues[s[1] + '_' + s[2]] = v
                    else:
                        if (qvalues.has_key(s[1])):
                            qvalues[s[1]] += " " + v
                        else:
                            qvalues[s[1]] = v
                            #print qvalues
                elif k == "boolrelwidget-boolean-representation":
                    qexpression = v
                elif k == "boolrelwidget-boolean-serialization":
                    # we add the serialization to the session
                    request.session['serialization_query'] = v
                    qserialization = v
                elif k == "qid":
                    qid = v

            if qexpression == None or qserialization == None or qexpression.strip()=="" or qserialization.strip() == "":
                response = HttpResponse()
                response.status_code = 500
                return response

            query = convert_qvalues_to_query(qvalues, qid, qexpression)
            (query, advparams) = convert_query_from_boolean_widget(qexpression, qid)
            #print "Query: " + query


            request.session['query'] = query
            request.session['advparams'] = advparams

            # We will be saving the query on to the serverside to be able to pull it all together at a later date
            try:
                # we get the current user
                this_user = User.objects.get(username = request.user)

                this_query = None

                this_query_hash = hashlib.sha1(qserialization).hexdigest()

                #print "This query is new, adding it and answers to it..."

                quest = None
                try:
                    quest = Questionnaire.objects.get(id = qid)
                except Questionnaire.DoesNotExist:
                    print "Questionnaire doesnt exist..."

                this_query = AdvancedQuery(user=this_user,name=("Query on "+timezone.now().strftime("%Y-%m-%d %H:%M:%S.%f")),
                    serialized_query_hash=this_query_hash,
                    serialized_query=qserialization, qid=quest)
                this_query.save()
                # and we all so insert the answers in a specific table exactly as they were on the post request to be able to put it back at a later time
                for k, v in request.POST.items():
                    if k.startswith("question_") and len(v) > 0:
                        aqa = AdvancedQueryAnswer(refquery=this_query,question=k, answer=v)
                        aqa.save()

                serialization_a = AdvancedQueryAnswer(refquery=this_query, question="boolrelwidget-boolean-representation", answer=qexpression)
                serialization_a.save()

                request.session['query_id'] = this_query.id
                request.session['query_type'] = this_query.qid.id

            except User.DoesNotExist:
                return HttpResponse("Invalid username")



            return results_fulltext_aux(request, query, isAdvanced=True, query_reference=this_query, advparams=advparams)



    query = ""
    advparams=None
    simple_query=None
    in_post = True
    try:
        query = request.POST['query']

        # must save only on post, and without the escaping so it doesnt encadeate escapes on queries remade
        if query != "" and request.POST.get('page') == None:
            simple_query = store_query(request, query)

        query = '"'+escapeSolrArg(query)+'"'

        request.session['query'] = query

        request.session['isAdvanced'] = False
        request.session['query_id'] = -1
        request.session['query_type'] = -1
    except:
        in_post = False
        #raise
    if not in_post:
        query = request.session.get('query', "")
        advparams=request.session.get('advparams', None)

    if query == "":
        return render(request, "results.html", {'request': request,
                                                'num_results': 0, 'page_obj': None, 'breadcrumb': True})
    try:
        # Store query by the user
        if 'search_full' in request.POST:
            search_full = request.POST['search_full']
            request.session['search_full'] = 'search_full'
        else:
            print "try to get in session"
            search_full = request.session.get('search_full', "")
        if search_full == "search_full":
            return results_fulltext(request, page, full_text=True, isAdvanced=request.session['isAdvanced'], query_reference=simple_query, advparams=advparams)
    except:
        raise
    #print "Printing the qexpression"
    #print request.POST['qexpression']
    return results_fulltext(request, page, full_text=False, isAdvanced=request.session['isAdvanced'], query_reference=simple_query, advparams=advparams)
Example #5
0
    def post(self, request, *args, **kw):
        sortmap = {
            'name': 'database_name_t',
            'type_name': 'type_t',
            'id': 'id',
            'last_activity': 'date_last_modification_t',
            'date': 'created_t',
        }
        #defaults
        rows = 20
        offset = 0
        sort_field = 'name'
        sort_order = 'asc'
        schema = None

        sortFilter = None

        if request.user.is_authenticated() and (
                request.user.is_staff
                or request.user.emif_profile.has_group('exporters')
                or request.user.emif_profile.has_group('developers')):
            search = request.DATA.get('search', None)
            crows = request.DATA.get('rows', None)
            coffset = request.DATA.get('offset', None)
            csortf = request.DATA.get('sort_field', None)
            csorto = request.DATA.get('sort_order', None)
            schema = request.DATA.get('schema', None)

            if search == None or len(search.strip()) == 0:
                return Response(
                    {
                        'status': 'Authenticated',
                        'method': 'POST',
                        'Error': 'Must specify a search text filter'
                    },
                    status=status.HTTP_400_BAD_REQUEST)

            if crows != None:
                rows = crows

            if coffset != None:
                offset = coffset

            if csortf != None:
                sort_field = csortf

            if csorto != None:
                sort_order = csorto

            if sort_order != 'asc' and sort_order != 'desc':
                return Response(
                    {
                        'status': 'Authenticated',
                        'method': 'POST',
                        'Error': 'Available sort orders are "asc" and "desc"'
                    },
                    status=status.HTTP_400_BAD_REQUEST)

            try:
                sortFilter = sortmap[sort_field] + " " + sort_order
            except:
                return Response(
                    {
                        'status':
                        'Authenticated',
                        'method':
                        'POST',
                        'Error':
                        'sort_field can only be name, type_name, id, last_activity or date.'
                    },
                    status=status.HTTP_400_BAD_REQUEST)

            filter_value = ''
            if schema != None:
                filter_value = 'AND type_t: "%s"' % escapeSolrArg(schema)

            c = CoreEngine()
            (list_databases, hits) = get_databases_from_solr_v2(
                request,
                'text_t:"%s" %s' % (escapeSolrArg(search), filter_value),
                sort=sortFilter,
                rows=rows,
                start=offset)

            return Response(
                {
                    'link': {
                        'status': 'Authenticated',
                        'method': 'POST'
                    },
                    'filters': {
                        'search': search,
                        'rows': rows,
                        'offset': offset
                    },
                    'result': {
                        'count': len(list_databases),
                        'databases': [d.__dict__ for d in list_databases]
                    }
                },
                status=status.HTTP_200_OK)

        return Response({
            'status': 'NOT authenticated',
            'method': 'POST'
        },
                        status=status.HTTP_401_UNAUTHORIZED)