示例#1
0
def refine_json(request, reconciliation_type):
    
    fuzzy=True
    if reconciliation_type == 'fec_ids':
        pass
    elif reconciliation_type == 'fec_ids_nofuzzy':
        fuzzy=False
    else:
        raise Exception ("Invalid reconciliation type: %s" % (reconciliation_type))
    
    queries = request.REQUEST.get('queries')
    if queries:
        q = json.loads(queries)
        thisjson={}
        if q is not None:
            for key, query in q.iteritems():
                
                state = query.get('state', None)
                office = query.get('office', None)
                cycle = query.get('cycle', None)
                result = run_fec_query(query['query'], state=state, office=office, cycle=cycle, fuzzy=fuzzy)
                thisjson[key] = {'result':result}
        return render_to_json(json.dumps(thisjson))

        
    else:
        message = "Couldn't decode the query JSON!"
        return render_to_json("{'Error':'%s'}" % message)
示例#2
0
def refine(request, reconciliation_type):
    
    #print "request is: %s" % (request)    
    # spec is to return metadata for any callback arg. 
    if request.REQUEST.get('callback'):
        callbackarg = request.REQUEST.get('callback')
        return get_metadata(callbackarg, reconciliation_type)
        
    query = request.REQUEST.get('query')
    queries = request.REQUEST.get('queries')

    result = {}
    if query:
        print "query is: %s" % query
        # Spec allows a simplified version, i.e. ?query=boston, so check for that first. 
        # ?query={"query":"boston","type":"/music/musical_group"}
        # ?query={"query":"Ford Taurus","limit": 3,"type":"/automotive/model","type_strict":"any","properties": [{"p":"year","v": 2009},{"pid":"/automotive/model/make","v":{"id":"/en/ford"}} ]}
        # !!This means using the word 'query' in an abbreviated search will break !! 
        if not re.search('query', query):
            query = "{\"query\":\"%s\"}" % query
            #print "revised query is: %s" % query
        
        q = json.loads(query)
        result  = do_query(q, reconciliation_type)
        #print "\n" + str(result)
        thisjson={}
        thisjson['result'] = result
        #print "this json = %s" % thisjson
        return render_to_json(json.dumps(thisjson))
        
    elif queries:
        #print "queries is %s" % queries
        # ?queries={ "q0" : { "query" : "hackney" }, "q1" : { "query" : "strategic" } }
        q = json.loads(queries)
        thisjson={}
        if q is not None:

            for key, query in q.iteritems():
                
                result  = do_query(query, reconciliation_type)
                #print "\n" + str(result)
                thisjson[key] = {'result':result}
                
        #print "this json: %s" % thisjson
        return render_to_json(json.dumps(thisjson))
        
    else:
        message = "Couldn't decode the query JSON!"
        return render_to_json("{'Error':'%s'}" % message)