def test_search(self): uri1 = u'http://xyz.com' uri2 = u'urn:uuid:xxxxx' user1 = u'levin' user2 = u'anna' anno1 = Annotation(uri=uri1, text=uri1, user=user1) anno2 = Annotation(uri=uri1, text=uri1 + uri1, user=user2) anno3 = Annotation(uri=uri2, text=uri2, user=user1) anno1.save() anno2.save() anno3.save() annotator.es.refresh(timesleep=0.01) res = Annotation.search() h.assert_equal(len(res), 3) # ordering (most recent first) h.assert_equal(res[0]['text'], uri2) res = Annotation.count() h.assert_equal(res, 3) res = Annotation.search(limit=1) h.assert_equal(len(res), 1) res = Annotation.count(limit=1) h.assert_equal(res, 3) res = Annotation.search(uri=uri1) h.assert_equal(len(res), 2) h.assert_equal(res[0]['uri'], uri1) h.assert_equal(res[0]['id'], anno2.id) res = Annotation.search(user=user1) h.assert_equal(len(res), 2) h.assert_equal(res[0]['user'], user1) h.assert_equal(res[0]['id'], anno3.id) res = Annotation.search(user=user1, uri=uri2) h.assert_equal(len(res), 1) h.assert_equal(res[0]['user'], user1) h.assert_equal(res[0]['id'], anno3.id) res = Annotation.count(user=user1, uri=uri2) h.assert_equal(res, 1)
def search_annotations(): kwargs = dict(request.args.items()) uid = current_user_id() if uid: if not auth.verify_request(request): return _failed_auth_response() results = Annotation.search(**kwargs) results = filter(lambda a: authz.authorize(a, 'read', uid), results) total = Annotation.count(**kwargs) return jsonify({ 'total': total, 'rows': results, })