Beispiel #1
0
 def test_query_utils(self):
     from myads_service import utils
     
     r = utils.cleanup_payload({'query': 'q=foo&fq=boo&foo=bar&boo=bar'})
     self.assert_(r == {'query': 'fq=boo&q=foo', 'bigquery': ""}, 'wrong output')
     
     r = utils.cleanup_payload({'query': {'q': 'foo', 'fq': 'boo', 'foo': 'bar', 'boo': 'bar'}})
     self.assert_(r == {'query': 'fq=boo&q=foo', 'bigquery': ""}, 'wrong output')
     
     def test_exc():
         utils.cleanup_payload({'query': {'q': 'foo', 'fq': 'boo', 'foo': 'bar', 'boo': 'bar'},
                                'bigquery': 'foo\nbar'})
         
     self.assertRaises(Exception, test_exc)
     
     r = utils.cleanup_payload({'query': {'q': 'foo', 'fq': '{!bitset}', 'foo': 'bar', 'boo': 'bar'},
                                'bigquery': 'foo\nbar'})
     self.assert_(r == {'query': 'fq=%7B%21bitset%7D&q=foo', 'bigquery': 'foo\nbar'})
Beispiel #2
0
def query(queryid=None):
    '''Stores/retrieves the montysolr query; it can receive data in urlencoded
    format or as application/json encoded data. In the second case, you can 
    pass 'bigquery' together with the 'query' like so:
    
    {
        query: {foo: bar},
        bigquery: 'data\ndata\n....'
    }
    '''
    if request.method == 'GET' and queryid:
        q = db.session.query(Query).filter_by(qid=queryid).first()
        if not q:
            return json.dumps({'msg': 'Query not found: ' + queryid}), 404
        return json.dumps({
            'qid': q.qid,
            'query': q.query,
            'numfound': q.numfound
        }), 200

    # get the query data
    try:
        payload, headers = check_request(request)
    except Exception as e:
        return json.dumps({'msg': e.message or e.description}), 400

    if len(payload.keys()) == 0:
        raise Exception('Query cannot be empty')

    payload = cleanup_payload(payload)

    # check we don't have this query already
    query = json.dumps(payload)
    qid = md5.new(headers['X-Adsws-Uid'] + query).hexdigest()
    q = db.session.query(Query).filter_by(qid=qid).first()
    if q:
        return json.dumps({'qid': qid, 'numFound': q.numfound}), 200

    # check the query is valid
    solrq = payload['query'] + '&wt=json'
    r = make_solr_request(query=solrq,
                          bigquery=payload['bigquery'],
                          headers=headers)
    if r.status_code != 200:
        return json.dumps({
            'msg': 'Could not verify the query.',
            'query': payload,
            'reason': r.text
        }), 404

    # extract number of docs found
    num_found = 0
    try:
        num_found = int(r.json()['response']['numFound'])
    except:
        pass

    # save the query
    q = Query(qid=qid, query=query, numfound=num_found)
    db.session.begin_nested()
    try:
        db.session.add(q)
        db.session.commit()
    except exc.IntegrityError as e:
        db.session.rollback()
        return json.dumps({'msg': e.message or e.description}), 400
        # TODO: update
        #q = db.session.merge(q) # force re-sync from database
        #q.updated = datetime.datetime.utcnow()
        #db.session.commit()

    # per PEP-0249 a transaction is always in progress
    db.session.commit()
    return json.dumps({'qid': qid, 'numFound': num_found}), 200
Beispiel #3
0
 def test_exc():
     utils.cleanup_payload({'query': {'q': 'foo', 'fq': 'boo', 'foo': 'bar', 'boo': 'bar'},
                            'bigquery': 'foo\nbar'})
Beispiel #4
0
def query(queryid=None):
    '''Stores/retrieves the montysolr query; it can receive data in urlencoded
    format or as application/json encoded data. In the second case, you can 
    pass 'bigquery' together with the 'query' like so:
    
    {
        query: {foo: bar},
        bigquery: 'data\ndata\n....'
    }
    '''
    if request.method == 'GET' and queryid:
        q = db.session.query(Query).filter_by(qid=queryid).first()
        if not q:
            return json.dumps({'msg': 'Query not found: ' + queryid}), 404
        return json.dumps({
            'qid': q.qid,
            'query': q.query,
            'numfound': q.numfound }), 200
    
    # get the query data
    try:
        payload, headers = check_request(request)
    except Exception as e:
        return json.dumps({'msg': e.message or e.description}), 400
    
    if len(payload.keys()) == 0:
        raise Exception('Query cannot be empty')

    payload = cleanup_payload(payload)
    
    # check we don't have this query already
    query = json.dumps(payload)
    qid = md5.new(headers['X-Adsws-Uid'] + query).hexdigest()
    q = db.session.query(Query).filter_by(qid=qid).first()
    if q:
        return json.dumps({'qid': qid, 'numFound': q.numfound}), 200
    
    # check the query is valid
    solrq = payload['query'] + '&wt=json'
    r = make_solr_request(query=solrq, bigquery=payload['bigquery'], headers=headers)
    if r.status_code != 200:
        return json.dumps({'msg': 'Could not verify the query.', 'query': payload, 'reason': r.text}), 404
    
    # extract number of docs found
    num_found = 0
    try:
        num_found = int(r.json()['response']['numFound'])
    except:
        pass 
    
    # save the query
    q = Query(qid=qid, query=query, numfound=num_found)
    db.session.begin_nested()
    try:
        db.session.add(q)
        db.session.commit()
    except exc.IntegrityError as e:
        db.session.rollback()
        return json.dumps({'msg': e.message or e.description}), 400
        # TODO: update 
        #q = db.session.merge(q) # force re-sync from database
        #q.updated = datetime.datetime.utcnow()
        #db.session.commit()
    
    # per PEP-0249 a transaction is always in progress    
    db.session.commit()
    return json.dumps({'qid': qid, 'numFound': num_found}), 200