@csrf_exempt
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(message % "{'Error':'%s'}")
    
# pass url-encoded json that's not quite so weird.
# instead of using:
# {"q0":{"query":"runyan, jon","type":"","type_strict":"should","properties":[{"pid":"state","v":"NJ"}]},"q1":{"query":"Romney, Mitt","type":"","type_strict":"should","properties":[{"pid":"state","v":""}]}}
# use: 
# {"q0":{"query":"runyan, jon","state":"NJ"}, "q1":{"query":"Romney, Mitt","state":"", "cycle":"2012"}}
dataset/ETHPy150Open sunlightlabs/read_FEC/fecreader/reconciliation/views.py/refine/swapped operands of `%`