Пример #1
0
def search():
    def reply(json):
        resp = make_response(dumps(json))
        resp.mimetype = "application/json"
        return resp

    query = request.args.get("q")
    if not query:
        return reply({"error": "No query given"})
    try:
        tree = parse_query(query)
    except QueryParseException as exc:
        return reply({"error": exc.args[0]})

    page = request.args.get("p", 1)
    try:
        page = int(page)
    except ValueError:
        return reply({"error": u"Invalid page number: %s" % page})

    highlight = request.args.get("hl", "0")
    highlight = highlight.lower() not in ["0", "false", "no"]

    count, codelets = database.search(tree, page)
    results = [clt.serialize(highlight) for clt in codelets]
    return reply({"count": count, "results": results})
Пример #2
0
 def test_parse(self):
     """test full query parsing"""
     for test, expected in TESTS:
         self.assertEqual(expected, parse_query(test).serialize())